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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java必學必會之線程(2)

java必學必會之線程(2)

2020-03-07 14:14孤傲蒼狼 JAVA教程

本文對java線程進行深入學習,重點介紹了線程同步問題、線程死鎖問題,感興趣的小伙伴們可以參考一下

一、線程的優先級別

  java必學必會之線程(2)

線程優先級別的使用范例:

?
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
package cn.galc.test;
 
public class TestThread6 {
 public static void main(String args[]) {
 MyThread4 t4 = new MyThread4();
 MyThread5 t5 = new MyThread5();
 Thread t1 = new Thread(t4);
 Thread t2 = new Thread(t5);
 t1.setPriority(Thread.NORM_PRIORITY + 3);// 使用setPriority()方法設置線程的優先級別,這里把t1線程的優先級別進行設置
 /*
  * 把線程t1的優先級(priority)在正常優先級(NORM_PRIORITY)的基礎上再提高3級
  * 這樣t1的執行一次的時間就會比t2的多很多     
  * 默認情況下NORM_PRIORITY的值為5
  */
 t1.start();
 t2.start();
 System.out.println("t1線程的優先級是:" + t1.getPriority());
 // 使用getPriority()方法取得線程的優先級別,打印出t1的優先級別為8
 }
}
 
class MyThread4 implements Runnable {
 public void run() {
 for (int i = 0; i <= 1000; i++) {
  System.out.println("T1:" + i);
 }
 }
}
 
class MyThread5 implements Runnable {
 public void run() {
 for (int i = 0; i <= 1000; i++) {
  System.out.println("===============T2:" + i);
 }
 }
}
  run()方法一結束,線程也就結束了。

二、線程同步

 java必學必會之線程(2) 

synchronized關鍵字的使用范例:

?
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
package cn.galc.test;
 
public class TestSync implements Runnable {
 Timer timer = new Timer();
 
 public static void main(String args[]) {
 TestSync test = new TestSync();
 Thread t1 = new Thread(test);
 Thread t2 = new Thread(test);
 t1.setName("t1");// 設置t1線程的名字
 t2.setName("t2");// 設置t2線程的名字
 t1.start();
 t2.start();
 }
 
 public void run() {
 timer.add(Thread.currentThread().getName());
 }
}
 
class Timer {
 private static int num = 0;
 
 public/* synchronized */void add(String name) {// 在聲明方法時加入synchronized時表示在執行這個方法的過程之中當前對象被鎖定
 synchronized (this) {
  /*
  * 使用synchronized(this)來鎖定當前對象,這樣就不會再出現兩個不同的線程同時訪問同一個對象資源的問題了 只有當一個線程訪問結束后才會輪到下一個線程來訪問
  */
  num++;
  try {
  Thread.sleep(1);
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  System.out.println(name + ":你是第" + num + "個使用timer的線程");
 }
 }
}

線程死鎖的問題:

?
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
64
65
66
67
68
69
70
71
72
package cn.galc.test;
 
/*這個小程序模擬的是線程死鎖的問題*/
public class TestDeadLock implements Runnable {
 public int flag = 1;
 static Object o1 = new Object(), o2 = new Object();
 
 public void run() {
 System.out.println(Thread.currentThread().getName() + "的flag=" + flag);
 /*
  * 運行程序后發現程序執行到這里打印出flag以后就再也不往下執行后面的if語句了
  * 程序也就死在了這里,既不往下執行也不退出
  */
 
 /* 這是flag=1這個線程 */
 if (flag == 1) {
  synchronized (o1) {
  /* 使用synchronized關鍵字把對象01鎖定了 */
  try {
   Thread.sleep(500);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  synchronized (o2) {
   /*
   * 前面已經鎖住了對象o1,只要再能鎖住o2,那么就能執行打印出1的操作了
   * 可是這里無法鎖定對象o2,因為在另外一個flag=0這個線程里面已經把對象o1給鎖住了
   * 盡管鎖住o2這個對象的線程會每隔500毫秒睡眠一次,可是在睡眠的時候仍然是鎖住o2不放的
   */
   System.out.println("1");
  }
  }
 }
 /*
  * 這里的兩個if語句都將無法執行,因為已經造成了線程死鎖的問題
  * flag=1這個線程在等待flag=0這個線程把對象o2的鎖解開,
  * 而flag=0這個線程也在等待flag=1這個線程把對象o1的鎖解開
  * 然而這兩個線程都不愿意解開鎖住的對象,所以就造成了線程死鎖的問題
  */
 
 /* 這是flag=0這個線程 */
 if (flag == 0) {
  synchronized (o2) {
  /* 這里先使用synchronized鎖住對象o2 */
  try {
   Thread.sleep(500);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  synchronized (o1) {
   /*
   * 前面已經鎖住了對象o2,只要再能鎖住o1,那么就能執行打印出0的操作了 可是這里無法鎖定對象o1,因為在另外一個flag=1這個線程里面已經把對象o1給鎖住了 盡管鎖住o1這個對象的線程會每隔500毫秒睡眠一次,可是在睡眠的時候仍然是鎖住o1不放的
   */
   System.out.println("0");
  }
  }
 }
 }
 
 public static void main(String args[]) {
 TestDeadLock td1 = new TestDeadLock();
 TestDeadLock td2 = new TestDeadLock();
 td1.flag = 1;
 td2.flag = 0;
 Thread t1 = new Thread(td1);
 Thread t2 = new Thread(td2);
 t1.setName("線程td1");
 t2.setName("線程td2");
 t1.start();
 t2.start();
 }
}

  解決線程死鎖的問題最好只鎖定一個對象,不要同時鎖定兩個對象

生產者消費者問題:

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cn.galc.test;
 
/* 范例名稱:生產者--消費者問題
 * 源文件名稱:ProducerConsumer.java
 * 要 點:
 * 1. 共享數據的不一致性/臨界資源的保護
 * 2. Java對象鎖的概念
 * 3. synchronized關鍵字/wait()及notify()方法
 */
 
public class ProducerConsumer {
 public static void main(String args[]){
  SyncStack stack = new SyncStack();
  Runnable p=new Producer(stack);
  Runnable c = new Consumer(stack);
  Thread p1 = new Thread(p);
  Thread c1 = new Thread(c);
  
  p1.start();
  c1.start();
 }
}
 
 
class SyncStack{ //支持多線程同步操作的堆棧的實現
 private int index = 0;
 private char []data = new char[6];
 public synchronized void push(char c){
 if(index == data.length){
 try{
  this.wait();
 }catch(InterruptedException e){}
 }
 this.notify();
 data[index] = c;
 index++;
 }
 public synchronized char pop(){
 if(index ==0){
  try{
  this.wait();
  }catch(InterruptedException e){}
 }
 this.notify();
 index--;
 return data[index];
 }
}
 
 
class Producer implements Runnable{
 SyncStack stack;
 public Producer(SyncStack s){
 stack = s;
 }
 public void run(){
 for(int i=0; i<20; i++){
  char c =(char)(Math.random()*26+'A');
  stack.push(c);
  System.out.println("produced:"+c);
  try{    
  Thread.sleep((int)(Math.random()*1000));
  }catch(InterruptedException e){
  }
 }
 }
}
 
 
class Consumer implements Runnable{
 SyncStack stack;
 public Consumer(SyncStack s){
 stack = s;
 }
 public void run(){
 for(int i=0;i<20;i++){
  char c = stack.pop();
  System.out.println("消費:"+c);
  try{    
  Thread.sleep((int)(Math.random()*1000));
  }catch(InterruptedException e){
  }
 }
 }
}

以上就是關于java線程的全部內容介紹,大家可以結合第一篇《java必學必會之線程(1)》進行學習,希望可以幫助到大家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美国产精品 | 午夜视频在线网站 | 国产成人精品午夜免费 | 精品四虎国产在免费观看 | 91精品手机国产在线观 | 亚洲AV无码专区国产精品麻豆 | 高h辣文小说网 烧书阁 | 妇女澡堂淋浴性 | 日韩一区二区三 | 国产精品aaa | 精品国产三级av在线 | 喜欢老头吃我奶躁我的动图 | 亚洲成人视屏 | 免费看黄色大片 | 高清不卡免费一区二区三区 | 日韩一区二区在线视频 | 青青久久精品国产 | 亚洲理论视频 | 久久人妻熟女中文字幕AV蜜芽 | 日本中文字幕在线观看视频 | 国内精品一区二区在线观看 | 好吊日在线 | 成人福利在线视频免费观看 | 99爱在线观看 | 欧美一区二区三区免费不卡 | 啊啊啊好大视频 | 羞羞私人影院可以直接免费观影吗 | 男人猛激烈吃奶gif动态图 | 天美传媒影视在线免费观看 | 午夜精品在线视频 | gaygayas男男免费中国 | 亚洲国产精品无码中文字满 | 邪恶肉肉全彩色无遮琉璃神社 | 污污免费 | 91国产高清 | 欧美日韩精品一区二区三区视频播放 | 99在线视频精品费观看视 | 国产日韩欧美视频 | 色综色| 大胸被c出奶水嗷嗷叫 | 免费网站看v片在线成人国产系列 |