java多线程实现得方式(第一种:继承Thread类;第二种:实现Runable 接口;第三种:实现Callable接口通过FutureTask包装器来创建Thread线程;第四种:使用ExecutorService、Callable、Future实现有返回结果的多线程)
一.继承Thread类
/** * @ClassName : Thread * @Description: 第一种实现(继承Thread类) * @Author: 13394 * @CreateDate: 2018/10/3 14:44 * @Version: 1.0 */ public class MyThread extends Thread{ @Override public void run() { System.out.println("你是猪啊!!!!!!!!!!!!!!"); } @Test public void test() throws Exception{ Class clazz= Class.forName("com.bootdo.common.config.collection.test.dxc.MyThread"); MyThread object= (MyThread)clazz.newInstance(); object.start(); } }
二.实现Runable 接口
/** * @ClassName : MyRunable * @Description: 第二种实现(实现Runnable接口) * @Author: 13394 * @CreateDate: 2018/10/3 17:01 * @Version: 1.0 */ public class MyRunable implements Runnable { @Override public void run() { System.out.println("你是一条狗!!!!!!!!!!!!!!11"); } @Test public void test() throws Exception{ Class clazz= Class.forName("com.bootdo.common.config.collection.test.dxc.MyRunable"); MyRunable object=(MyRunable)clazz.newInstance(); Thread thread=new Thread(object); //实例化Thread 并且传入自己的MyRunable 的 thread.start(); } }
三.实现Callable接口通过FutureTask包装器来创建Thread线程
/** * @ClassName : MyCallable * @Description: 实现Callable接口通过FutureTask包装器来创建Thread线程 * @Author: 13394 * @CreateDate: 2018/10/3 17:10 * @Version: 1.0 */ public class MyCallable<V> implements Callable<V> { @Override public V call() throws Exception { return null; } @Test public void test(){ Callable<V> callable=new MyCallable<V>(); FutureTask<V> futureTask=new FutureTask<V>(callable); Thread thread=new Thread(); thread.start(); } }
四.使用ExecutorService、Callable、Future实现有返回结果的多线程
/** * @ClassName : MyExecutorServiceCallableFuture * @Description: 使用ExecutorService、Callable、Future实现有返回结果的多线程 * @Author: 13394 * @CreateDate: 2018/10/3 17:18 * @Version: 1.0 */ @RunWith(Parameterized.class) public class MyExecutorServiceCallableFuture implements Callable<Object> { private String taskNum; public MyExecutorServiceCallableFuture(String taskNum) { this.taskNum = taskNum; } @Override public Object call() throws Exception { System.out.println(">>>" + taskNum + "任务启动"); Date dateTmp1 = new Date(); Thread.sleep(1000); Date dateTmp2 = new Date(); long time = dateTmp2.getTime() - dateTmp1.getTime(); System.out.println(">>>" + taskNum + "任务终止"); return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】"; } }
public class Test { @org.junit.Test public void test() throws Exception { System.out.println("----程序开始运行----"); Date date1 = new Date(); int taskSize = 5; // 创建一个线程池 ExecutorService executorService= Executors.newFixedThreadPool(taskSize); List<Future> list=new ArrayList<Future>(); for (int i = 0; i < taskSize; i++) { Callable callable=new MyExecutorServiceCallableFuture(i+" "); Future future= executorService.submit(callable); list.add(future); } executorService.shutdown(); for(Future f:list){ System.out.println(">>>" + f.get().toString()); } Date date2=new Date(); System.out.println("----程序结束运行----,程序运行时间【"+(date2.getTime()-date1.getTime())+"毫秒】"); } }