最近在学习并发,因而我在网上搜了一本《java并发编程实战》书学习。java
在我印象中建立线程有两种方式编程
1. 继承Thread类,重写run方法,实例化本身写Thread子类,并用start()方法开启。缓存
2.实现Runnable接口,重写run方法,把Runnable的子类的实例对象做为Thread的构造参数传递进去,建立线程,并开启。并发
可是我看别人代码时大部分都用第一种方式,直接new Thread 而后重写run方法。其实第二种方式更加符合面向对象的编程,由于,Thread是一个线程,他只管建立和开启线程,而不该该进行逻辑的处理代码写到里面,逻辑处理应该交给Runnable的子类的进行。ide
传统定时器是Timer类,建立方式工具
Timer timer = new Timer(); timer.schedule(new TimerTask(){ //建立定时器任务 @Override public void run() { System.out.println("你好"); } },2000); //2秒以后打印你好 timer.schedule(new TimerTask(){ @Override public void run() { System.out.println("你好"); } },2000,3000); //2秒以后打印你好,接着每3秒打印一次你好。
此处有个要求,须要在2以后打印你好,3秒以后打印世界...,而后不断的循环打印下去。该怎么办?oop
思路:1.我能够建立两个定时器任务,task1任务2秒后打印你好,task2任务3秒后打印,而后在task1任务结束时开启task2,在task2结束时开启task1学习
2.能够只建立一个task ,让后在外部作一个 flag标记,当为true是执行打印你好,而后在结束时开启一个新任务,并把flag=!flag;
spa
//第一种实现 public static void main(String[] args) { new Timer().schedule(new MyTimerTask1(), 2000); while (true) { System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static class MyTimerTask1 extends TimerTask { @Override public void run() { System.out.println("hello"); new Timer().schedule(new MyTimerTask2(), 3000); } } static class MyTimerTask2 extends TimerTask { @Override public void run() { System.out.println("wrold"); new Timer().schedule(new MyTimerTask1(), 2000); } } }
第二种能够就不贴了。线程
使用Executors工具类进行建立线程池
API的介绍:
newFixedThreadPool 建立一个固定长度的线程池,当到达线程最大数量时,线程池的规模将再也不变化。
newCachedThreadPool 建立一个可缓存的线程池,若是当前线程池的规模超出了处理需求,将回收空的线程;当需求增长时,会增长线程数量;线程池规模无限制。
newSingleThreadPoolExecutor 建立一个单线程的Executor,确保任务对了,串行执行(此单个线程死以后又会有个线程代替他)
public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(3); //第1种 // ExecutorService threadPool = Executors.newCachedThreadPool();//第2种 // ExecutorService threadPool = Executors.newSingleThreadExecutor();//第3种 for (int i = 1; i <= 10; i++) { final int task = i; threadPool.execute(new Runnable() { @Override public void run() { for (int j = 1; j <= 5; j++) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task); } } }); } }
把10个任务交个3种线程池去完成,
结果
第1种的结果 10个任务只有个开起固定的3个线程去完成任务 pool-1-thread-1 is looping of 1 for task of 1 pool-1-thread-3 is looping of 1 for task of 3 pool-1-thread-2 is looping of 1 for task of 2 pool-1-thread-1 is looping of 2 for task of 1 pool-1-thread-2 is looping of 2 for task of 2 pool-1-thread-3 is looping of 2 for task of 3 pool-1-thread-2 is looping of 1 for task of 6 pool-1-thread-1 is looping of 1 for task of 4 pool-1-thread-3 is looping of 1 for task of 5 pool-1-thread-3 is looping of 2 for task of 5 pool-1-thread-3 is looping of 3 for task of 3 pool-1-thread-2 is looping of 4 for task of 2 第2种的结果 10个任务开起了10个线程去完成 pool-1-thread-1 is looping of 1 for task of 1 pool-1-thread-3 is looping of 1 for task of 3 pool-1-thread-2 is looping of 1 for task of 2 pool-1-thread-9 is looping of 1 for task of 9 pool-1-thread-10 is looping of 1 for task of 10 pool-1-thread-6 is looping of 1 for task of 6 pool-1-thread-5 is looping of 1 for task of 5 pool-1-thread-4 is looping of 1 for task of 4 pool-1-thread-8 is looping of 1 for task of 8 pool-1-thread-7 is looping of 1 for task of 7 pool-1-thread-3 is looping of 2 for task of 3 pool-1-thread-1 is looping of 2 for task of 1 pool-1-thread-2 is looping of 2 for task of 2 pool-1-thread-7 is looping of 2 for task of 7 pool-1-thread-4 is looping of 2 for task of 4 pool-1-thread-5 is looping of 2 for task of 5 pool-1-thread-9 is looping of 2 for task of 9 pool-1-thread-8 is looping of 2 for task of 8 pool-1-thread-6 is looping of 2 for task of 6 pool-1-thread-10 is looping of 2 for task of 10 第3种的结果 10个任务却只开启了一个线程 pool-1-thread-1 is looping of 1 for task of 1 pool-1-thread-1 is looping of 2 for task of 1 pool-1-thread-1 is looping of 1 for task of 2 pool-1-thread-1 is looping of 2 for task of 2 pool-1-thread-1 is looping of 1 for task of 3 pool-1-thread-1 is looping of 2 for task of 3 pool-1-thread-1 is looping of 1 for task of 4 pool-1-thread-1 is looping of 2 for task of 4 pool-1-thread-1 is looping of 1 for task of 5 pool-1-thread-1 is looping of 2 for task of 5 pool-1-thread-1 is looping of 1 for task of 6 pool-1-thread-1 is looping of 2 for task of 6 pool-1-thread-1 is looping of 1 for task of 7 pool-1-thread-1 is looping of 2 for task of 7 pool-1-thread-1 is looping of 1 for task of 8 pool-1-thread-1 is looping of 2 for task of 8 pool-1-thread-1 is looping of 1 for task of 9 pool-1-thread-1 is looping of 2 for task of 9 pool-1-thread-1 is looping of 1 for task of 10 pool-1-thread-1 is looping of 2 for task of 10
newScheduledThreadPool是建立定时器 和Timer差很少,但Timer内部只有一个线程进行执行任务,而newScheduledThreadPool是能够设置多个线程的
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("bombing!"); } }, 6, 2, TimeUnit.SECONDS);