java|java并发编程(顺序输出A、B、C循环10次)

【java|java并发编程(顺序输出A、B、C循环10次)】要求:3个线程,分别输出A、B、C。循环10次,给出三种方法,code如下
一、wait、notify

public class PrintABC { static int state = 0; private static Object o = new Object(); public static void main(String[] args) { ExecutorService ser = Executors.newCachedThreadPool(); ser.submit(new ThreadA()); ser.submit(new ThreadB()); ser.submit(new ThreadC()); ser.shutdown(); } public static class ThreadA implements Runnable {@Override public void run() {for(int i=0; i<10; i++){ synchronized (o) { while(state%3!=0){ try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("A"); state++; o.notifyAll(); }} } } public static class ThreadB implements Runnable { @Override public void run() { for(int i=0; i<10; i++){ synchronized (o) { while(state%3!=1){ try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("B"); state++; o.notifyAll(); } } } } public static class ThreadC implements Runnable { @Override public void run() { for(int i=0; i<10; i++){ synchronized (o) { while(state%3!=2){ try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("C"); state++; o.notifyAll(); } } } } }

二、lock、unlock
public class PrintABC2 { static int state = 0; private static Lock lock = new ReentrantLock(); public static void main(String[] args) { ExecutorService ser = Executors.newCachedThreadPool(); ser.submit(new ThreadA()); ser.submit(new ThreadB()); ser.submit(new ThreadC()); ser.shutdown(); } public static class ThreadA implements Runnable {@Override public void run() {for(int i=0; i<10; ){ lock.lock(); if(state%3==0){ System.out.println("A"); state++; i++; } lock.unlock(); }} } public static class ThreadB implements Runnable { @Override public void run() { for(int i=0; i<10; ){ lock.lock(); if(state%3==1){ System.out.println("B"); state++; i++; } lock.unlock(); } } } public static class ThreadC implements Runnable { @Override public void run() { for(int i=0; i<10; ){ lock.lock(); if(state%3==2){ System.out.println("C"); state++; i++; } lock.unlock(); } } } }


三、信号量
public class PrintABC3 { static int state = 0; private static Semaphore A = new Semaphore(1); private static Semaphore B = new Semaphore(1); private static Semaphore C = new Semaphore(1); public static void main(String[] args) { ExecutorService ser = Executors.newCachedThreadPool(); try { B.acquire(); C.acquire(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ser.submit(new ThreadA()); ser.submit(new ThreadB()); ser.submit(new ThreadC()); ser.shutdown(); } static class ThreadA extends Thread {@Override public void run() { try { for (int i = 0; i < 10; i++) { A.acquire(); System.out.print("A"); sleep(2000); B.release(); } } catch (InterruptedException e) { e.printStackTrace(); } }}static class ThreadB extends Thread {@Override public void run() { try { for (int i = 0; i < 10; i++) { B.acquire(); System.out.print("B"); C.release(); } } catch (InterruptedException e) { e.printStackTrace(); } }}static class ThreadC extends Thread { @Override public void run() { try { for (int i = 0; i < 10; i++) { C.acquire(); System.out.print("C"); A.release(); } } catch (InterruptedException e) { e.printStackTrace(); } }}}


结果如下:
A B C A B C A B C A B C A B C A B C A B C A B C A B C A B C




    推荐阅读