zoukankan      html  css  js  c++  java
  • Java Threading Consumer&Producer

    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
     

  • 相关阅读:
    koa2 ctx.body 在 mysql query的回调函数中无法赋值的问题
    hibernate 实现多表连接查询
    Struts2 的国际化实现
    struts2 dwr There is no Action mapped for action ... 原因及解决方案
    Hibernate4.1配置数据库连接池 org.hibernate.service.jndi.JndiException:Unable to lookup JNDI name java:comp/env...
    Android 使用JSON格式与服务器交互 中文乱码问题解决
    Struts2 访问 Servlet API 的三种方法
    Struts2 输入校验
    hibernate4 和 spring3 整合注意事项 否则java.lang.NoSuchMethodError异常
    MySQL密码忘记的解决方案
  • 原文地址:https://www.cnblogs.com/hcfalan/p/1965945.html
Copyright © 2011-2022 走看看