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

推荐订阅源

N
News | PayPal Newsroom
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
S
SegmentFault 最新的问题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Project Zero
Project Zero
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
Recorded Future
Recorded Future
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
Schneier on Security
Schneier on Security
H
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
M
MIT News - Artificial intelligence
W
WeLiveSecurity
P
Proofpoint News Feed
A
About on SuperTechFans
S
Securelist
I
InfoQ
G
Google Developers Blog
博客园 - 司徒正美
博客园 - 叶小钗
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
腾讯CDC
Jina AI
Jina AI
S
Schneier on Security
I
Intezer
V
Visual Studio Blog
美团技术团队
V2EX - 技术
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
罗磊的独立博客
Y
Y Combinator Blog

博客园 - 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