Java多线程的三种实现方式

时间:2023-03-08 17:19:14

java多线程的三种实现方式

一.继承Thread类

二.实现Runnable接口

三.使用ExecutorService, Callable, Future

无论是通过继承Thread类还是实现Runnable接口来实现java多线程, 都得不到线程执行后的结果. 要想得到线程执行后的结果,就需要使用ExecutorService, Callable, Future来实现java多线程.

可返回值的任务必须实现Callable接口, 类似的, 无返回值的任务必须实现Runnable接口. 执行Callable任务后, 可以获取一个Future对象, 在该对象上调用get方法就可以获取到Callable任务返回的结果了, 再结合线程池接口ExecutorService就可以实现有返回结果的多线程了. 下面给出一个完整的例子.

 public class Test {
     public static void main(String[] args) {
         ExecutorService executor = Executors.newCachedThreadPool();
         Task task = new Task();
         Future<Integer> result = executor.submit(task);
         executor.shutdown();

         try {
             Thread.sleep(1000);
         } catch (InterruptedException e1) {
             e1.printStackTrace();
         }

         System.out.println("主线程在执行任务");

         try {
             System.out.println("task运行结果"+result.get());
         } catch (InterruptedException e) {
             e.printStackTrace();
         } catch (ExecutionException e) {
             e.printStackTrace();
         }

         System.out.println("所有任务执行完毕");
     }
 }
 class Task implements Callable<Integer>{
     @Override
     public Integer call() throws Exception {
         System.out.println("子线程在进行计算");
         Thread.sleep(3000);
         int sum = 0;
         for(int i=0;i<100;i++)
             sum += i;
         return sum;
     }
 }

执行结果

 子线程在进行计算
 主线程在执行任务
 task运行结果4950
 所有任务执行完毕

上述代码中, Executors类提供了一系列工厂方法用于创建线程池, 返回的线程池都实现了ExecutorService接口.

/**
 *创建固定数目线程的线程池
*/
public static ExecutorService newFixedThreadPool(int nThreads) 

/**  *创建可缓存的线程池, 调用execute()将重用以前构造的线程(如果线程可用). *如果现有线程没有可用的, 则创建一个新线程并添加到线程池中. *终止并从缓存中移除那些已有60秒未被使用的线程*/public static ExecutorService newCachedThreadPool() 
/**
 *创建一个单线程化的Executor
*/public static ExecutorService newSingleThreadExecutor() 

/**  *创建一个支持定时和周期性的任务执行的线程池*/public static ScheduledExecutorService newScheduledThreadPool() 

ExecutorService提供了submit()方法, 传递一个Callable,或Runnable,返回Future。如果Executor后台线程池还没有完成Callable的计算,这时调用Future对象的get()方法时,会阻塞直到计算完成.