题目: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++;
			}
		}
	}
}

 

posted on 2013-10-05 15:39  真心的不懂  阅读(1641)  评论(0编辑  收藏  举报