<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-95  評論-31  文章-10  trackbacks-0
     1public class MyStack {
     2    private int[] a;
     3    private int count = 0;
     4    public final int MAX_SIZE;
     5
     6    public MyStack(int size) {
     7        MAX_SIZE = size;
     8        a = new int[size];
     9        count = 0;
    10    }

    11
    12    public synchronized void push() {
    13         while(count==MAX_SIZE){//這里用while而不是if因?yàn)楫?dāng)被喚醒時(shí),該線程處于鎖池等待獲取鎖,這個(gè)時(shí)候可能會(huì)有別的該線改變數(shù)組的大小。所以喚醒時(shí)繼續(xù)檢查數(shù)組是否已滿。
    14             try {
    15                this.wait(); //釋放掉當(dāng)前的對象鎖,在等待池等待
    16            }
     catch (InterruptedException e) {
    17                e.printStackTrace();
    18            }

    19         }

    20         a[count++]=count;
    21         System.out.println(Thread.currentThread().getName()+"壓入數(shù)據(jù):"+count);
    22         this.notify();  //喚醒生產(chǎn)者消費(fèi)者線程
    23    }

    24
    25    public synchronized int pop() {
    26             while(count ==0){
    27                 try {
    28                    this.wait();
    29                }
     catch (InterruptedException e) {
    30                    e.printStackTrace();
    31                }

    32             }

    33             this.notify();
    34             System.out.println(Thread.currentThread().getName()+"彈出數(shù)據(jù):"+count);
    35             return a[--count];
    36     }

    37}

    38/**
    39 *生產(chǎn)者
    40 **/

    41public class Producer extends Thread {
    42
    43    private MyStack stack;
    44    
    45    public Producer(MyStack stack){
    46        this.stack = stack;
    47    }

    48    @Override
    49    public void run() {
    50        while (true{
    51            stack.push();
    52            try {
    53                Thread.sleep(200);
    54            }
     catch (InterruptedException e) {
    55                e.printStackTrace();
    56            }

    57        }

    58    }

    59}

    60/**
    61 *消費(fèi)者
    62 **/

    63public class Consumer extends Thread{
    64             
    65    private MyStack stack;
    66    
    67    public Consumer(MyStack stack){
    68        this.stack = stack;
    69    }

    70    
    71    @Override
    72    public void run() {
    73        while(true){
    74            stack.pop();
    75            try {
    76                Thread.sleep(300);
    77            }
     catch (InterruptedException e) {
    78                e.printStackTrace();
    79            }

    80        }

    81    }

    82}

    線程狀態(tài)圖
    posted on 2010-08-19 10:19 朔望魔刃 閱讀(234) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 最近最好最新2019中文字幕免费| 亚洲精品无码高潮喷水在线| 日韩免费精品视频| 黄色网址免费大全| 91在线视频免费91| 伊人久久综在合线亚洲91| 亚洲午夜久久久精品电影院| 一级毛片**免费看试看20分钟 | 国产成人精品亚洲精品| 亚洲精品在线播放| 午夜在线免费视频| 精品国产无限资源免费观看| 伊人久久大香线蕉亚洲| 亚洲最大中文字幕无码网站 | 两个人看的www免费视频| 日韩a级无码免费视频| 好爽又高潮了毛片免费下载| 丁香花在线观看免费观看| 中文亚洲AV片不卡在线观看| 亚洲人成色在线观看| 黄色毛片免费观看| 91成年人免费视频| 国产亚洲综合网曝门系列| 豆国产96在线|亚洲| 巨波霸乳在线永久免费视频| 国产AV无码专区亚洲AV手机麻豆| 亚洲国产区男人本色| 222www免费视频| 亚洲精品无码MV在线观看 | 美女内射无套日韩免费播放| 999久久久免费精品国产| 亚洲色大成网站WWW久久九九| 亚洲AV无码精品国产成人| 免费专区丝袜脚调教视频| 国产A在亚洲线播放| 亚洲人和日本人jizz| 久久国产乱子伦精品免费强 | 在线观看免费人成视频| 亚洲AV日韩AV高潮无码专区| 一级一黄在线观看视频免费| 日韩午夜免费视频|