Skip to content

Commit 7ebbda2

Browse files
committed
java-multithread communication更新至example11,生产者消费者系列
1 parent 26d9caa commit 7ebbda2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1261
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# 仓库目录
1515

1616
- [blogs](/blogs):博客文档
17-
- [java-base](/java-base):java基础巩固部分的模块
18-
- [java-multithread](/java-multithread):多线程模块
17+
- [java-base](/java-base):java基础巩固模块的java源码
18+
- [java-multithread](/java-multithread):多线程模块的java源码
1919

2020

2121

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class C_Thread extends Thread{
7+
private Consumer c;
8+
9+
public C_Thread(Consumer c) {
10+
super();
11+
this.c = c;
12+
}
13+
14+
@Override
15+
public void run() {
16+
while (true){
17+
c.popService();
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class Consumer {
7+
private MyStack myStack;
8+
9+
public Consumer(MyStack myStack) {
10+
super();
11+
this.myStack = myStack;
12+
}
13+
14+
public void popService(){
15+
System.out.println("pop = "+ myStack.pop()+" Consumer的popService方法中打印pop返回值");
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by Brian on 2016/4/14.
8+
*/
9+
public class MyStack {
10+
private List list = new ArrayList();
11+
12+
synchronized public void push(){
13+
try {
14+
while(list.size()== 1){
15+
System.out.println("push操作中的: "+ Thread.currentThread().getName()+ " 线程呈wait状态");
16+
this.wait();
17+
}
18+
list.add(Math.random());
19+
this.notify();
20+
System.out.println("push = " + list.size() );
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
synchronized public String pop(){
27+
String returnValue = "";
28+
try {
29+
while(list.size() == 0){
30+
System.out.println("pop操作中的: "+ Thread.currentThread().getName()+ " 线程呈wait状态");
31+
this.wait();
32+
}
33+
returnValue =list.get(0)+ " "+Thread.currentThread().getName() ;
34+
list.remove(0);
35+
this.notify();
36+
System.out.println("pop = "+ list.size()+" Mystack的pop方法中 线程"+Thread.currentThread().getName());
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
return returnValue;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class P_Thread extends Thread{
7+
private Producer p;
8+
9+
public P_Thread(Producer p) {
10+
super();
11+
this.p = p;
12+
}
13+
14+
@Override
15+
public void run() {
16+
while (true){
17+
p.pushService();
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
4+
/**
5+
* Created by Brian on 2016/4/14.
6+
*/
7+
public class Producer {
8+
private MyStack myStack;
9+
public Producer(MyStack myStack){
10+
super();
11+
this.myStack = myStack;
12+
}
13+
14+
public void pushService(){
15+
myStack.push();
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.brianway.learning.java.multithread.communication.example10;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* P168
9+
* 一生产与多消费
10+
*
11+
* while判断解决条件发生改变时没有得到及时的响应,多个呈wait状态的线程被唤醒的问题
12+
* 但会出现新的问题:假死
13+
*
14+
*/
15+
public class Run10_oneP_manyC {
16+
public static void main(String[] args) {
17+
MyStack myStack = new MyStack();
18+
Producer p = new Producer(myStack);
19+
20+
P_Thread p_thread = new P_Thread(p);
21+
p_thread.start();
22+
23+
24+
int cNum = 5;
25+
Consumer[] consumers = new Consumer[cNum];
26+
C_Thread[] c_threads = new C_Thread[cNum];
27+
28+
for(int i=0;i<cNum;i++){
29+
consumers[i] = new Consumer(myStack);
30+
}
31+
32+
for (int i=0;i<cNum;i++){
33+
c_threads[i] = new C_Thread(consumers[i]);
34+
}
35+
36+
for (int i=0;i<cNum;i++){
37+
c_threads[i].start();
38+
}
39+
40+
}
41+
}
42+
43+
/*
44+
输出:
45+
push = 1
46+
push操作中的: Thread-0 线程呈wait状态
47+
pop = 0 Mystack的pop方法中 线程Thread-1
48+
pop = 0.28218649074189095 Thread-1 Consumer的popService方法中打印pop返回值
49+
pop操作中的: Thread-1 线程呈wait状态
50+
pop操作中的: Thread-2 线程呈wait状态
51+
push = 1
52+
push操作中的: Thread-0 线程呈wait状态
53+
pop = 0 Mystack的pop方法中 线程Thread-3
54+
pop = 0.26526620276366075 Thread-3 Consumer的popService方法中打印pop返回值
55+
pop操作中的: Thread-3 线程呈wait状态
56+
pop操作中的: Thread-1 线程呈wait状态
57+
pop操作中的: Thread-2 线程呈wait状态
58+
pop操作中的: Thread-5 线程呈wait状态
59+
pop操作中的: Thread-4 线程呈wait状态
60+
61+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.brianway.learning.java.multithread.communication.example11;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class C_Thread extends Thread{
7+
private Consumer c;
8+
9+
public C_Thread(Consumer c) {
10+
super();
11+
this.c = c;
12+
}
13+
14+
@Override
15+
public void run() {
16+
while (true){
17+
c.popService();
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.brianway.learning.java.multithread.communication.example11;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class Consumer {
7+
private MyStack myStack;
8+
9+
public Consumer(MyStack myStack) {
10+
super();
11+
this.myStack = myStack;
12+
}
13+
14+
public void popService(){
15+
System.out.println("pop = "+ myStack.pop()+" Consumer的popService方法中打印pop返回值");
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.brianway.learning.java.multithread.communication.example11;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by Brian on 2016/4/14.
8+
*/
9+
public class MyStack {
10+
private List list = new ArrayList();
11+
12+
synchronized public void push(){
13+
try {
14+
while(list.size()== 1){
15+
System.out.println("push操作中的: "+ Thread.currentThread().getName()+ " 线程呈wait状态");
16+
this.wait();
17+
}
18+
list.add(Math.random());
19+
this.notifyAll();
20+
System.out.println("push = " + list.size() );
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
synchronized public String pop(){
27+
String returnValue = "";
28+
try {
29+
while(list.size() == 0){
30+
System.out.println("pop操作中的: "+ Thread.currentThread().getName()+ " 线程呈wait状态");
31+
this.wait();
32+
}
33+
returnValue =list.get(0)+ " "+Thread.currentThread().getName() ;
34+
list.remove(0);
35+
this.notifyAll();
36+
System.out.println("pop = "+ list.size()+" Mystack的pop方法中 线程"+Thread.currentThread().getName());
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
return returnValue;
41+
}
42+
}

0 commit comments

Comments
 (0)