高天乐 welcome you

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

线程在运行过程中需要停下来等待,然后再继续执行。

范例程序,一个线程(ClientThread)对另外一个线程(ServerThread)传递请求,实现一个模拟消息通道的功能。

public class GuardedSuspensionTest {
	public static void main(String[] args) {
		RequestQueue queue = new RequestQueue();
		ClientThread ct = new ClientThread(queue, "Alice's Client");
		ServerThread st = new ServerThread(queue, "Bobby's Server");
		
		ct.start();
		st.start();
	}
}

class ServerThread extends Thread{
	private RequestQueue requestQueue;
	
	public ServerThread(RequestQueue requestQueue, String name){
		super(name);
		
		this.requestQueue=requestQueue;
	}

	@Override
	public void run() {
		for(int i=0;i <10;i++){
			Request request=requestQueue.getRequest();
			System.out.println(Thread.currentThread().getName() + " handles " + request);
			Thread.yield();
		}
	}
}

class ClientThread extends Thread{
	private RequestQueue requestQueue;
	
	public ClientThread(RequestQueue requestQueue, String name){
		super(name);
		
		this.requestQueue=requestQueue;
	}

	@Override
	public void run() {
		for(int i=0; i<10;i++){
			Request request = new Request("No." + i);
			System.out.println(Thread.currentThread().getName() + " requests " + request);
			requestQueue.putRequest(request);
			
			Thread.yield();
		}
	}
}

class RequestQueue{

	private final LinkedList<Request> queue = new LinkedList<Request>();

	public synchronized Request getRequest(){
		while(queue.size()<=0){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		return queue.removeFirst();
	}
	
	public synchronized void putRequest(Request request){
		queue.addLast(request);
		notifyAll();
	}
}

class Request{
	private final String name;
	public Request(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}

	@Override
	public String toString() {
		return "[Request name:" + name + "]";
	}
}

  

posted on 2013-09-07 19:30  高天乐  阅读(694)  评论(0编辑  收藏  举报