锘??xml version="1.0" encoding="utf-8" standalone="yes"?>黄色一区二区在线,污视频在线观看网站,视频一区在线免费观看http://www.aygfsteel.com/dericgit/archive/2012/12/04/392394.html涓紓曚竴紕?/dc:creator>涓紓曚竴紕?/author>Tue, 04 Dec 2012 02:06:00 GMThttp://www.aygfsteel.com/dericgit/archive/2012/12/04/392394.htmlhttp://www.aygfsteel.com/dericgit/comments/392394.htmlhttp://www.aygfsteel.com/dericgit/archive/2012/12/04/392394.html#Feedback0http://www.aygfsteel.com/dericgit/comments/commentRss/392394.htmlhttp://www.aygfsteel.com/dericgit/services/trackbacks/392394.htmlJava浠g爜  :
import java.util.ArrayList;  
import java.util.List;  
/** 
 * 閫氳繃wait鍜宯otify瀹炵幇鐨勭敓浜ц呮秷璐硅卍emo 
 * User: zhangb 
 * Date: 12-12-1 
 * Time: 涓嬪崍7:19 
 */  
public class ProducerAndCustomerDemo {  
    private static int capacity = 150;  
    private static List<String> basket = new ArrayList<String>(capacity);  
  
    public static void main(String[] args) {  
        // 澶氱敓浜ц呬笌澶氭秷璐硅? 
        int producerSize = 2;  
        Thread[] ps = new Thread[producerSize];  
        for (int i = 0, step = 500; i < producerSize; i ++) {  
            ps[i] = new Thread(new Producer((i) * step, (i+1) * step), "鐢熶駭-->綰跨▼--" + (i+1));  
            ps[i].start();  
        }  
  
        int customerSize = 10;  
        Thread[] cs = new Thread[customerSize];  
        for (int i = 0; i < customerSize; i ++) {  
            cs[i] = new Thread(new Customer(), "娑堣垂綰跨▼--" + (i+1));  
            cs[i].start();  
        }  
  
        // 絳夊緟鐢熶駭綰跨▼緇撴潫騫朵腑鏂秷璐圭嚎紼? 
        for (int i = 0; i < producerSize; i ++) {  
            try {  
                ps[i].join();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
        for (int i = 0; i < customerSize; i ++) {  
            cs[i].interrupt();  
        }  
    }  
    static class Producer implements Runnable {  
        private int start;  
        private int end;  
  
        Producer(int start, int end) {  
            this.start = start;  
            this.end = end;  
        }  
  
        @Override  
        public void run() {  
            for (int i = start; i < end; i ++) {  
                synchronized (basket) {  
                    try {  
                        while (basket.size() == capacity) {  
                            basket.wait();  
                        }  
                        String p = " PRO" + i;  
                        System.out.println(Thread.currentThread().getName() + p);  
                        basket.add(p);  
                        basket.notifyAll();  
                        Thread.yield(); // 璁╁嚭褰撳墠綰跨▼鐨勬墽琛屾潈,鏈夊埄浜庣湅鍑轟氦鏇跨嚎紼嬭繍琛岀殑鏁堟灉  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                        break;  
                    }  
                }  
            }  
        }  
    }  
    static class Customer implements Runnable {  
        @Override  
        public void run() {  
            while (true) {  
                synchronized (basket) {  
                    try{  
                        while (basket.size() == 0) {  
                            basket.wait();  
                        }  
                        System.out.println(Thread.currentThread().getName() + basket.remove(0));  
                        basket.notifyAll();  
                    } catch (InterruptedException e) {  
                        System.out.println(Thread.currentThread().getName() + "閫鍑?);  
                        break;  
                    }  
                }  
            }  
        }  
    }  
}  


]]>
主站蜘蛛池模板: 永城市| 清新县| 邳州市| 乌拉特中旗| 修武县| 新乐市| 自治县| 阿拉善左旗| 沽源县| 随州市| 巴里| 芜湖市| 筠连县| 施甸县| 小金县| 尖扎县| 宁武县| 枣庄市| 五常市| 太康县| 会泽县| 辰溪县| 许昌县| 南昌县| 台安县| 炎陵县| 兴文县| 宣恩县| 齐河县| 额尔古纳市| 十堰市| 芦溪县| 乌鲁木齐县| 天镇县| 哈巴河县| 金沙县| 台安县| 谷城县| 洛浦县| 卢湾区| 赤水市|