论坛首页 Java企业应用论坛

三个线程循环打印abc十次

浏览 19813 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2012-01-05  
public class Test {

	public static void main(String[] args) {
		Test t = new Test();
		t.print();
	}

	public void print() {
		Object[] locks = new Object[] { new Object(), new Object(), new Object() };

		new PrintThread("a", locks[0], locks[1]).start();
		new PrintThread("b", locks[1], locks[2]).start();
		new PrintThread("c", locks[2], locks[0]).start();

		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		synchronized (locks[0]) {
			locks[0].notify();
		}
	}

	public static class PrintThread extends Thread {
		private String printText;
		private Object currThreadLock;
		private Object nextThreadLock;

		public PrintThread(String printText, Object currThreadLock, Object nextThreadLock) {
			this.printText = printText;
			this.currThreadLock = currThreadLock;
			this.nextThreadLock = nextThreadLock;
		}

		@Override
		public void run() {
			try {
				while (true) {
					synchronized (currThreadLock) {
						currThreadLock.wait();
					}
					System.out.println(printText);
					Thread.sleep(500);
					synchronized (nextThreadLock) {
						nextThreadLock.notify();
					}
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
0 请登录后投票
   发表时间:2012-03-01  
用线程的setPriority方法,设置 3 个级别
0 请登录后投票
   发表时间:2012-03-02  
my_corner 写道
aiheng1988 写道
华为的2011面试题a卷就有这个,我去过!!!

你答的好吗

没有啊,当时没有太能理解好题目的意思,我直接挂了三个线程,没有考虑线程同步的问题。。。惭愧
0 请登录后投票
   发表时间:2012-03-09  
i2534 写道
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final AtomicInteger ai = new AtomicInteger(0);

		Thread a = new Thread("a") {

			private int i = 0;

			@Override
			public void run() {
				while (true) {
					if (ai.get() == 1) {
						System.out.println("a");
						ai.set(2);
						if (i++ == 10) {
							break;
						}
					}
				}
			}
		};
		a.start();

		Thread b = new Thread("b") {
			private int i = 0;

			@Override
			public void run() {
				while (true) {
					if (ai.get() == 2) {
						System.out.println("b");
						ai.set(3);
						if (i++ == 10) {
							break;
						}
					}
				}
			}
		};
		b.start();

		Thread c = new Thread("c") {
			private int i = 0;

			@Override
			public void run() {
				while (true) {
					if (ai.get() == 3) {
						System.out.println("c");
						ai.set(1);
						if (i++ == 10) {
							break;
						}
					}
				}
			}
		};
		c.start();

		ai.set(1);

	}


这样应该可以吧

感觉不大对,和我这个思路差不多,但是像您这样写的话就不一定谁能抢到锁了
package online.test2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
 * 
 * @author 苹果天堂
 *
 */
public class PrintThread extends Thread {
	private static char current = 'A';
	private static boolean finish = false;
	private static int count = 0;
	private char name;
	private ReadWriteLock lock = new ReentrantReadWriteLock();
	private Lock write = lock.writeLock();
	public PrintThread(char name) {
		this.name = name;
	}
	public void run() {
		super.run();
		try {
			while(!finish) {
				if(name == current) {
				write.lock();
				print();
				write.unlock();
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void print() {
		System.out.print(name);
		switch(current) {
		case 'A':
			current = 'B';
			break;
		case 'B':
			current = 'C';
			break;
		case 'C':
			current = 'A';
			count ++;
			if(count == 10) {
				finish = true;
				current =' ';
			}
			break;
		}
	}
	public static void main(String args[]) {
		Thread thA = new PrintThread('A');
		Thread thB = new PrintThread('B');
		Thread thC = new PrintThread('C');
		thA.start();
		thB.start();
		thC.start();
	}
}


0 请登录后投票
   发表时间:2012-03-13  
试了一下:

import java.util.ArrayList;

public class ThreadPrint extends Thread {
	
	private ArrayList<String> list;
	
	public ThreadPrint(String name, ArrayList<String> list) {
		super(name);
		this.list = list;
	}
	
	@Override
	public void run() {
		synchronized (list) {
			while (true) {
				String name = Thread.currentThread().getName();
				String header = list.get(0);
				if (!header.equals(name)) {
					try {
						list.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					System.out.print(name);
					list.remove(0);
					list.add(header);
					list.notifyAll();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		ArrayList<String> ll = new ArrayList<String>();
		ll.add("A");
		ll.add("B");
		ll.add("C");
		ThreadPrint pA = new ThreadPrint("A", ll);
		ThreadPrint pB = new ThreadPrint("B", ll);
		ThreadPrint pC = new ThreadPrint("C", ll);
		pA.start();
		pB.start();
		pC.start();
	}

}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics