通过线程按照顺序循环输出ABC n次

通过线程按照顺序循环输出ABC n次

根据题目的意思应该是每个线程输出一个字符,按照顺序输出n次。

对于这个题目应该是3个线程。

分析:线程的执行方式是乱序的,需要通过协作才能实现3个线程顺序输出字符。

在jdk1.4中线程的协作是通过wait/notify/notifyAll实现的,使用这3个方法要求获取同步对象的monitor,否则即使编译成功也会在运行的时候出现 IllegalMonitorStateException的异常。

在jdk1.5中线程的协作是通过Lock/Condition及Samephore/CyclicBarriar/CountLatchDown实现的

 

JDK1.4的实现

 

package demo; public class ABC4 { private int cond = 1;//通过cond来确定A B C的输出 private Object obj = new Object();//同步对象 /** * @param args */ public static void main(String[] args) { ABC4 abc = new ABC4();//内部类,线程执行通过jdk1.4 ThreadA ta = abc.new ThreadA();//声明3个runnable类 ThreadB tb = abc.new ThreadB(); ThreadC tc = abc.new ThreadC(); for (int i = 0; i < 10; i++) { new Thread(ta).start(); new Thread(tb).start(); new Thread(tc).start(); } } class ThreadA implements Runnable{ public void run() { synchronized (obj) { while(true){ if (cond % 3 == 1) { System.out.println("A"); cond++; obj.notifyAll(); break; } else { try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } class ThreadB implements Runnable{ public void run() { synchronized (obj) { while(true){ if (cond % 3 == 2) { System.out.println("B"); cond++; obj.notifyAll(); break; } else { try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } class ThreadC implements Runnable{ public void run() { synchronized (obj) { while(true){ if (cond % 3 == 0) { System.out.println("C"); cond++; obj.notifyAll(); break; } else { try { obj.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } }

 

JDK1.5的实现

package demo; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ABC5 { private int cond = 1;//通过cond来确定A B C的输出 private Lock lock = new ReentrantLock();//通过JDK5中的锁来保证线程的访问的互斥 private Condition condition = lock.newCondition();//线程协作 /** * @param args */ public static void main(String[] args) { ABC5 abc = new ABC5();//内部类线程执行方式jdk1.5 ThreadA ta = abc.new ThreadA();//声明3个runnable类 ThreadB tb = abc.new ThreadB(); ThreadC tc = abc.new ThreadC(); ExecutorService executor = Executors.newFixedThreadPool(3);//通过线程池执行 for (int i = 0; i < 10; i++) { executor.execute(ta); executor.execute(tb); executor.execute(tc); } executor.shutdown();//关闭线程池 } class ThreadA implements Runnable{ public void run() { lock.lock(); try{ while(true){ if (cond % 3 == 1) { System.out.println("A"); cond++; condition.signalAll(); break; } else { try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } } }finally{ lock.unlock(); } } } class ThreadB implements Runnable{ public void run() { lock.lock(); try{ while(true){ if (cond % 3 == 2) { System.out.println("B"); cond++; condition.signalAll(); break; } else { try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } } }finally{ lock.unlock(); } } } class ThreadC implements Runnable{ public void run() { lock.lock(); try { while(true){ if (cond % 3 == 0) { System.out.println("C"); cond++; condition.signalAll(); break; } else { try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } } }finally{ lock.unlock(); } } } }

你可能感兴趣的:(J2SE,J2EE)