少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

           

          問題解決2——加入等待與喚醒


          package edu.sjtu.erplab.thread;

          class Info{
              private String name="name";
              private String content="content";
              private boolean flag=true;
              public  synchronized void set(String name,String content)
              {
                  if(!flag)//標志位為false,不可以生產
                  {
                      try {
                          super.wait();
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                  this.setName(name);
                  try {
                      Thread.sleep(30);
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  this.setContent(content);
                  flag=false;//修改標志位為false,表示生產者已經完成資源,消費者可以消費。
                  super.notify();//喚醒消費者進程
              }
             
              public synchronized void get()
              {
                  if(flag)
                  {
                      try {
                          super.wait();
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                  try {
                      Thread.sleep(30);
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  System.out.println(this.getName()+":-->"+this.getContent());
                  flag=true;//修改標志位為true,表示消費者拿走資源,生產者可以生產。
                  super.notify();//喚醒生產者進程。
              }
             
             
              public String getName() {
                  return name;
              }
              public void setName(String name) {
                  this.name = name;
              }
              public String getContent() {
                  return content;
              }
              public void setContent(String content) {
                  this.content = content;
              }
             
          }

          class Producer implements Runnable{
              private Info info=null;
              public Producer(Info info)
              {
                  this.info=info;
              }
             

              @Override
              public void run() {
                  boolean flag=false;
                  for(int i=0;i<10;i++)
                      if(flag)
                      {
                          this.info.set("name+"+i, "content+"+i);
                          flag=false;
                      }
                      else
                      {
                          this.info.set("name-"+i, "content-"+i);
                          flag=true;
                      }
              }
          }

          class Consumer implements Runnable{
              private Info info=null;
              public Consumer(Info info)
              {
                  this.info=info;
              }
              @Override
              public void run() {
                  for(int i=0;i<10;i++)
                  {
                      try {
                          Thread.sleep(10);
                      } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      this.info.get();
                  }
                 
              }
          }

          public class ThreadDeadLock {
              public static void main(String args[])
              {
                  Info info=new Info();
                  Producer pro=new Producer(info);
                  Consumer con=new Consumer(info);
                  new Thread(pro).start();
                  new Thread(con).start();
              }
             
          }




          http://www.cnblogs.com/xwdreamer/archive/2011/11/20/2296931.html#2397397

          posted on 2012-08-02 15:30 abin 閱讀(467) 評論(0)  編輯  收藏 所屬分類: JavaMultithread
          主站蜘蛛池模板: 贡嘎县| 准格尔旗| 巴林右旗| 新田县| 渝北区| 广饶县| 达孜县| 中江县| 江华| 荣成市| 龙门县| 苏尼特右旗| 北流市| 台南市| 河津市| 泰州市| 响水县| 太谷县| 唐山市| 镇沅| 贵南县| 奉节县| 洛阳市| 台州市| 丰原市| 雷波县| 湘潭市| 蒙城县| 棋牌| 双桥区| 磐石市| 华坪县| 辽宁省| 眉山市| 商都县| 泰顺县| 驻马店市| 阜康市| 吉木萨尔县| 高安市| 儋州市|