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

推荐订阅源

U
Unit 42
T
Threatpost
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
Project Zero
Project Zero
H
Heimdal Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
NISL@THU
NISL@THU
N
News and Events Feed by Topic
S
Security Affairs
The Last Watchdog
The Last Watchdog
T
Tor Project blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security

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