本文代碼展示了在一個方法中
下面的代碼展示了在一個方法中
這樣的代碼可用於在一個類內部通過另起線程來執行一個支線任務
package ncurrency;
public class StartFromMethod {
private Thread t;
private int number;
private int count =
public StartFromMethod(int number) {
this
}
public void runTask() {
if (t == null) {
t = new Thread() {
public void run() {
while (true) {
System
+
if (++count ==
return;
}
}
};
t
}
}
public static void main(String[] args) {
for (int i =
new StartFromMethod(i)
}
}
結果
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25892.html