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

推荐订阅源

博客园_首页
I
InfoQ
The Register - Security
The Register - Security
L
LangChain Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
Schneier on Security
博客园 - 【当耐特】
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
H
Heimdal Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
雷峰网
雷峰网
N
Netflix TechBlog - Medium
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - 聂微东
PCI Perspectives
PCI Perspectives
Last Week in AI
Last Week in AI
A
Arctic Wolf
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
SecWiki News
SecWiki News
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AI
AI
Engineering at Meta
Engineering at Meta

博客园 - hcfalan

Mybatis分页功能 安聊服务端Netty的应用 安聊系统1.0发布 VC中发送电子邮件 自绘窗口边框和标题栏 Windows下生产者-消费者问题的解法 Javascript 时间日期转换 Boost socket 同步编程示例(服务端,客户端) MFC CSocket的跨线程问题 用于主题检测的临时日志(54b8134e-5e4a-46fc-95af-c75ccf06d66e - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 网络电话 VC 剪贴板操作 The 1st Boost Serial Demo - hcfalan MS Proxy FTP Sample 《斯坦福大学开放课程: 编程方法学》 VC2005下编译log4cpp的修正方法 - hcfalan - 博客园 适合孩子的书籍 一个最经典的线程类(转) 应用MFC开发高级应用程序(转)
Java Threading - Consumer&Producer
hcfalan · 2011-02-26 · via 博客园 - hcfalan

import java.util.Vector;class Test {private Vector<String> mCache = new Vector<String>();static class Consumer implements Runnable {
        
private Test mTestRef;
        Consumer(Test t) {
            mTestRef 
= t;
        }
public void run() {
            
try {
                
for (; ;) {
                    
synchronized (mTestRef) {
                        
if (mTestRef.mCache.size() > 0) {
                            String item 
= mTestRef.mCache.get(0);
                            mTestRef.mCache.remove(
0);
                            System.out.println(
"Consume the item: " + item);
                        } 
else {
                            mTestRef.wait();
                        }
                    }
                }
            }
            
catch (InterruptedException ex) {
            }
        }
    }
static class Producer implements Runnable {
        
private Test mTestRef;
        
private String mProducerId;
        Producer(String id, Test t) {
            mProducerId 
= id;
            mTestRef 
= t;
        }
public void run() {
            
try {
                
int i = 0;
                
while ((++i)<5) {
                    
synchronized (mTestRef) {
                        String item 
= mProducerId + " #" + i;
                        mTestRef.mCache.add(item);
                        mTestRef.notify();
                    }
                    Thread.sleep(
1000);
                }
            }
            
catch (InterruptedException ex) {
            }
        }
    }
public static void main(String[] args) {
        Test test 
= new Test();
        Thread consumer 
= new Thread(new Consumer(test));
        Thread producer1 
= new Thread(new Producer("p1", test));
        Thread producer2 
= new Thread(new Producer("p2", test));

        producer1.start();
        producer2.start();
        consumer.start();

try {
            Thread.sleep(
15000);
            producer1.interrupt();
            producer2.interrupt();
            consumer.interrupt();

            producer1.join();
            producer2.join();
            consumer.join();
        }

catch (Exception e) {
            System.out.println(
"exception: " + e.getMessage());        
        }
    }
            
}
 

output is:

Consume the item: p1 #1
Consume the item: p2 #1
Consume the item: p1 #2
Consume the item: p2 #2
Consume the item: p1 #3
Consume the item: p2 #3
Consume the item: p1 #4
Consume the item: p2 #4