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  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.解决信号量丢失和假唤醒 public class MyWaitNotify3{ MonitorObject m...
    Q罗阅读 906评论 0 1
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,754评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,356评论 11 349
  • 本文出自 Eddy Wiki ,转载请注明出处:http://eddy.wiki/interview-java.h...
    eddy_wiki阅读 2,232评论 0 14
  • 学习内容:结构思考力视频第二章第一节识别概括 只有观点才能成为结论 观点分为有事实支撑的观点和没有事实支撑的观点,...
    Rasmallin阅读 144评论 0 0