本博客系列是学习并发编程过程当中的记录总结。因为文章比较多,写的时间也比较散,因此我整理了个目录贴(传送门),方便查阅。html
并发编程系列博客传送门java
在Java中有多种方式能够实现多线程编程(记得这是一道常问的面试题,特别是在应届生找工做的时候被问的频率就更高了)。面试
经过继承Thread类来实现多线程编程很容易。下面代码中MyThread
类继承了Thread
类,并重写了run方法。编程
可是这种方式不是很建议使用,其中最主要的一个缘由就是Java是单继承模式,MyThread
类继承了Thread
类以后就不能再继承其余类了。因此使用implement的形式比继承的方式更好。线面会讲到使用Runnable接口实现多线程。多线程
public class MyThread extends Thread { public static final int THREAD_COUNT = 5; public static void main(String[] args) { List<Thread> threadList = new ArrayList<>(); for (int i = 0; i < THREAD_COUNT; i++) { Thread thread = new MyThread(); thread.setName("myThread--"+i); threadList.add(thread); } threadList.forEach(var->{var.start();}); } @Override public void run() { super.run(); System.out.println("my thread name is:"+Thread.currentThread().getName()); Random random = new Random(); int sleepTime = random.nextInt(5); try { TimeUnit.SECONDS.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); }finally { System.out.println(Thread.currentThread().getName()+" end after "+sleepTime+" seconds"); } } }
下面咱们就经过实现Runnable接口的形式来改造下上面的代码。并发
能够发现,经过实现Runnable接口实现多线程编程也很是方便。可是不须要再继承Thread类,减小了耦合。同时new了一个Runner对象后,这个对象能够比较方便地在各个线程之间共享。所以相对于继承Thread的方式,更加推荐使用Runnable接口的方式实现多线程编程。dom
public class MyThread { public static final int THREAD_COUNT = 5; public static void main(String[] args) { List<Thread> threadList = new ArrayList<>(); Runner runner = new Runner(); for (int i = 0; i < THREAD_COUNT; i++) { Thread thread = new Thread(runner); thread.setName("myThread--"+i); threadList.add(thread); } threadList.forEach(var->{var.start();}); } public static class Runner implements Runnable{ @Override public void run() { System.out.println("my thread name is:"+Thread.currentThread().getName()); Random random = new Random(); int sleepTime = random.nextInt(5); try { TimeUnit.SECONDS.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); }finally { System.out.println(Thread.currentThread().getName()+" end after "+sleepTime+" seconds"); } } } }
上面介绍了两种方式均可以很方便地实现多线程编程。可是这两种方式也有几个很明显的缺陷:ide
为了解决以上的问题,在JDK5版本的java.util.concurretn
包中,引入了新的线程实现机制:Callable接口。学习
@FunctionalInterface public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
看了Callable接口的介绍,其实这个接口的功能是和Runnable同样的,和Runnable接口最主要区别就是:线程
下面经过使用Callable接口的方式来改造下上面的代码:
public class MyThread { public static final int THREAD_COUNT = 5; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT); Runner runner = new Runner(); for (int i = 0; i < THREAD_COUNT; i++) { Future<Integer> submit = executorService.submit(runner); //get方法会一直阻塞等到线程执行结束 System.out.println(submit.get()); } executorService.shutdown(); } public static class Runner implements Callable<Integer> { @Override public Integer call() throws Exception { System.out.println("my thread name is:"+Thread.currentThread().getName()); Random random = new Random(); int sleepTime = random.nextInt(500); try { TimeUnit.SECONDS.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); }finally { System.out.println(Thread.currentThread().getName()+" end after "+sleepTime+" seconds"); } return sleepTime; } } }
上面代码中,咱们使用Future
类来获取返回结果。Future接口的主要方法以下: