import java.util.List; import java.util.ArrayList; import java.util.Queue; import java.util.LinkedList; public class Test3 { public static void main(String[] args) { final Manager3 m3 = new Manager3(); m3.init(); Thread t1 = new Thread(new Runnable() { public void run() { while (true) { if (m3.getter.q.size() > 0) { TestData3 td = m3.getter.q.pop(); System.out.println(td.getName()); } else { try {wait();} catch (Exception e) {} } } } }); t1.start(); } } class TestData3 { String type; String id; String name; TestData3(String type, String id, String name) { this.type = type; this.id = id; this.name = name; } String getName() { return name; } } class TestDataQueue3 { Queue<TestData3> q = new LinkedList<TestData3>(); synchronized void push(TestData3 td) { q.offer(td); } synchronized TestData3 pop() { return q.poll(); } long size() { return q.size(); } } class Manager3 { Getter3 getter = new Getter3(); Setter3 setter = new Setter3(); Adapter3 adapter = Setter3.adapter; void init() { adapter.addGetter(getter); setter.regist(); setter.putData(); adapter.listenData(); } } class Getter3 { TestDataQueue3 q = new TestDataQueue3(); void update(TestData3 data) { q.push(data); } } class Adapter3 { TestDataQueue3 q = new TestDataQueue3(); List<Getter3> glst = new ArrayList<Getter3>(); List<Setter3> slst = new ArrayList<Setter3>(); void addGetter(Getter3 g) { glst.add(g); } void addSetter(Setter3 s) { slst.add(s); } void listenData() { Thread t = new Thread(new Runnable() { public void run() { while (true) { if (q.size() > 0) { broadcast(q.pop()); } else { try { wait(); } catch (Exception e) {} } } } }); t.start(); } void broadcast(TestData3 td) { for (int i = 0; i < glst.size(); i++) { glst.get(i).update(td); } } } class Setter3 { static final Adapter3 adapter = new Adapter3(); void regist() { adapter.addSetter(this); } void putData() { Thread t = new Thread(new Runnable() { public void run() { int i = 0; while (true) { TestData3 td = new TestData3("computer", "c" + String.format("%08d", i), "computer" + i); adapter.q.push(td); i++; try { Thread.sleep(1000); notify(); } catch (Exception e) {} } } }); t.start(); } }