Skip to content

Commit 64c66e2

Browse files
committed
no message
1 parent 9807eb7 commit 64c66e2

4 files changed

Lines changed: 135 additions & 2 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
package org.tj.study.thread.pipe;
22

3+
import java.io.IOException;
4+
import java.io.PipedReader;
5+
import java.io.PipedWriter;
6+
37
/**
48
* Created by 001 on 16/8/23.
59
*/
610
public class Piped {
11+
12+
public static void main(String[] args) throws IOException {
13+
PipedReader reader = new PipedReader();
14+
PipedWriter writer = new PipedWriter();
15+
writer.connect(reader);
16+
new Thread(new Print(reader),"reader1").start();
17+
int receive = 0;
18+
19+
try {
20+
while ((receive = System.in.read()) != 0){
21+
writer.write(receive);
22+
}
23+
} finally {
24+
writer.close();
25+
}
26+
}
27+
28+
29+
static class Print implements Runnable{
30+
31+
private PipedReader in;
32+
33+
public Print(PipedReader in){
34+
this.in = in;
35+
}
36+
37+
@Override
38+
public void run() {
39+
int receive = 0;
40+
try {
41+
while ((receive = in.read()) != 0){
42+
System.out.print((char)receive);
43+
}
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
}
47+
}
48+
}
749
}

src/main/java/org/tj/study/thread/produceconsumer/Consumer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,20 @@
33
/**
44
* Created by 001 on 16/8/23.
55
*/
6-
public class Consumer {
6+
public class Consumer implements Runnable
7+
{
8+
9+
Product product;
10+
int num;
11+
12+
public Consumer(Product product,int num){
13+
this.product = product;
14+
this.num = num;
15+
}
16+
17+
@Override
18+
public void run() {
19+
product.consume(num);
20+
}
21+
722
}

src/main/java/org/tj/study/thread/produceconsumer/Producer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,18 @@
33
/**
44
* Created by 001 on 16/8/23.
55
*/
6-
public class Producer {
6+
public class Producer implements Runnable{
7+
8+
Product product;
9+
int num;
10+
11+
public Producer(Product product,int num){
12+
this.product = product;
13+
this.num = num;
14+
}
15+
16+
@Override
17+
public void run() {
18+
product.produce(num);
19+
}
720
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11
package org.tj.study.thread.produceconsumer;
22

3+
import org.tj.study.thread.ticket.one.ThreadA;
4+
35
/**
46
* Created by 001 on 16/8/23.
57
*/
68
public class Product {
9+
10+
Storage storage = new Storage();
11+
final int MAX_STOARGE = 50;
12+
13+
private class Storage{
14+
int storage = 0;
15+
private void produce(int num){
16+
this.storage+=num;
17+
System.out.println(Thread.currentThread().getName()+" 产生了 "+num+" , 还剩 "+storage );
18+
19+
}
20+
21+
private void comsume(int num){
22+
this.storage-=num;
23+
System.out.println(Thread.currentThread().getName()+" 消费了 "+num+" , 还剩 "+storage );
24+
}
25+
26+
public int getStorage() {
27+
return storage;
28+
}
29+
30+
public void setStorage(int storage) {
31+
this.storage = storage;
32+
}
33+
}
34+
35+
public void produce(int num){
36+
// 如果库存容量无限 直接生产 否则当前库存+num 大于最大库存 无法生产 生产者等待
37+
synchronized (storage){
38+
while (storage.getStorage()+num>MAX_STOARGE){
39+
try {
40+
System.out.println(Thread.currentThread().getName()+" 生产者进入等待状态,想生产: "+num+" 库存: "+storage.getStorage()+" 剩余库存"+(MAX_STOARGE - storage.getStorage()));
41+
storage.wait();
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
storage.produce(num);
47+
// 叫醒消费线程起来消费
48+
storage.notifyAll();
49+
}
50+
}
51+
52+
public void consume(int num){
53+
synchronized (storage){
54+
while (storage.getStorage()<num){
55+
// 库存小于消费的数量 等待
56+
try {
57+
System.out.println(Thread.currentThread().getName()+" 消费者进入等待状态,想消费: "+num+" 库存: "+storage.getStorage());
58+
storage.wait();
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
// 消费
64+
storage.comsume(num);
65+
storage.notifyAll();
66+
}
67+
}
68+
69+
770
}

0 commit comments

Comments
 (0)