1、传统线程机制的回顾java
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.二、实现Runnable接口ide
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.三、Timer类和TimerTask类的应用oop
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 线程的同步互斥与通信spa
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<s.length();i++){ System.out.print(s.charAt(i)); } System.out.println(); } } public synchronized static void outprint2(String s){ for(int i =0;i<s.length();i++){ System.out.print(s.charAt(i)); } System.out.println(); } }
2、java5线程池方式线程
l线程池的概念与Executors类的应用code
** * 步骤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(); } } } }