博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Guarded Suspension
阅读量:7050 次
发布时间:2019-06-28

本文共 2151 字,大约阅读时间需要 7 分钟。

  hot3.png

                                                                        Guarded Suspension

    当线程需要一些资源准备的时候才可以运行的时候就最好使用Guarded Suspension,对使用资源的部分使用保卫空间,不满足就wait(),对补充资源的地方使用notifyAll/notify 两种通知等待线程。这个时候其实也发现了,使用资源的线程和补充资源的线程不应该是同一个,不然就会永远等待了。

    public class Main {	public static void main(String args[]){		RequestQueue requestQueue=new RequestQueue();		new ClientThread(requestQueue, "chen", 10000).start();		new ServerThread(requestQueue, "cdk", 3000).start();	}    }        //使用添加资源的线程的地方    public class ClientThread extends Thread {	private Random random;		private RequestQueue requestQueue;	public ClientThread(RequestQueue requestQueue,String name,long seed) {		super(name);		this.requestQueue=requestQueue;		this.random=new Random(seed);	}		public void run(){		for(int i=0;i<1000;i++){			Request request=new Request("no."+i);			System.out.println(Thread.currentThread().getName()+" request "+request);			requestQueue.putRequest(request);			try{				Thread.sleep(1000);			}catch(InterruptedException e){			}		}	}    }    //使用资源线程    public class ServerThread  extends Thread{	private Random random;		private RequestQueue requestQueue;	public ServerThread(RequestQueue requestQueue,String name,long seed) {		super(name);		this.requestQueue=requestQueue;		random=new Random(seed);	}		public void run(){		for(int i=0;i<1000;i++){			Request request=requestQueue.getRequest();			System.out.println(Thread.currentThread().getName()+" handler "+request);			try{				Thread.sleep(3000);			}catch(InterruptedException e){			}		}	}    }        public class Request {	private final String name;		public Request(String name) {		this.name=name;	}		public String getName(){		return name;	}		public String toString(){		return "[request "+ name+" ]";	}    }           //资源缓存区,同时也是定义线程行为的地方    public class RequestQueue {		private final LinkedList queue = new LinkedList();	public  synchronized Request getRequest() {		while (queue.size() <= 0) {			try {				wait();			} catch (InterruptedException e) {			}		}		return (Request) queue.removeFirst();	}		public synchronized void putRequest(Request request){		queue.add(request);		notifyAll();	}}

    这种模式的情况就是,线程获取数据需要一些条件的情况下,而且既要有等待线程,也要有唤醒线程。

转载于:https://my.oschina.net/QAAQ/blog/662404

你可能感兴趣的文章
three.js模型
查看>>
网络流24题 餐巾计划问题
查看>>
基于 Android NDK 的学习之旅-----序言
查看>>
InnoDB recovery过程解析
查看>>
鼓浪屿
查看>>
alloc_skb申请函数分析
查看>>
WPF PRISM开发入门二(Unity依赖注入容器使用)
查看>>
使用 data-* 属性来嵌入自定义数据:
查看>>
炒股的常见技术指标
查看>>
工控随笔_07_西门子_WinCC利用命令行实现操作log日志
查看>>
解决MySQL报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ........
查看>>
(筆記) 如何設計邊緣檢測電路? (SOC) (Verilog)
查看>>
MPEG文件中什么是GOP
查看>>
C#查找指定窗口的子窗口的句柄
查看>>
Linux man命令的使用方法
查看>>
在delphi中嵌入脚本语言--(译)RemObjects Pascal Script使用说明(1)(译)
查看>>
Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
查看>>
Azure VMSS (2) 对VM执行Generalize操作
查看>>
C# 4.0四大新特性代码示例与解读
查看>>
HUST 1017 Exact cover
查看>>