<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--線程的協(xié)作

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

    package book.thread;

    import java.util.Vector;

    /**
     * 線程間的協(xié)作
     
    */
    public class WaitNotify {
        
    /**
         * 打印信息的類,是一個(gè)線程。
         
    */
        
    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){
                                
    //如果任務(wù)列表為空,而且線程還允許運(yùn)行,則等待任務(wù)
                                System.out.println("wait begin!");
                                
    //該線程進(jìn)入等待狀態(tài),直到被其他線程喚醒
                                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();
                }
            }
            
    /**
             * 添加待打印的任務(wù)
             
    */
            
    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();
            
    //啟動(dòng)打印線程
            printer.start();
            
    //添加任務(wù)
            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();
        }
    }



                                                                                                           --    學(xué)海無(wú)涯
            

    主站蜘蛛池模板: 久久精品国产免费一区| 亚洲人成网站免费播放| 亚洲精品国产情侣av在线| 99在线精品视频观看免费| 久久精品国产亚洲AV未满十八| 亚洲欧洲久久av| 国产免费一区二区三区| 青青视频免费在线| 亚洲第一香蕉视频| 亚洲av成人一区二区三区在线观看| 免费国产成人α片| 豆国产96在线|亚洲| 久久亚洲国产成人精品性色| 在线免费视频一区| 无码国产精品一区二区免费模式| 亚洲精品永久在线观看| 久久久久亚洲AV片无码| 国产hs免费高清在线观看| 18禁黄网站禁片免费观看不卡| 亚洲AV无码国产精品永久一区| 亚洲天天做日日做天天看| 午夜亚洲福利在线老司机| 嫖丰满老熟妇AAAA片免费看| WWW免费视频在线观看播放| 亚洲精品无码成人| 亚洲国产成人超福利久久精品| 中文亚洲成a人片在线观看| 国产美女无遮挡免费视频网站| 91在线手机精品免费观看| 人与动性xxxxx免费| 亚洲日产乱码一二三区别| 91亚洲精品第一综合不卡播放| 精品国产亚洲男女在线线电影| 啦啦啦手机完整免费高清观看| 99在线观看免费视频| 韩国免费A级毛片久久| 污视频网站在线观看免费| 亚洲精品无码不卡在线播放| 亚洲日本香蕉视频| 久久久久亚洲av无码专区导航 | 亚洲AV永久无码天堂影院|