原 创建Java线程的3种方式及对比
2687 | 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人已关注
领课教育 32913
10631
update 48098
5360
领课教育 18686
husheng 21319
请更新代码 42055
凯哥Java 2631
凯哥Java 3067
凯哥Java 2326