• 多线程的多消费者和多生产者signal和await


    package test;
    
    import java.awt.List;
    import java.awt.image.AreaAveragingScaleFilter;
    import java.lang.reflect.Array;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.*;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.io.BufferedInputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Reader;
    import java.io.Writer;
    import javax.security.auth.callback.LanguageCallback;
    import javax.swing.text.StyledEditorKit.BoldAction;
    
    class Resourse {
    	private String name;
    	private boolean flag = false;
    	private int count;
    	Lock lock = new ReentrantLock();
    	Condition proCondition = lock.newCondition();
    	Condition condition = lock.newCondition();
    
    	public void put(String name) {
    		lock.lock();
    		try {
    			while (flag) {
    				try {
    					proCondition.await();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    
    			flag = true;
    			++count;
    			this.name = name + count;
    			condition.signal();
    		} finally {
    			lock.unlock();
    		}
    	}
    
    	public void take() {
    		lock.lock();
    		try {
    			while (!flag) {
    				try {
    					condition.await();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    			}
    			flag = false;
    			System.out.println(Thread.currentThread().getName() + " ........" + this.name);
    			proCondition.signal();
    		} finally {
    			lock.unlock();
    		}
    	}
    }
    
    class Producer implements Runnable {
    	Resourse r;
    
    	Producer(Resourse r) {
    		this.r = r;
    	}
    
    	@Override
    	public void run() {
    		while (true) {
    			r.put("烤鸭");
    		}
    	}
    
    }
    
    class Consumer implements Runnable {
    	Resourse r;
    
    	Consumer(Resourse r) {
    		this.r = r;
    	}
    
    	@Override
    	public void run() {
    		while (true)
    			r.take();
    	}
    
    }
    
    public class Main {
    
    	public static void main(String[] args) throws Exception {
    
    		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    		
    		Resourse r = new Resourse();
    		Producer a = new Producer(r);
    		Producer b = new Producer(r);
    		Consumer c = new Consumer(r);
    		Consumer d = new Consumer(r);
    		
    		Thread t1 = new Thread(a);
    		Thread t2 = new Thread(b);
    		Thread t3 = new Thread(c);
    		Thread t4 = new Thread(d);
    		
    		t1.start();
    		t2.start();
    		t3.start();
    		t4.start();
    	}
    }
    

      

  • 相关阅读:
    使用Code First Migrations依据代码更新数据库结构
    Engine Yard增加对Node.js的支持
    CSS3无前缀脚本prefixfree.js及Animatable介绍
    html5客户端本地存储之sessionStorage及storage事件
    cctype,string,vector
    管理朋友信息程序
    三位数的排列组合
    结构体字节对齐
    习题3.13
    OPENCV用户手册之图像处理部分(之一):梯度、边缘与角点(中文翻译)
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788632.html
Copyright © 2020-2023  润新知