题目:3个线程名字分别是A,B,C 现在在console上连续打印10次 ABC 。

public class Test {

	public static void main(String[] args) {
		Thread a = new Thread(new Task("A",0));
		Thread b = new Thread(new Task("B",1));
		Thread c = new Thread(new Task("C",2));
		a.start();
		b.start();
		c.start();
	}
	
	static class Task implements Runnable{
		
		private String one;
		private int count;
		private static int num = 0;
		
		public Task(String one,int count){
			this.one = one;
			this.count = count;
		}
		
		@Override
		public void run() {
			
			int i = 0;
			while(i < 10){
				synchronized(Task.class){
					if(num % 3 == count){
						num++;
						System.out.println(one);
					}else{
						continue;
					}
				}
				i++;
			}
		}
	}
}

 

相关文章:

  • 2022-02-25
  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-09-11
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
相关资源
相似解决方案