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

推荐订阅源

Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
T
Tailwind CSS Blog
D
Docker
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
腾讯CDC
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
U
Unit 42
The Cloudflare Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
爱范儿
爱范儿
Recent Announcements
Recent Announcements
博客园 - 聂微东
博客园 - 叶小钗
H
Help Net Security
MyScale Blog
MyScale 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