日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java多线程间的通信传值_Java 多线程之间的通信

發布時間:2024/9/19 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多线程间的通信传值_Java 多线程之间的通信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個線程送水,一個線程出水:多個線程操作同一個資源,但操作的動作不同。兩個線程操作同一資源,但操作的動作不一樣。兩個方法

packagecn.itcast.day5.thread;//一進一出

public class線程通信

{public static voidmain(String[] args)

{

Resouce resouce= newResouce();

Input input= newInput(resouce);

OutPut outPut= newOutPut(resouce);

Thread thread1= newThread(input);

Thread thread2= newThread(outPut);

thread1.start();

thread2.start();

}

}classResouce

{publicString name;publicString sex;

}class Input implementsRunnable

{

Resouce resouce;

Input(Resouce resouce)

{this.resouce =resouce;

}

@Overridepublic voidrun()

{int x = 1;while (true)

{synchronized (Input.class)//兩個線程的鎖只要是一個object 在內存中的字節碼

{if (x == 0)

{

resouce.name= "peter.peng";

resouce.sex= "boy";

}else{

resouce.name= "彭運松";

resouce.sex= "男";

}

x= (x + 1) % 2;

}

}

}

}class OutPut implementsRunnable

{

Resouce resouce;

OutPut(Resouce resouce)

{this.resouce =resouce;

}

@Overridepublic voidrun()

{while (true)

{synchronized (Input.class) //兩個線程的鎖只要是一個object 在內存中的字節碼

{

System.out.println(Thread.currentThread()+ "-----:" + resouce.name + ":" +resouce.sex);

}

}

}

}

進水與進水的案例

如果要達到一進一去,交替出現而不是從上一案例一樣,則得用到 wait()??? ?notify()? notityAll(),理解這幾個意義

1 packagecn.itcast.day5.thread;2

3 //一進一出

4 public class線程通信5 {6 public static voidmain(String[] args)7 {8 Resouce resouce = newResouce();9 Input input = newInput(resouce);10 OutPut outPut = newOutPut(resouce);11

12 Thread thread1 = newThread(input);13 Thread thread2 = newThread(outPut);14

15 thread1.start();16 thread2.start();17 }18 }19

20 classResouce21 {22 publicString name;23 publicString sex;24 boolean flag=false;25 }26

27 class Input implementsRunnable28 {29 Resouce resouce;30

31 Input(Resouce resouce)32 {33 this.resouce =resouce;34 }35

36 @Override37 public voidrun()38 {39 int x = 0;40 while (true)41 {42 synchronized (resouce)//兩個線程的鎖只要是一個object 在內存中的字節碼

43 {44 if(resouce.flag)45 try

46 {47 wait();48 //由于第一次flag=false 所以線程1不會等待,就會執行x==0,賦一次值,賦值完后,flag=true,這時線程149 //就會等待,同是叫醒線程2.50 //由于flag=true 則線程2不會等待,就會執行輸出語句,輸出完后,flag=false,這時線程2就會等待,同時叫醒線程151 //這時就會執行else語句給resource第二次賦值,賦完值后x=(x+1)%2,這里x=0,同時flag=true,線程1又等待。同時52 //第二次叫醒線程2 .............

53 }54 catch(Exception e)55 {56 }57 if (x == 0)58 {59 resouce.name = "peter.peng";60 resouce.sex = "boy";61 } else

62 {63 resouce.name = "彭運松";64 resouce.sex = "男";65 }66 x = (x + 1) % 2;67 resouce.flag = true;68 resouce.notify();//叫醒線程2

69 }70 }71 }72

73 }74

75 class OutPut implementsRunnable76 {77 Resouce resouce;78

79 OutPut(Resouce resouce)80 {81 this.resouce =resouce;82 }83

84 @Override85 public voidrun()86 {87 while (true)88 {89 synchronized (resouce) //兩個線程的鎖只要是一個object 在內存中的字節碼

90 {91 if (!resouce.flag)92 try

93 {94 resouce.wait();95 }96 catch(Exception e)97 {98

99 }100 System.out.println(resouce.name + ":" +resouce.sex);101 resouce.flag = false;102 resouce.notify();//叫醒線程1

103 }104 }105 }106 }

多張程交替打印兩個人名

對等待線程的優化(上一程序) 對Resurce進行封裝,并把set get 方法進行同步。

生者消費者的例子

1 packagenet.nw.entites;2

3 public classThreadDemo_Product_Customer {4

5 public static voidmain(String[] args) {6

7 Resource resource = newResource();8 Product product = newProduct(resource);9 Customer customer = newCustomer(resource);10

11 Thread t1 = newThread(product);12 Thread t2 = newThread(customer);13 Thread t3 = newThread(product);14 Thread t4 = newThread(customer);15

16 t1.start();17 t2.start();18 t3.start();19 t4.start();20 }21 }22

23 classResource {24 publicString name;25 public booleanflag;26 public int count = 0;27

28 public synchronized voidSet(String name) {29 while (true) {30 while(flag) {31 try{32 wait();33 } catch(Exception e) {34 e.printStackTrace();35 }36 }37 this.name = name + count++;38 System.out.println(Thread.currentThread().getName() + "-P:"

39 + this.name);40 flag = true;41 notifyAll();42 }43 }44

45 public synchronized voidgetOut() {46 while (true) {47 while (!flag) {48 try{49 wait();50 } catch(Exception e) {51 e.printStackTrace();52 }53 }54 System.out.println(Thread.currentThread().getName() + "-C:"

55 + this.name);56 flag = false;57 notifyAll();58 }59 }60 }61

62 class Product implementsRunnable {63 Resource resource;64

65 Product(Resource resource) {66 this.resource =resource;67 }68

69 @Override70 public voidrun() {71 resource.Set("pc");72 }73 }74

75 class Customer implementsRunnable {76 Resource resource;77

78 Customer(Resource resource) {79 this.resource =resource;80 }81

82 @Override83 public voidrun() {84 resource.getOut();85 }86

87 }

兩個以上線程的消費與生產關系

JdK1.5 新特性的多線程操作

1 packagenet.nw.entites;2

3 importjava.util.concurrent.locks.Condition;4 importjava.util.concurrent.locks.Lock;5 importjava.util.concurrent.locks.ReentrantLock;6

7 public classThreadDemo_Product_Customer {8

9 public static voidmain(String[] args) {10

11 Resource resource = newResource();12 Product product = newProduct(resource);13 Customer customer = newCustomer(resource);14

15 Thread t1 = newThread(product);16 Thread t2 = newThread(customer);17 Thread t3 = newThread(product);18 Thread t4 = newThread(customer);19

20 t1.start();21 t2.start();22 t3.start();23 t4.start();24 }25 }26

27 classResource {28 publicString name;29 public booleanflag;30 public int count = 0;31

32 final Lock lock = newReentrantLock();33 final Condition notFull =lock.newCondition();34 final Condition notEmpty =lock.newCondition();35

36 public voidSet(String name) {37 lock.lock();38 try{39 while (true) {40 while(flag) {41 try{42 notFull.await();43 } catch(Exception e) {44 e.printStackTrace();45 }46 }47 this.name = name + count++;48 System.out.println(Thread.currentThread().getName() + "-P:"

49 + this.name);50 flag = true;51 notEmpty.signal();52 }53 } finally{54 lock.unlock();55 }56 }57

58 public voidgetOut() {59 lock.lock();60 try{61 while (true) {62 while (!flag) {63 try{64 notEmpty.await();65 } catch(Exception e) {66 e.printStackTrace();67 }68 }69 System.out.println(Thread.currentThread().getName() + "-C:"

70 + this.name);71 flag = false;72 notFull.signal();73 }74 } finally{75 lock.unlock();76 }77 }78 }79

80 class Product implementsRunnable {81 Resource resource;82

83 Product(Resource resource) {84 this.resource =resource;85 }86

87 @Override88 public voidrun() {89 resource.Set("pc");90 }91 }92

93 class Customer implementsRunnable {94 Resource resource;95

96 Customer(Resource resource) {97 this.resource =resource;98 }99

100 @Override101 public voidrun() {102 resource.getOut();103 }104

105 }

兩個以上線程的生產與消費關系

說明:Lock提供更為優秀的方法來替換synchronized??? lock.lock ? ? lock.unlock(放在finally)

await??? single?? singleall? 分別對應? wait notify notifyAll ,但JDK1.5后提供了codition,可以為生產消費指這不同的codition,這樣兩個線程或是兩個以上線程就可以分開出來控制

如果是同一個codition singleAll時就會呼醒其它的線程同時也可以是同類的線程,達不到控制的效果。

線程的中斷:其實只要控制住run方法的循環條件就可以了,但有一種情況下await()時就沒有辦法做了,因為根本就不會執行循環體的語句,這時要用到interruput,使線程勢拋出一個異常InterruptedException,然后在驗證循環體的條件。

1 packagenet.nw.entites;2

3 importjava.util.concurrent.locks.Condition;4 importjava.util.concurrent.locks.Lock;5 importjava.util.concurrent.locks.ReentrantLock;6

7 public classThreadDemo_Product_Customer {8

9 public static voidmain(String[] args) {10

11 Resource resource = newResource();12 Product product = newProduct(resource);13 Customer customer = newCustomer(resource);14

15 Thread t1 = newThread(product);16 Thread t2 = newThread(customer);17 Thread t3 = newThread(product);18 Thread t4 = newThread(customer);19 t1.start();20 t2.start();21

22 //t3.start();23 //t4.start();

24 }25 }26

27 classResource {28 publicString name;29 public booleanflag;30 public boolean conditionFlag = true;31 public int count = 0;32

33 final Lock lock = newReentrantLock();34 final Condition notFull =lock.newCondition();35 final Condition notEmpty =lock.newCondition();36

37 public voidSet(String name) {38

39 lock.lock();40 try{41 while(conditionFlag) {42 while(flag) {43 try{44 notFull.await();45 Thread.currentThread().interrupt();//把當前的線程中斷了。

46 } catch(InterruptedException e) {47 conditionFlag = false;48 }49 }50 this.name = name + count++;51 System.out.println(Thread.currentThread().getName() + "-P:"

52 + this.name);53 flag = true;54 notEmpty.signal();55 }56 } finally{57 lock.unlock();58 if(Thread.interrupted()) {59 System.out.println(Thread.currentThread().getName()60 + ":線程中斷了。。。。。。。。。。。");61 }62 }63 }64

65 public voidgetOut() {66

67 lock.lock();68 try{69 while(conditionFlag) {70 while (!flag) {71 try{72 notEmpty.await();73 Thread.currentThread().interrupt();//把當前的線程中斷了。

74 } catch(InterruptedException e) {75 conditionFlag = false;76 }77 }78 System.out.println(Thread.currentThread().getName() + "-C:"

79 + this.name);80 flag = false;81 notFull.signal();82 }83 } finally{84 lock.unlock();85 if(Thread.interrupted()) {86 System.out.println(Thread.currentThread().getName()87 + ":線程中斷了。。。。。。。。。。。");88 }89 }90 }91 }92

93 class Product implementsRunnable {94 Resource resource;95

96 Product(Resource resource) {97 this.resource =resource;98 }99

100 @Override101 public voidrun() {102 resource.Set("pc");103 }104 }105

106 class Customer implementsRunnable {107 Resource resource;108

109 Customer(Resource resource) {110 this.resource =resource;111 }112

113 @Override114 public voidrun() {115 resource.getOut();116 }117

118 }

多線程安全通信的線程中斷

線程的join?? setDaemon? yield

1 packagecn.itcast.day5.thread;2

3 class Demo1 implementsRunnable4 {5 private int count = 100;6

7 @Override8 public voidrun()9 {10 while (count > 0)11 {12 System.out.println(Thread.currentThread().getName() + ":" + "------peter.peng" + count--);13 Thread.yield();14 }15 }16 }17

18 public classThreadJionYield19 {20 public static void main(String[] args) throwsInterruptedException21 {22 //SetDaemon();//設為后臺線程23 //ThreadJoin();//join可以用來時加入線程,遇到一個線程的Join主線程凍結,其它的不變,只到Join這個線程結束,主線程再執行。24 //SetPriority();//設置線程的優勢級

25 Thread thread1 = new Thread(new Demo1());//yield讓當前線程停止執行另一個線程。

26 Thread thread2 = new Thread(newDemo1());27

28 thread1.start();29 thread2.start();30 }31

32 private static voidSetPriority()33 {34 Thread thread1 = new Thread(newRunnable()35 {36 int count = 100;37

38 public voidrun()39 {40 while (count > 0)41 {42 System.out.println(Thread.currentThread().toString() + "------peter.peng" + count--);43 }44 }45 });46

47 Thread thread2 = new Thread(newRunnable()48 {49 int count = 100;50

51 public voidrun()52 {53 while (count > 0)54 {55 System.out.println(Thread.currentThread().toString() + "------peter.peng" + count--);56 }57 }58 });59

60 thread1.start();61 thread1.setPriority(Thread.MAX_PRIORITY);//設線程的優化級

62 thread2.start();63

64 System.out.println(Thread.currentThread().toString());65 }66

67 private static void ThreadJoin() throwsInterruptedException68 {69 Thread thread1 = new Thread(newRunnable()70 {71 int count = 100;72

73 public voidrun()74 {75 while (count > 0)76 {77 System.out.println(Thread.currentThread() + "------peter.peng" + count--);78 }79 }80 });81

82 Thread thread2 = new Thread(newRunnable()83 {84 int count = 100;85

86 public voidrun()87 {88 while (count > 0)89 {90 System.out.println(Thread.currentThread() + "------peter.peng" + count--);91 }92 }93 });94

95 thread1.start();96 //thread1.join();//說明thread1要主線程的執行權,這個時候主線程就會凍結了,只到它不要了才會給主線程,如果把它放在后面來執行

97 thread2.start();98 thread1.join();//這時thread1 與thread2交才替出現,只到結束才執行主線程的方法。

99

100 System.out.println(Thread.currentThread() + ":main");101 }102

103 private static voidSetDaemon()104 {105 Thread thread1 = new Thread(newRunnable()106 {107 int count = 0;108

109 public voidrun()110 {111 while (true)112 {113 System.out.println("------peter.peng" + count++);114 }115 }116 });117

118 Thread thread2 = new Thread(newRunnable()119 {120 int count = 0;121

122 public voidrun()123 {124 while (true)125 {126 System.out.println("------peter.peng" + count++);127 }128 }129 });130 thread1.setDaemon(true);//設為后臺線程

131 thread2.setDaemon(true);//設為后臺線程

132 thread1.start();133 thread2.start();134 System.out.println("over");135 }136 }

線程的Join,setDaemon yield

總結

以上是生活随笔為你收集整理的java多线程间的通信传值_Java 多线程之间的通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。