brand new start

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          java 多線程

          生產(chǎn)消費(fèi)例子:
          用synchronized:
          class Resource{
              
          private String name;
              
          private int count = 0;
              
          private boolean flag = true;
              
          public synchronized void set(String name){
                  
          while(!flag){
                      
          try {
                          wait();
                      } 
          catch (InterruptedException e) {
                      
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                  
          if(flag){
                      
          this.name = name;
                      System.out.println(Thread.currentThread().getName()
          +"   生產(chǎn)了    "+name+""+(++count));
                      
                  }
                  flag 
          = false;
                  notifyAll();
              }
              
          public synchronized void get(){
                  
          while(flag){
                      
          try {
                          wait();
                      } 
          catch (InterruptedException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  }
                  
          if(!flag){
                      System.out.println(Thread.currentThread().getName()
          +"   消費(fèi)了    "+name+""+count);
                      
                  }
                  flag 
          = true;
                  notifyAll();
              }
          }

          class Producer implements Runnable{
              
          private Resource r;
              Producer(Resource r){
                  
          this.r = r;
              }
              
          public void run(){
                  
          while(true){
                      r.set(
          "Tom");
                  }
              }
              
          }

          class Consumer implements Runnable{
              
          private Resource r;
              Consumer(Resource r){
                  
          this.r = r;
              }
              
          public void run(){
                  
          while(true){
                      r.get();
                  }
              }
              
          }

          public class TradeDemo {
              
          public static void main(String[] args){
                  Resource res 
          = new Resource();
                  Producer p 
          = new Producer(res);
                  Consumer c 
          = new Consumer(res);
                  Thread t1 
          = new Thread(p);
                  Thread t2 
          = new Thread(p);
                  Thread t3 
          = new Thread(c);
                  Thread t4 
          = new Thread(c);
                  t1.start();
                  t2.start();
                  t3.start();
                  t4.start();
              }
          }
          用Lock:
          import java.util.concurrent.locks.*;

          import org.junit.Test;

          class Resources {
              
          private String name;
              
          private int count = 0;
              
          private boolean flag = true;
              
          private Lock lock = new ReentrantLock();
              
          private Condition condition_pro = lock.newCondition();
              
          private Condition condition_con = lock.newCondition();

              
          public void set(String name) {
                  lock.lock();
                  
          try {
                      
          while (!flag) {
                          condition_pro.await();
                      }
                      
          if (flag) {
                          
          this.name = name;
                          System.out.println(Thread.currentThread().getName()
                                  
          + "   生產(chǎn)了    " + name + "" + (++count));

                      }
                      flag 
          = false;
                      condition_con.signal();
                  } 
          catch (InterruptedException e) {
                      System.out.println(
          "生產(chǎn)exception");
                  } 
          finally {
                      lock.unlock();
                  }
              }

              
          public void get() {
                  lock.lock();
                  
          try {
                      
          while (flag) {
                          condition_con.await();
                      }
                      
          if (!flag) {
                          System.out.println(Thread.currentThread().getName()
                                  
          + "   消費(fèi)了    " + name + "" + count);

                      }
                      flag 
          = true;
                      condition_pro.signal();
                  } 
          catch (InterruptedException e) {
                      System.out.println(
          "消費(fèi)exception");
                  } 
          finally {
                      lock.unlock();
                  }
              }
          }

          class Producers implements Runnable {
              
          private Resources r;

              Producers(Resources r) {
                  
          this.r = r;
              }

              
          public void run() {
                  
          while (true) {
                      r.set(
          "Tom");
                  }
              }

          }

          class Consumers implements Runnable {
              
          private Resources r;

              Consumers(Resources r) {
                  
          this.r = r;
              }
              @Test
              
          public void run() {
                  
          while (true) {
                      r.get();
                  }
              }

          }

          public class TradeDemo2 {
              
          public static void main(String[] args) {
                  Resources res 
          = new Resources();
                  Producers p 
          = new Producers(res);
                  Consumers c 
          = new Consumers(res);
                  Thread t1 
          = new Thread(p);
                  Thread t2 
          = new Thread(p);
                  Thread t3 
          = new Thread(c);
                  Thread t4 
          = new Thread(c);
                  t1.start();
                  t2.start();
                  t3.start();
                  t4.start();
              }
          }
          Dead lock:
          public class DeadLockThread implements Runnable{
              
          private int tickets = 100;
              
          private boolean flag;
              DeadLockThread(
          boolean flag){
                  
          this.flag = flag;
              }
              
          public void run(){
                  
          while(tickets>0){
                      
          if(flag){
                          
          synchronized(MyLock.locka){
                              System.out.println(
          "if locka");
                              
          synchronized(MyLock.lockb){
                                  System.out.println(Thread.currentThread().getName()
          +"..true.."+"if lockb  "+tickets--);
                              }
                          }
                      }
                      
          else{
                          
          synchronized(MyLock.lockb){
                              System.out.println(
          "else lockb");
                              
          synchronized(MyLock.locka){
                                  System.out.println(Thread.currentThread().getName()
          +"..false.."+"else lockb  "+tickets--);
                              }
                          }
                      }
                  }
              }
              
          }

          class MyLock{
              
          static Object locka = new Object();
              
          static Object lockb = new Object();
          }

          posted on 2012-06-14 17:12 Nemo_blinker 閱讀(1027) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 晋城| 江陵县| 怀远县| 五家渠市| 宜丰县| 五莲县| 南雄市| 百色市| 辽阳县| 新乡市| 开化县| 清丰县| 长阳| 桦川县| 利津县| 潮安县| 东丽区| 正安县| 永登县| 西宁市| 尖扎县| 茂名市| 施秉县| 夹江县| 乐山市| 渝北区| 延寿县| 陵川县| 昌吉市| 屏山县| 临清市| 漾濞| 叙永县| 东宁县| 江北区| 晋州市| 灵宝市| 南江县| 南皮县| 台州市| 普定县|