原 创建Java线程的3种方式及对比
2568 | 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人已关注
领课教育 29704
7895
update 44039
3663
领课教育 16275
husheng 19315
请更新代码 40269
凯哥Java 836
凯哥Java 839
凯哥Java 667