一道面试题:三个线程循环有序输出10次ABCABC...

面试遇到的一道面试题:三个线程循环有序输出10次ABCABC...

解决思路:使用synchronized,wait和notifyAll控制线程同步


package com.myj.test.thread;
/**
 * 三个线程循环打印ABC10次
 * 
 * 通过给实现Runnable接口的类添加一个私有变量,该变量就是每一个运行该任务线程的标识,
 * 只有当线程的标识与Letter的私有变量一致才会运行输出,否则将阻塞,
 * 直到其他线程将其唤醒,并在阻塞的位置继续执行,即再判断标识和letter的值
 * 
 * letter的值也是需要改变的,每次线程输出完之后,要将该值更改为下一个
 * 
 *
 */
public class ABC {
	public static void main(String[] args) {
		
		ABC t = new ABC();
		Letter l = t.new Letter();
		new Thread(t.new Mission(l, 'A')).start();
		new Thread(t.new Mission(l, 'B')).start();
		new Thread(t.new Mission(l, 'C')).start();
		
	}
	
	
	class Letter{
		private char letter = 'A';
		
		public void show(){
			System.out.println(letter);
			
		}
		
		public void nextLetter(char letter){
			switch(letter){
			case 'A':
				this.letter = 'B';
				break;
			case 'B':
				this.letter = 'C';
				break;
			case 'C':
				this.letter = 'A';
				break;
			}
		}
		
		public char getLetter(){
			return this.letter;
		}
		
		
	}
	
	
	
	class Mission implements Runnable{

		private Letter letter;
		//线程的标识
		private char let;
		
		public Mission(Letter letter, char let){
			this.letter = letter;
			this.let = let;
		}
		
		@Override
		public void run() {
			
			for(int i=0;i<10;i++){
				
				synchronized(letter){
					//运行的线程标识如果和letter的值不一样就先使该线程阻塞
					//直到运行的线程标识和letter值一致
					while(let!=letter.getLetter()){
						try {
							//进入阻塞,直到其他线程调用notifyAll唤醒,
							//在此基础上继续运行,继续判断是否该轮到该线程输出
							letter.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					letter.show();
					letter.nextLetter(let);
					letter.notifyAll();
				}
				
				
			}
			
			
		}
		
	}
	
	
}


可以使用Java中的Semaphore信号量实现,并且基于线程间协作的方式来解决: ```java import java.util.concurrent.Semaphore; class PrintThread extends Thread { private Semaphore currentSemaphore; private Semaphore nextSemaphore; private String message; private int repeatTimes; public PrintThread(Semaphore currentSemaphore, Semaphore nextSemaphore, String message, int repeatTimes) { this.currentSemaphore = currentSemaphore; this.nextSemaphore = nextSemaphore; this.message = message; this.repeatTimes = repeatTimes; } public void run() { try { for (int i = 0; i < repeatTimes; i++) { currentSemaphore.acquire(); System.out.print(message); nextSemaphore.release(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { Semaphore semA = new Semaphore(1); Semaphore semB = new Semaphore(0); Semaphore semC = new Semaphore(0); int repeatTimes = 10; PrintThread threadA = new PrintThread(semA, semB, "A", repeatTimes); PrintThread threadB = new PrintThread(semB, semC, "B", repeatTimes); PrintThread threadC = new PrintThread(semC, semA, "C", repeatTimes); threadA.start(); threadB.start(); threadC.start(); } } ``` 上述代码中,我们使用三个Semaphore对象控制三个线程的执行顺序。具体来讲,我们将Semaphore semA、Semaphore semB和Semaphore semC初始化分别为1、0和0,因为我们希望线程A最先执行。然后,我们创建三个PrintThread对象,并分别将Semaphore对象作为参数传入,同时设置循环数为10。 在PrintThread的run()方法中,我们使用Semaphore对象实现线程间的等待和释放。首先,currentSemaphore.acquire()会在当前Semaphore的值减1后继续执行,因为我们将Semaphore semA初始化为1,所以线程A会直接执行。然后,线程打印出自己的输出(即message),并调用nextSemaphore.release()方法,将下一个Semaphore的值加1,这里即将Semaphore semB的值加1。接着线程继续执行循环,等待当前Semaphore的值变为1后再执行,直到达到循环数就结束。 通过上述方法,我们能够实现三个线程有序输出,且可以重复执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值