File tree Expand file tree Collapse file tree
java-se-base-parent/model-multy-thread/src/main/java/com/multy/thread/block04notify Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99| B01| [ Java并发:线程的创建方式,状态周期管理] ( https://mp.weixin.qq.com/s/h0zuh364It-fDtQyYdzIwA ) |
1010| B02| [ Java并发:线程核心机制,基础概念扩展] ( https://mp.weixin.qq.com/s/nU5tbKuAiDf_LDu-8A4Etg ) |
1111| B03| [ Java并发:多线程并发访问,同步控制] ( https://mp.weixin.qq.com/s/xWhd3vgYPNp-Mm2lJpmFmA ) |
12+ | B04| [ Java并发:线程间通信,等待/通知机制] ( https://mp.weixin.qq.com/s/WWbVjvkEqpddx605me9E-A ) |
1213
1314### JavaEE基础
1415
Original file line number Diff line number Diff line change 1+ package com .multy .thread .block04notify ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ import java .util .concurrent .TimeUnit ;
6+
7+ public class NotifyThread01 {
8+
9+ static Object lock = new Object () ;
10+ static volatile List <String > dataList = new ArrayList <>();
11+
12+ public static void main (String [] args ) throws Exception {
13+ Thread saveThread = new Thread (new SaveData (),"SaveData" );
14+ saveThread .start ();
15+ TimeUnit .SECONDS .sleep (3 );
16+ Thread dataThread = new Thread (new AnalyData (),"AnalyData" );
17+ dataThread .start ();
18+ }
19+
20+ // 等待数据生成,保存
21+ static class SaveData implements Runnable {
22+ @ Override
23+ public void run () {
24+ synchronized (lock ){
25+ while (dataList .size ()==0 ){
26+ try {
27+ System .out .println (Thread .currentThread ().getName ()+"等待..." );
28+ lock .wait ();
29+ } catch (InterruptedException e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+ System .out .println ("SaveData .." + dataList .get (0 )+dataList .get (1 ));
34+ }
35+ }
36+ }
37+ // 生成数据,通知保存
38+ static class AnalyData implements Runnable {
39+ @ Override
40+ public void run () {
41+ synchronized (lock ){
42+ dataList .add ("hello," );
43+ dataList .add ("java" );
44+ lock .notify ();
45+ System .out .println ("AnalyData End..." );
46+ }
47+ }
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ package com .multy .thread .block04notify ;
2+
3+ import java .io .*;
4+
5+ public class NotifyThread02 {
6+ public static void main (String [] args ) throws Exception {
7+ PipedInputStream pis = new PipedInputStream ();
8+ PipedOutputStream pos = new PipedOutputStream ();
9+ // 链接输入流和输出流
10+ pos .connect (pis );
11+ // 写数据线程
12+ new Thread (new Runnable () {
13+ public void run () {
14+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
15+ // 将从键盘读取的数据写入管道流
16+ PrintStream ps = new PrintStream (pos );
17+ while (true ) {
18+ try {
19+ System .out .print (Thread .currentThread ().getName ());
20+ ps .println (br .readLine ());
21+ Thread .sleep (1000 );
22+ } catch (Exception e ) {
23+ e .printStackTrace ();
24+ }
25+ }
26+ }
27+ }, "输入数据线程:" ).start ();
28+ // 读数据线程
29+ new Thread (new Runnable () {
30+ public void run () {
31+ BufferedReader br = new BufferedReader (new InputStreamReader (pis ));
32+ while (true ) {
33+ try {
34+ System .out .println (Thread .currentThread ().getName () + br .readLine ());
35+ } catch (IOException e ) {
36+ e .printStackTrace ();
37+ }
38+ }
39+ }
40+ }, "输出数据线程:" ).start ();
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ package com .multy .thread .block04notify ;
2+
3+ public class NotifyThread03 {
4+ public static void main (String [] args ) {
5+ Product product = new Product ();
6+ ProductFactory productFactory = new ProductFactory (product );
7+ ProductShop productShop = new ProductShop (product );
8+ productFactory .start ();
9+ productShop .start ();
10+ }
11+ }
12+ // 产品
13+ class Product {
14+ public String name ;
15+ public double price ;
16+ // 产品是否生产完毕,默认没有
17+ boolean flag ;
18+ }
19+ // 产品工厂:生产
20+ class ProductFactory extends Thread {
21+ Product product ;
22+ public ProductFactory (Product product ){
23+ this .product = product ;
24+ }
25+ @ Override
26+ public void run () {
27+ int i = 0 ;
28+ while (i < 20 ) {
29+ synchronized (product ) {
30+ if (!product .flag ){
31+ if (i %2 == 0 ){
32+ product .name = "鼠标" ;
33+ product .price = 79.99 ;
34+ } else {
35+ product .name = "键盘" ;
36+ product .price = 89.99 ;
37+ }
38+ System .out .println ("产品:" +product .name +"【价格:" +product .price +"】出厂..." );
39+ product .flag = true ;
40+ i ++;
41+ // 通知消费者
42+ product .notifyAll ();
43+ } else {
44+ try {
45+ // 进入等待状态
46+ product .wait ();
47+ } catch (InterruptedException e ) {
48+ e .printStackTrace ();
49+ }
50+ }
51+ }
52+ }
53+ }
54+ }
55+ // 产品商店:销售
56+ class ProductShop extends Thread {
57+ Product product ;
58+ public ProductShop (Product product ){
59+ this .product = product ;
60+ }
61+ @ Override
62+ public void run () {
63+ while (true ) {
64+ synchronized (product ) {
65+ if (product .flag == true ){
66+ System .out .println ("产品:" +product .name +"【价格" +(product .price *2 )+"】卖出..." );
67+ product .flag = false ;
68+ product .notifyAll (); //唤醒生产者
69+ } else {
70+ try {
71+ product .wait ();
72+ } catch (InterruptedException e ) {
73+ e .printStackTrace ();
74+ }
75+ }
76+ }
77+ }
78+ }
79+ }
You can’t perform that action at this time.
0 commit comments