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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值