annotate src/main/java/jp/ac/u_ryukyu/treevnc/MulticastQueue.java @ 76:ef3586035d25

minor change
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 29 Apr 2014 13:30:39 +0900
parents f1938dd3b518
children f6946d4fe926
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
12c3a73be47f rename package
one
parents: 4
diff changeset
1 package jp.ac.u_ryukyu.treevnc;
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
3 import java.nio.ByteBuffer;
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import java.util.concurrent.CountDownLatch;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 public class MulticastQueue<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 Node<T> tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 public MulticastQueue()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 tail = new Node<T>(null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 }
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
15
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
16 /**
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
17 * @param size
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
18 * @return
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
19 *
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
20 * try to allocate byteBuffer.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
21 * wait until heap is available.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
22 *
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
23 *
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
24 */
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
25 public ByteBuffer allocate(int size)
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
26 {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
27 ByteBuffer b=null;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
28 while(true){
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
29 try {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
30 b = ByteBuffer.allocate(size);
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
31 } catch (OutOfMemoryError e) {
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
32 b = null;
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
33 System.err.println("multicastqueue : wait for heap : " + e);
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
34 }
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
35 if (b!=null) {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
36 break;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
37 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
38 try {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
39 wait();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
40 } catch (InterruptedException e) {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
41 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
42 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
43 return b;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
44 }
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
46 public synchronized void heapAvailable() {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
47 notifyAll();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
48 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
49
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 public synchronized void put(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 Node<T> next = new Node<T>(item);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 tail.set(next);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 tail = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 public Client<T> newClient()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 return new Client<T>(tail);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
4
b32668b8e83c create multicast function
one
parents: 3
diff changeset
62 public static class Client<T>
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 Node<T> node;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 Client(Node<T> tail)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 node = tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 synchronized public T poll()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 Node<T> next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 T item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 do {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 try {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 next = node.next();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 }catch(InterruptedException _e){
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 continue;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 item = next.getItem();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 node = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 } while ( item == null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 return item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 static class Node<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 private T item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 private Node<T> next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 private CountDownLatch latch;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 public Node(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 this.item = item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 this.next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 latch = new CountDownLatch(1);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 synchronized public T getItem() {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 return item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 public void set(Node<T> next)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 this.next = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 latch.countDown();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 public Node<T> next() throws InterruptedException
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 latch.await();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 return next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 synchronized public void clear() {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }