标签:
最近在学习并发,于是我在网上搜了一本《java并发编程实战》书学习。
在我印象中创建线程有两种方式
1. 继承Thread类,重写run方法,实例化自己写Thread子类,并用start()方法开启。
2.实现Runnable接口,重写run方法,把Runnable的子类的实例对象作为Thread的构造参数传递进去,创建线程,并开启。
但是我看别人代码时大部分都用第一种方式,直接new Thread 然后重写run方法。其实第二种方式更加符合面向对象的编程,因为,Thread是一个线程,他只管创建和开启线程,而不应该进行逻辑的处理代码写到里面,逻辑处理应该交给Runnable的子类的进行。
传统定时器是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秒之后打印世界...,然后不断的循环打印下去。该怎么办?
思路:1.我可以创建两个定时器任务,task1任务2秒后打印你好,task2任务3秒后打印,然后在task1任务结束时开启task2,在task2结束时开启task1
2.可以只创建一个task ,让后在外部做一个 flag标记,当为true是执行打印你好,然后在结束时开启一个新任务,并把flag=!flag;
//第一种实现 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);
标签:
原文地址:http://my.oschina.net/qiuhoude/blog/388039