91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java編程之多線程死鎖與線程間通信簡單實現代碼

發布時間:2020-09-10 04:26:16 來源:腳本之家 閱讀:134 作者:yongxiHU 欄目:編程語言

死鎖定義

死鎖是指兩個或者多個線程被永久阻塞的一種局面,產生的前提是要有兩個或兩個以上的線程,并且來操作兩個或者多個以上的共同資源;我的理解是用兩個線程來舉例,現有線程A和B同時操作兩個共同資源a和b,A操作a的時候上鎖LockA,繼續執行的時候,A還需要LockB進行下面的操作,這個時候b資源在被B線程操作,剛好被上了鎖LockB,假如此時線程B剛好釋放了LockB則沒有問題,但沒有釋放LockB鎖的時候,線程A和B形成了對LockB鎖資源的爭奪,從而造成阻塞,形成死鎖;具體其死鎖代碼如下:

public class MyDeadLockTest {
 public static void main(String[] args){
  Object obj1 = new Object();
  Thread thread1 = new Thread(new DeadRes(true,obj1));
  Thread thread2 = new Thread(new DeadRes(false,obj1));
  thread1.start();
  thread2.start();
 }
}
class DeadRes implements Runnable{
 boolean flag;
 Object obj;
 public DeadRes(boolean flag, Object obj1) {
  this.flag = flag;
  this.obj = obj1;
 }
 @Override
 public void run() {
   if(flag){
    synchronized (DeadRes.class){
     System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
     synchronized (obj){
      System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
     }
    }
   }else{
    synchronized (obj){
     System.out.println(Thread.currentThread().getName()+" acquie lock is obj");
     synchronized (DeadRes.class){
      System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class");
     }
    }
   }
 }
}

執行結果如下圖:

Thread-1 acquie lock is obj
Thread-0 acquie lock is DeadRes.class

當然每次執行的結果不一樣,有可能是一種和諧狀態,沒有發生死鎖,此時為保證每次死鎖,可以讓run()方法中,執行while(true)循環,這樣保證了每次必定發生死鎖;當然實際應用中,我們應該盡量避免死鎖,當有多線程操作多個共同資源的時候,避免發生同一鎖對象的同步嵌套。

線程間的通訊—-生產者與消費者模式

1、讓兩個線程交替進行操作,當生產了一個數字后,緊接著消費一個,首先采用Object對象中的wait-notify來實現,具體代碼如下:

public class ThreadProConsume {
 public static void main(String[] args){
  Product product = new Product();
  Thread thread1 = new Thread(new Producer(product));
  Thread thread2 = new Thread(new Consumer(product));
  thread1.start();
  thread2.start();
 }
}
class Product{
 String name;
 private int count = 1;
 boolean flag = false;
 public synchronized void set(String name){
  if(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name +"--"+count++;
  flag = true;
  System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
  this.notify();
 }
 public synchronized void out(){
  if(!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
  flag = false;
  this.notify();
 }
}
class Producer implements Runnable{
 Product res;
 public Producer(Product product) {
  this.res = product;
 }
 @Override
 public void run() {
  while(true){
   res.set("guyue");
  }
 }
}
class Consumer implements Runnable{
 Product res;
 public Consumer(Product product) {
  this.res = product;
 }
 @Override
 public void run() {
  while(true){
   res.out();
  }
 }
}

執行結果如圖:

Thread-1 consume num is : guyue--3938
Thread-0 produce num : guyue--3939
Thread-1 consume num is : guyue--3939
Thread-0 produce num : guyue--3940
Thread-1 consume num is : guyue--3940
Thread-0 produce num : guyue--3941
Thread-1 consume num is : guyue--3941

當超過兩個以上線程操作的時候,這里需要在set()與out()方法中的if判斷改為while,并且notif方法,改為notifyAll(),這樣多個線程操作的時候,便可以交替進行,具體代碼如下:

public class ThreadProConsume {
 public static void main(String[] args){
  Product product = new Product();
  Thread thread1 = new Thread(new Producer(product));
  Thread thread3 = new Thread(new Producer(product));
  Thread thread2 = new Thread(new Consumer(product));
  Thread thread4 = new Thread(new Consumer(product));
  thread1.start();
  thread3.start();
  thread2.start();
  thread4.start();
 }
}
class Product{
 String name;
 private int count = 1;
 boolean flag = false;
 public synchronized void set(String name){
  while(flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  this.name = name +"--"+count++;
  flag = true;
  System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
  this.notifyAll();
 }
 public synchronized void out(){
  while (!flag){
   try {
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name);
  flag = false;
  this.notifyAll();
 }
}

執行結果如下:

Thread-0 produce num : guyue--50325
Thread-2 consume num is : guyue--50325
Thread-1 produce num : guyue--50326
Thread-3 consume num is : guyue--50326
Thread-0 produce num : guyue--50327
Thread-2 consume num is : guyue--50327
Thread-1 produce num : guyue--50328
Thread-3 consume num is : guyue--50328

2、采用Lock-Condition方法實現如下:

class Product{
 String name;
 private int count = 1;
 boolean flag = false;
 Lock lock = new ReentrantLock();
 Condition conditon = lock.newCondition();
 public void set(String name){
  try{
   lock.lock();
   while(flag){
    conditon.await();
   }
   this.name = name +"--"+count++;
   flag = true;
   System.out.println(Thread.currentThread().getName()+" produce num : "+this.name);
   conditon.signalAll();
  }catch (Exception e){
  }finally {
   lock.unlock();
  }
 }
 public void out(){
  try{
   lock.lock();
   while(!flag){
    conditon.await();
   }
   flag = false;
   System.out.println(Thread.currentThread().getName()+" consumer num is : "+this.name);
   conditon.signalAll();
  }catch (Exception e){
  }finally {
   lock.unlock();
  }
 }
}

執行結果如下:

Thread-0 produce num : guyue--20305
Thread-3 consumer num is : guyue--20305
Thread-1 produce num : guyue--20306
Thread-2 consumer num is : guyue--20306
Thread-0 produce num : guyue--20307
Thread-3 consumer num is : guyue--20307
Thread-1 produce num : guyue--20308
Thread-2 consumer num is : guyue--20308

以上就是本文關于Java編程之多線程死鎖與線程間通信簡單實現代碼的全部內容,希望對大家有所幫助。關于Java多線程以及線程間通信的例子,本站還有幾篇文章可以參考:

詳解java中的互斥鎖信號量和多線程等待機制、Java多線程編程小實例模擬停車場系統、Java網絡編程基礎篇之單向通信

如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南城县| 庆云县| 宿迁市| 阆中市| 皮山县| 怀化市| 鞍山市| 昌江| 鄂州市| 高雄县| 鹤壁市| 龙门县| 军事| 开鲁县| 类乌齐县| 怀远县| 大化| 连山| 古交市| 平塘县| 双江| 米泉市| 石渠县| 台南市| 五原县| 张家川| 邵阳市| 万宁市| 老河口市| 姜堰市| 平泉县| 祥云县| 四川省| 喀什市| 吴江市| 新平| 大冶市| 互助| 靖边县| 海伦市| 荔波县|