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

推荐订阅源

博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
T
Tailwind CSS Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
罗磊的独立博客
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
量子位
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
美团技术团队

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