一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼

Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼

2021-01-25 11:51yongxiHU Java教程

這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼,具有一定參考價值,需要的朋友可以了解下。

死鎖定義

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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");
     }
    }
   }
 }
}

執(zhí)行結(jié)果如下圖:

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

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

線程間的通訊—-生產(chǎn)者與消費者模式

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
  }
 }
}

執(zhí)行結(jié)果如圖:

?
1
2
3
4
5
6
7
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(),這樣多個線程操作的時候,便可以交替進行,具體代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
 }
}

執(zhí)行結(jié)果如下:

?
1
2
3
4
5
6
7
8
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方法實現(xiàn)如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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();
  }
 }
}

執(zhí)行結(jié)果如下:

?
1
2
3
4
5
6
7
8
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

以上就是本文關(guān)于Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼的全部內(nèi)容,希望對大家有所幫助。關(guān)于Java多線程以及線程間通信的例子,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/yongxihu/article/details/70277372

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 97超pen个人视频公开视频视 | 91制片厂制作传媒网站破解 | 国产日本久久久久久久久婷婷 | 国产精品热久久毛片 | 我的好妈妈7中字在线观看韩国 | 91正在 播放 | 美女奶口隐私免费视频网站 | 特黄特级高清免费视频毛片 | 无人区大片免费播放器 | 暖暖日本高清 | 国精品午夜dy8888狼人 | 亚洲国产精品成人久久 | 免费高清观看 | 免费看成年视频网页 | ass韩国美女人体pics | 大陆国语自产精品视频在 | 女明星放荡高h日常生活 | 99在线精品免费视频九九视 | ak福利影院 | 欧美在线成人免费国产 | 无码AV精品一区二区三区 | 国产日韩一区二区三区在线播放 | poverty中国老妇人 | 精品久久久久久久久久久久久久久 | 亚洲好色网 | 视频一区国产精戏刘婷30 | 涩情主播在线翻车 | 精品国产福利一区二区在线 | 美女被狂揉下部羞羞动漫 | 五月丁开婷婷 | 国产免费资源高清小视频在线观看 | 亚洲一级视频在线观看 | 古装一级无遮挡毛片免费观看 | 无遮18禁在线永久免费观看挡 | 草免费视频 | jzzjzz视频免费播放 | 欧美一区二区三区四区在线观看 | 国产成人精品一区二三区 | 免费精品一区二区三区在线观看 | poronovideos变态极限 | h视频免费高清在线观看 |