comparison src/test/MultiThreadTee.java @ 53:9250cacee347

MultiThreadTee
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 07 Jul 2011 22:29:53 +0900
parents 089bd4510538
children a240b19b66f0
comparison
equal deleted inserted replaced
52:6eb7d0c8f11d 53:9250cacee347
1 package test; 1 package test;
2 2
3 import java.util.LinkedList; 3 import java.util.LinkedList;
4 import java.util.concurrent.BlockingQueue;
5 import java.util.concurrent.LinkedBlockingQueue;
4 import java.util.concurrent.SynchronousQueue; 6 import java.util.concurrent.SynchronousQueue;
5 7
6 public class MultiThreadTee { 8 public class MultiThreadTee {
7 9
8 static class Parent extends Thread { 10 static class Parent extends Thread {
9 LinkedList<SynchronousQueue<String>> clients = new LinkedList<SynchronousQueue<String>>(); 11 LinkedList<BlockingQueue<String>> clients = new LinkedList<BlockingQueue<String>>();
10 12
11 13
12 SynchronousQueue<String> accept() { 14 BlockingQueue<String> accept() {
13 15
14 SynchronousQueue<String> s = new SynchronousQueue<String>(); 16 // BlockingQueue<String> s = new LinkedBlockingQueue<String>();
17 BlockingQueue<String> s = new SynchronousQueue<String>();
15 clients.add(s); 18 clients.add(s);
16 19
17 return s; 20 return s;
18 21
19 22
20 } 23 }
21 24
22 void put(String s) throws InterruptedException { 25 void put(String s) throws InterruptedException {
23 for (SynchronousQueue<String> queue : clients) { 26 for (BlockingQueue<String> queue : clients) {
24 queue.put(s); 27 queue.put(s);
25 28
26 } 29 }
27 30
28 } 31 }
31 34
32 35
33 36
34 static class Client extends Thread { 37 static class Client extends Thread {
35 Parent p; 38 Parent p;
36 private SynchronousQueue<String> queue; 39 private BlockingQueue<String> queue;
37 40
38 41
39 42
40 43
41 public Client(Parent p2) { 44 public Client(Parent p2) {
46 queue = p.accept(); 49 queue = p.accept();
47 50
48 String item; 51 String item;
49 // while(!(item = queue.poll()).equals("") ) { 52 // while(!(item = queue.poll()).equals("") ) {
50 while(true) { 53 while(true) {
51 item = queue.poll(); 54 try {
52 if(item == null) continue; 55 item = queue.take();
56 } catch (InterruptedException e) {
57 // TODO Auto-generated catch block
58 // System.out.println("wating");
59 continue;
60 }
61 // if(item == null) continue;
53 if(item.equals("")) return; 62 if(item.equals("")) return;
54 System.out.println(item); 63 System.out.println(item);
55 } 64 }
56 } 65 }
57 66