一、传统线程机制的回顾
1.1、继承Thread类
Thread thread1 = new Thread(){ public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("chenweisong"); } } }; thread1.start();
1.2、实现Runnable接口
Thread thread2 = new Thread(new Runnable(){ public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("chenweisong"); } } }); thread2.start();
1.3、Timer类和TimerTask类的应用
Timer timer1 = new Timer(); timer1.schedule(new TimerTask(){ public void run() { System.out.println("bombing!!!!"); } }, 2000); while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
1.4 线程的同步互斥与通讯
final Output output = new Output(); new Thread(new Runnable(){ public void run() { while(true){ try { Thread.sleep(500); output.outprint("chenweisong"); } catch (Exception e) { e.printStackTrace(); } } } }).start(); new Thread(new Runnable(){ public void run() { while(true){ try { Thread.sleep(500); Output.outprint2("wuyouyi"); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } static class Output{ public void outprint(String s){ synchronized(Output.class){ for(int i =0;i
二、java5线程池方式
l线程池的概念与Executors类的应用
** * 步骤1:用3个大小的固定线程池去执行10个内部循环10次就结束的任务, * 为了观察固定线程池下的其他任务一直再等待,希望打印出正在执行的线程名、 * 任务序号和任务内部的循环次数,刚开始看到只有3个线程在执行, * 并看到任务前仆后继的效果。注意:这10个任务要用各自独立的runnable对象,才能看到任务的序号。步骤2:改为缓存线程池,可以看到当前有多少个任务,就会分配多少个线程为之服务。//ExecutorService ThreadPool = Executors.newFixedThreadPool(3); ExecutorService ThreadPool = Executors.newCachedThreadPool();//改为缓存线程池 for (int i = 1; i <= 10; i++) { final int task=i; ThreadPool.execute(new Runnable(){ public void run() { try { //Thread.sleep(100); for(int j=1; j<=10; j++){ System.out.println(Thread.currentThread().getName()+" executor task "+task+" loop of "+j); } } catch (Exception e) { e.printStackTrace(); } } }); } ThreadPool.shutdown();
/** * 用下面这句代码来说明上面的代码是在提交任务, * 并且所有的任务都已经提交了, * 但任务是什么时候执行的,则是由线程池调度的! */ ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); scheduledExecutorService.schedule(//隔多久后执行一次 new Runnable(){ public void run() { System.out.println("bombing!!!!"); } }, 3, TimeUnit.SECONDS); scheduledExecutorService.scheduleAtFixedRate(//隔多久後執行一次,以后每隔多久执行一次 new Runnable(){ public void run() { System.out.println("bombing!!!!"); } }, 3, 1, TimeUnit.SECONDS);
Lock&Condition实现线程同步通信
以下例子为,三个线程轮流做一件事情
package TradicationThreadTest;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class ThreeConditionCommunication { /** * @param args */ public static void main(String[] args) { final Business business = new Business(); new Thread( new Runnable() { @Override public void run() { for(int i=1;i<=50;i++){ business.sub2(i); } } } ).start(); new Thread( new Runnable() { @Override public void run() { for(int i=1;i<=50;i++){ business.sub3(i); } } } ).start(); for(int i=1;i<=50;i++){ business.main(i); } } static class Business { Lock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); private int shouldSub = 1; public void sub2(int i){ lock.lock(); try{ while(shouldSub != 2){ try { condition2.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int j=1;j<=10;j++){ System.out.println("sub2 thread sequence of " + j + ",loop of " + i); } shouldSub = 3; condition3.signal(); }finally{ lock.unlock(); } } public void sub3(int i){ lock.lock(); try{ while(shouldSub != 3){ try { condition3.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int j=1;j<=20;j++){ System.out.println("sub3 thread sequence of " + j + ",loop of " + i); } shouldSub = 1; condition1.signal(); }finally{ lock.unlock(); } } public void main(int i){ lock.lock(); try{ while(shouldSub != 1){ try { condition1.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int j=1;j<=100;j++){ System.out.println("main thread sequence of " + j + ",loop of " + i); } shouldSub = 2; condition2.signal(); }finally{ lock.unlock(); } } }}