我运行了这段代码
NUM = 0
def count():
global NUM
NUM += 1
time.sleep(1)
print(t.getName()+":"+"NUM is "+str(NUM))
for i in range(10):
t = threading.Thread(target=count)
t.start()
Run Code Online (Sandbox Code Playgroud)
输出是
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Thread-10:NUM is 10
Run Code Online (Sandbox Code Playgroud)
我知道为什么NUM总是10,但为什么线程名称总是相同的?每个线程运行print(t.getName()+":"+"NUM is "+str(NUM)); 的t不应该是得到的CPU时间的一个线程?我认为这个名字不应该是一样的.
当我改变这个
NUM = 0
def count():
global NUM
NUM += 1
name = t.getName()
time.sleep(1)
print(name+":"+"NUM is "+str(NUM)) …Run Code Online (Sandbox Code Playgroud) 我有一个这样的代码:
HttpURLConnection connection = (HttpURLConnection) loginUrl.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Referer", "http://www.icourse163.org/member/logout.htm");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
Run Code Online (Sandbox Code Playgroud)
我的连接是 HttpURLConnection 的一个实例,但是当我调试代码时。当代码运行到OutputStream out = connection.getOutputStream();.I时, 我看到getOutputStream()是 sun.net.www.protocol.http.HttpURLConnection的方法。为什么?
我有很多变量,例如isOnPause,isOnStop等等。当 Activity 调用时,onStop()我isOnStop = true想知道是否存在像这样的官方方法this.isOnPause(), this.isOnStop()?
我查了Activity的API参考,只看到了isDestroyed(),但是没有isStoped(), isPaused()。
protect final void onStop(){
isOnStop = true // I don't think this is good way to do this.
}
Run Code Online (Sandbox Code Playgroud)
所以,我想知道是否存在像这样的官方方法this.isOnPause(), this.isOnStop()?