Skip to content

Commit ee75e83

Browse files
committed
producer
1 parent 2b0cb61 commit ee75e83

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.colin.test.net;
2+
3+
import java.io.*;
4+
import java.net.*;
5+
import java.util.*;
6+
7+
public class EchoServer{
8+
public static void main(String[] args){
9+
try{
10+
ServerSocket server = new ServerSocket(8888);
11+
Socket inSocket = server.accept();
12+
try{
13+
InputStream inStream = inSocket.getInputStream();
14+
OutputStream outStream = inSocket.getOutputStream();
15+
16+
Scanner in = new Scanner(inStream);
17+
PrintWriter out = new PrintWriter(outStream,true);
18+
19+
out.println("Hello! Enter bye to exit.");
20+
21+
boolean done = false;
22+
while(!done && in.hasNextLine()){
23+
String line = in.nextLine();
24+
out.println("Echo: " + line);
25+
if(line.trim().equals("bye")){
26+
done = true;
27+
}
28+
}
29+
}finally{
30+
inSocket.close();
31+
}
32+
}catch(IOException e){
33+
e.printStackTrace();
34+
}
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.colin.test.net;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.colin.test.thread.producer;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.ArrayBlockingQueue;
5+
import java.lang.Runnable;
6+
import java.lang.InterruptedException;
7+
8+
9+
class Producer implements Runnable{
10+
private static final int COUNT = 100;
11+
private final BlockingQueue<String> queue;
12+
private final String seed;
13+
private int id;
14+
15+
Producer(BlockingQueue<String> queue, String seed){
16+
this.queue = queue;
17+
this.seed = seed;
18+
this.id = 0;
19+
}
20+
21+
public void run(){
22+
try{
23+
for(int i = 0; i<COUNT; i++){
24+
queue.put(produce());
25+
}
26+
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
private String produce(){
33+
id++;
34+
return seed+"id:"+id;
35+
}
36+
}
37+
38+
class Consumer implements Runnable {
39+
private final BlockingQueue<String> queue;
40+
41+
Consumer(BlockingQueue<String> queue){
42+
this.queue = queue;
43+
}
44+
45+
public void run(){
46+
try{
47+
while(true){
48+
consume(queue.take());
49+
}
50+
} catch (InterruptedException e){
51+
e.printStackTrace();
52+
}
53+
54+
}
55+
56+
private void consume(String seed){
57+
System.out.println("conssume : " + seed);
58+
}
59+
}
60+
61+
public class ProducerConsumer{
62+
public static void main(String[] args){
63+
BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
64+
Producer p1 = new Producer(queue,"p1 ");
65+
Producer p2 = new Producer(queue,"p2 ");
66+
Consumer c1 = new Consumer(queue);
67+
Consumer c2 = new Consumer(queue);
68+
new Thread(p1).start();
69+
new Thread(p2).start();
70+
new Thread(c1).start();
71+
new Thread(c2).start();
72+
System.out.println("OK");
73+
}
74+
}

0 commit comments

Comments
 (0)