惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - daniel-shen

精通EJB-概述 硬件基础知识大全 教程:关于XMLBeans的初步 在客户端进行xslt对xml的xuan渲染 jaxp笔记2007-4-17 6.ZK用户接口标记语言 5.事件的监听和处理 - daniel-shen - 博客园 4.组件的生命周期 3.zk体系结构 "tomcat启动分析"文章读后笔记 jdk5.0 范型说明 java 数组反射例子 HTML小技巧 MessageFormat初步 ResourceBundle 与locale的使用 jni中关于dll的装载问题 HttpSessionBindingListener实现与应用 用Java Servlets 2.4实现过滤 加和不加java:comp/env/前缀有什么区别?
java线程-消费者vs生产者
daniel-shen · 2006-11-20 · via 博客园 - daniel-shen

在这里我们用多线程中最典型的例子,生产者与消费者问题。在这个例子里面我们定义了生产者Producer,消费者Consumer和仓库Warehouse三个类,在整个程序的生命周期里,生产者随机地制造出产品放到仓库中,消费者也是随即地从仓库中取出产品。

import exception.ProducerConsumerException;

/**

* Consumer.java

* Consumer

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:47:58 PM

*/

public class Consumer extends Thread {

private Warehouse warehouse;

private String id;

public Consumer(Warehouse warehouse, String id) {

this.warehouse = warehouse;

this.id = id;

}

public void run() {

int tmp = (int) Math.random() * 10;

try {

warehouse.get(tmp);

System.out.println("Consumer # " + this.id + " get " + tmp);

} catch (ProducerConsumerException e) {

e.printStackTrace();

}

try {

sleep((int) (Math.random() * 100));

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

在这个类中,值得注意的一点是run方法中必须使用try-catch,因为,消费者从仓库中取东西时有可能诸如仓库中的储量不够得异常,在消费者里面也是一样,只不过异常变为仓库已满。

import exception.*;

/**

* Producer.java

* Producer

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:47:45 PM

*/

public class Producer extends Thread {

private Warehouse warehouse;

private String id;

public Producer(Warehouse warehouse, String id) {

this.warehouse = warehouse;

this.id = id;

}

public void run() {

int tmp = (int) Math.random() * 10;

if (tmp != 0) {

try {

warehouse.put(tmp);

System.out.println("Consumer # " + this.id + " put " + tmp);

} catch (ProducerConsumerException e) {

e.printStackTrace();

}

}

try {

sleep((int) (Math.random() * 100));

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

最重要的一部分在Warehouse类,如上所说为了保证get何set的原子性,在这里使用了synchronized关键字,并且在操作时抛出了可能跑出的异常。

import exception.*;

/**

* Warehouse

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:48:10 PM

*/

public class Warehouse {

// max capability of the warehouse

private int MAX;

private int contents;

// init with max capacity

public Warehouse(int max) {

this.MAX = max;

this.contents = 0;

}

public synchronized void get(int amount) throws ProducerConsumerException {

// the amount you want to get is bigger than the contends that the warehouse stores

if (amount > this.contents) {

throw new NotEnoughGoodsException();

}

amount -= contents;

}

public synchronized void put(int amount) throws ProducerConsumerException {

// the amount you want to put is out of the capability of the warehouse

if (amount > (this.MAX - this.contents)) {

throw new WarehouseFullException();

} else if (this.contents == 0) {

// warehouse is empty

throw new WarehouseEmptyException();

}

amount += contents;

}

}