多线程实现的四种方法

1) 继承Thread类

复制代码
 1 public class TestThread {
 2      public static void main(String[] args) {
 3          ThreadDemo threadDemo = new ThreadDemo();
 4          threadDemo.start();
 5      }
 6 }
 7  
 8  
 9 class  ThreadDemo extends Thread{
10  
11     @Override
12     public void run() {
13         boolean flag = false;
14         for(int i  = 3 ; i < 100 ; i ++) {
15             flag = false;
16             for(int j = 2; j <= Math.sqrt(i) ; j++) {
17                 if(i % j == 0) {
18                     flag = true;
19                     break;
20                 }
21             }
22             if(flag == false) {
23                 System.out.print(i+"  ");
24             }
25         }
26     }
27   
28      
29 }
复制代码

2)实现Runnable接口

复制代码
 1 public class TestRunnable {
 2     public static void main(String[] args) {
 3         RunnableDemo runnableDemo = new RunnableDemo();
 4         new Thread(runnableDemo).start();
 5     }
 6 }
 7  
 8 class RunnableDemo implements Runnable{
 9  
10     @Override
11     public void run() {
12         boolean flag = false;
13         for(int i  = 3 ; i < 100 ; i ++) {
14             flag = false;
15             for(int j = 2; j <= Math.sqrt(i) ; j++) {
16                 if(i % j == 0) {
17                     flag = true;
18                     break;
19                 }
20             }
21             if(flag == false) {
22                 System.out.print(i+"  ");
23             }
24         }
25     }
26     
27 }
复制代码

3)实现Callable接口

复制代码
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.concurrent.Callable;
 4 import java.util.concurrent.ExecutionException;
 5 import java.util.concurrent.Executors;
 6 import java.util.concurrent.FutureTask;
 7  
 8 public class TestCallable1 {
 9     public static void main(String[] args) throws InterruptedException, ExecutionException {
10         CallableDemo callableDemo = new CallableDemo();
11         FutureTask futureTask = new FutureTask<>(callableDemo); 
12         new Thread(futureTask).start();
13         List<Integer> lists = (List<Integer>)futureTask.get(); //获取返回值
14         for (Integer integer : lists) {
15             System.out.print(integer + "  ");
16         }
17     }
18 }
19  
20 class CallableDemo implements Callable<List<Integer>>{
21  
22     @Override
23     public List<Integer> call() throws Exception {
24         boolean flag = false;
25         List<Integer> lists = new ArrayList<>();
26         for(int i  = 3 ; i < 100 ; i ++) {
27             flag = false;
28             for(int j = 2; j <= Math.sqrt(i) ; j++) {
29                 if(i % j == 0) {
30                     flag = true;
31                     break;
32                 }
33             }
34             if(flag == false) {
35                 lists.add(i);
36             }
37         }
38         return lists;
39     }
40     
41 }
复制代码

4) 线程池

复制代码
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.concurrent.Callable;
 4 import java.util.concurrent.ExecutionException;
 5 import java.util.concurrent.ExecutorService;
 6 import java.util.concurrent.Executors;
 7 import java.util.concurrent.Future;
 8  
 9 public class TestThreadPool {
10     public static void main(String[] args) throws InterruptedException, ExecutionException {
11         ExecutorService executorService = Executors.newFixedThreadPool(5);
12         List<Future<List<Integer>>> ints = new ArrayList<>();
13         for(int i = 0 ; i < 5; i ++) {
14             Future<List<Integer>> future = executorService.submit(new Callable<List<Integer>>() {
15                 @Override
16                 public List<Integer> call() throws Exception {
17                     boolean flag = false;
18                     System.out.println(Thread.currentThread().getName()+"  ");
19                     List<Integer> lists = new ArrayList<>();
20                     for(int i  = 3 ; i < 100 ; i ++) {
21                         flag = false;
22                         for(int j = 2; j <= Math.sqrt(i) ; j++) {
23                             if(i % j == 0) {
24                                 flag = true;
25                                 break;
26                             }
27                         }
28                         if(flag == false) {
29                             lists.add(i);
30                         }
31                     }
32                     return lists;
33                 }
34             });
35             ints.add(future);
36         }
37         
38         for (Future<List<Integer>> future : ints) {
39             System.out.println(future.get());
40         }
41     }
42 }
43  
44 class ThreadPoolDemo {
45  
46 }
复制代码
原文地址:https://www.cnblogs.com/duguangming/p/11047448.html