2種實(shí)現(xiàn)線程的方法:1.繼承Thread類。 2.實(shí)現(xiàn)Runnable接口。
          多個(gè)線程操作同一資源的問題(不同步):第一個(gè)線程正操作資源,還沒完成,第二個(gè)線程也來操作。
          出現(xiàn)了資源錯(cuò)位。
          解決的方法是資源的同步(synchronized)。即:一個(gè)線程訪問資源的時(shí)候,別的線程不能訪問該資源。
          實(shí)現(xiàn)同步有2種方法: 1.把方法同步。  2.同步代碼塊。
          實(shí)現(xiàn)了同步特征:1.線程安全了。 2.性能差了。(異步正好相反)
          Object中的wait(),使線程進(jìn)入等待狀態(tài),需要?jiǎng)e的線程notify()繼續(xù)工作。
          wait()必須要在同步的情況下才能使用。
          隨之帶來了死鎖的問題。
          面試題:
          一、wait()和sleep()區(qū)別?
          wait方法是Object類中的方法。
          sleep方法是Thread類中的方法。

          當(dāng)線程進(jìn)入wait時(shí)別的線程可以訪問鎖定的對(duì)象。
          當(dāng)線程進(jìn)入sleep時(shí)別的線程不能訪問鎖定的對(duì)象。

          二、寫一個(gè)死鎖。
          class Demo implements Runnable {
              
          int flag ;
              
          static Object o1 = new Object(), o2 = new Object();
              
          public void run(){
                  System.out.println(
          "線程" + flag + "在運(yùn)行。。");
                  
          if(flag == 1){
                      
          synchronized(o1){
                          
          try {
                              Thread.sleep(
          100);
                          } 
          catch(InterruptedException e) {
                              e.printStackTrace();
                          }
                          System.out.println(
          "線程1 --> o1");
                          
          synchronized(o2){
                              System.out.println(
          "線程1 --> o2");
                          }
                      }
                  }
                  
          if(flag == 2){
                      
          synchronized(o2){
                          
          try {
                              Thread.sleep(
          100);
                          } 
          catch(InterruptedException e) {
                              e.printStackTrace();
                          }
                          System.out.println(
          "線程2 --> o2");
                          
          synchronized(o1){
                              System.out.println(
          "線程2 --> o1");
                          }
                      }
                  }
              }
          }

          結(jié)果導(dǎo)致死鎖。

          線程1在運(yùn)行。。
          線程2在運(yùn)行。。
          線程1 
          --> o1
          線程2 
          --> o2
          不能繼續(xù)進(jìn)行。

          2個(gè)Object類為static,說明他們鎖定的是同2個(gè)對(duì)象;

          三、Producer和Consumer的問題
          涉及了wait方法和nofity方法
          public class ProducerConsumer {
              
          public static void main(String[] args) {
                  SyncStack ss 
          = new SyncStack();
                  Producer p 
          = new Producer(ss);
                  Consumer c 
          = new Consumer(ss);
                  
          new Thread(p).start();
                  
          new Thread(c).start();
              }
          }

          class WoTou {
              
          int id; 
              WoTou(
          int id) {
                  
          this.id = id;
              }
              
          public String toString() {
                  
          return "WoTou : " + id;
              }
          }

          class SyncStack {
              
          int index = 0;
              WoTou[] arrWT 
          = new WoTou[6];
              
              
          public synchronized void push(WoTou wt) {
                  
          while(index == arrWT.length) {
                      
          try {
                          
          this.wait();
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  
          this.notifyAll();        
                  arrWT[index] 
          = wt;
                  index 
          ++;
              }
              
              
          public synchronized WoTou pop() {
                  
          while(index == 0) {
                      
          try {
                          
          this.wait();
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  
          this.notifyAll();
                  index
          --;
                  
          return arrWT[index];
              }
          }

          class Producer implements Runnable {
              SyncStack ss 
          = null;
              Producer(SyncStack ss) {
                  
          this.ss = ss;
              }
              
              
          public void run() {
                  
          for(int i=0; i<20; i++) {
                      WoTou wt 
          = new WoTou(i);
                      ss.push(wt);
          System.out.println(
          "生產(chǎn)了:" + wt);
                      
          try {
                          Thread.sleep((
          int)(Math.random() * 200));
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }            
                  }
              }
          }

          class Consumer implements Runnable {
              SyncStack ss 
          = null;
              Consumer(SyncStack ss) {
                  
          this.ss = ss;
              }
              
              
          public void run() {
                  
          for(int i=0; i<20; i++) {
                      WoTou wt 
          = ss.pop();
          System.out.println(
          "消費(fèi)了: " + wt);
                      
          try {
                          Thread.sleep((
          int)(Math.random() * 1000));
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }            
                  }
              }
          }

          posted on 2008-12-30 13:51 天長(zhǎng) 閱讀(137) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 萝北县| 武冈市| 密云县| 新竹县| 昌平区| 色达县| 山东| 绵竹市| 呼和浩特市| 卓资县| 汶川县| 清涧县| 宝鸡市| 于田县| 积石山| 洛浦县| 扎赉特旗| 修武县| 凤山县| 赤壁市| 涟源市| 徐州市| 凤城市| 徐水县| 湄潭县| 双牌县| 绥棱县| 山阴县| 镇康县| 桐柏县| 石河子市| 三原县| 宁德市| 大化| 思南县| 湾仔区| 长兴县| 余干县| 古交市| 甘南县| 韶关市|