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

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

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

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    線程4--線程的協作

    Posted on 2009-06-02 20:19 啥都寫點 閱讀(187) 評論(0)  編輯  收藏 所屬分類: J2SE
    關鍵技術:
    • 在synchronized代碼塊中使用wait方法,能夠使當前線程進入等待狀態,并釋放當前線程擁有的對象鎖。
    • 在synchronized代碼塊中使用notify或者notifyAll方法,當前線程釋放對象鎖,并喚醒其他正在等待該對象鎖的線程。當有多個線程都在等待該對象鎖時,由Java虛擬機決定被喚醒的等待線程。

    package book.thread;

    import java.util.Vector;

    /**
     * 線程間的協作
     
    */
    public class WaitNotify {
        
    /**
         * 打印信息的類,是一個線程。
         
    */
        
    static class Printer extends Thread{
            Vector task 
    = new Vector();
            
    boolean running = false;
            
    public void start(){
                
    this.running = true;
                
    super.start();
            }
            
    public void run(){
                
    try {
                    System.out.println(
    "Printer begin!");
                    
    while (running){
                        
    synchronized(this) {
                            
    while ((task.size() == 0&& running){
                                
    //如果任務列表為空,而且線程還允許運行,則等待任務
                                System.out.println("wait begin!");
                                
    //該線程進入等待狀態,直到被其他線程喚醒
                                wait();
                                System.out.println(
    "wait end!");
                            }
                        }
                        
    if (running){
                            System.out.println(
    "print the task: " + task.remove(0));
                        }
                    }
                    System.out.println(
    "Printer end!");
                } 
    catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            
    /**
             * 添加待打印的任務
             
    */
            
    public void addTask(String str){
                
                
    synchronized (this){
                    
    this.task.add(str);
                    
    //喚醒其他等待的線程
                    System.out.println("addTask notify!");
                    notify();
                    
    //notifyAll();
                }
            }
            
    /**
             * 停止線程
             
    */
            
    public void stopPrinter(){
                
    this.running = false;
                
    synchronized (this){
                    
    //喚醒其他等待的線程
                    System.out.println("stopPrinter notify!");
                    notify();
                }
            }
        }

        
    public static void main(String[] args) {
            Printer printer 
    = new Printer();
            
    //啟動打印線程
            printer.start();
            
    //添加任務
            try {
                Thread.sleep(
    200);
                
    for (int i=0; i<5; i++){
                    
    //休眠200毫秒
                    Thread.sleep(200);
                    printer.addTask(
    "The task " + i);
                }
            }
            
    catch (InterruptedException e) {
                e.printStackTrace();
            }
            printer.stopPrinter();
        }
    }



                                                                                                           --    學海無涯
            

    主站蜘蛛池模板: 99免费在线观看视频| 三年在线观看免费观看完整版中文| 一区二区免费视频| 亚洲精品国产字幕久久不卡| 一日本道a高清免费播放| 亚洲黄片手机免费观看| 一级毛片aaaaaa视频免费看| 亚洲国产成人久久一区WWW| 免费福利在线观看| 久久精品国产亚洲Aⅴ蜜臀色欲 | 亚洲一区精品伊人久久伊人| 色噜噜噜噜亚洲第一| 四虎成人精品在永久免费| 免费看一级高潮毛片| 亚洲中文久久精品无码ww16| 色www永久免费网站| 亚洲一区二区影院| 很黄很色很刺激的视频免费| 亚洲人成色77777在线观看| 国产a级特黄的片子视频免费| 免费人成网上在线观看| 亚洲成A人片777777| 18女人毛片水真多免费| 亚洲色偷偷色噜噜狠狠99| 国产一级淫片视频免费看| 国产特黄特色的大片观看免费视频| 亚洲香蕉成人AV网站在线观看| 无码人妻一区二区三区免费n鬼沢| 亚洲噜噜噜噜噜影院在线播放| 成年女人免费视频播放体验区| 暖暖免费中文在线日本| 亚洲AV综合色区无码一区| 国产福利在线免费| www.av在线免费观看| 91亚洲一区二区在线观看不卡| 超pen个人视频国产免费观看| 一本一道dvd在线观看免费视频 | 亚洲香蕉久久一区二区| 亚洲av无码不卡私人影院| 99视频在线看观免费| 欧美日韩亚洲精品|