原 创建Java线程的3种方式及对比
2666 | 0 | 1
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime . . .
}
}
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime . . .
}
}
The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
FutureTask task = new FutureTask((Callable)()-> {
int i = 0;
for(; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "循环变量i的值: "+i);
}
return i;
});
通过分析,一般推荐采用实现接口的方式来创建多线程。
文章来源http://blog.sina.com.cn/s/blog_e3734cfe0102xsoo.html
1

sss
16人已关注
领课教育 32535
10334
update 47769
5157
领课教育 18475
husheng 21157
请更新代码 41840
凯哥Java 2427
凯哥Java 2863
凯哥Java 2153