乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java中的wait和notify總結和應用

       Levy_X 2017-07-25
      package concurrency; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ProducerAndConsumer { /** * 使用wait notify 的多生產者多消費者,固定消費空間的例子 * 消費者 生產者的 執(zhí)行速度不一 */ private List<Integer> data = new LinkedList<>(); private static final int MAX_DATA_LEN = 10; class Producer implements Runnable { private int pid = 0; public Producer(int pid){ this.pid = pid; } public void run() { try { while (!Thread.currentThread().isInterrupted()) { synchronized (data) { //try { while (data.size() >= MAX_DATA_LEN) { System.out.println('Producer' pid ' waiting ! size : ' data.size()); data.wait(); } data.add(pid); System.out.println('Producer' pid ' add ' pid ' size: ' data.size()); data.notifyAll(); } Thread.sleep(50); } } catch (InterruptedException ie) { ie.printStackTrace(); } } } class Consumer implements Runnable{ private int cid = 0; public Consumer(int cid){ this.cid = cid; } public void run(){ try { while (!Thread.currentThread().isInterrupted()) { synchronized (data) { while (data.isEmpty()) { System.out.println('Consumer' cid ' waiting, data size : ' data.size()); data.wait(); } int pid = data.remove(0); System.out.println('Consumer' cid ' consuming data ' pid ' data size : ' data.size()); data.notifyAll(); } Thread.sleep(500); } }catch (InterruptedException ie){ ie.printStackTrace(); } } } public void start(){ ExecutorService executor = Executors.newCachedThreadPool(); for(int i = 0; i < 5; i){ executor.submit(new Producer(i)); executor.submit(new Consumer(i)); } try { Thread.sleep(10*1000); } catch (InterruptedException e) { e.printStackTrace(); } executor.shutdownNow(); } public static void main(String []args){ new ProducerAndConsumer().start(); } }

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多