• 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
     

  • 相关阅读:
    Android ListView嵌套Button,Button事件覆盖item事件解决办法
    android 再按一次退出程序(实现代码)
    Android 带checkbox的listView 实现多选,全选,反选
    Android调用第三方应用
    Android输入法界面管理(打开/关闭/状态获取)
    ViewPager的使用方法和实现过程
    安装pycharm 2018.3 Professional Edition
    layui和jquery冲突:Syntax error, unrecognized expression: +
    解决因为本地代码和远程代码冲突,导致git pull无法拉取远程代码的问题(转载)
    Object.assign()
  • 原文地址:https://www.cnblogs.com/hcfalan/p/1965945.html
Copyright © 2020-2023  润新知