1
package org.jboss.remoting.samples.chat.utility;
6
* <p>Copyright: Copyright (c) 2003</p>
8
* @author not attributable
15
public class ReadWriteArrayList implements Serializable
17
private ArrayList arrayList;
18
private static final long serialVersionUID = 6;
19
private Gate gate = new Gate();
21
public ReadWriteArrayList()
23
arrayList = new ArrayList();
26
public boolean add(Object o)
34
public Object get(int i)
37
Object o = arrayList.get(i);
42
public ArrayList copy()
44
return (ArrayList) subList(0, arrayList.size());
47
public List subList(int from, int to)
49
gate.enterRead(to - 1);
50
List list = new ArrayList(arrayList.subList(from, to));
55
public Object firstElement()
60
public Object remove(int i)
63
Object o = arrayList.remove(i);
70
return arrayList.size();
73
public Object[] toArray()
75
return arrayList.toArray();
78
public Object[] toArray(Object[] a)
80
return arrayList.toArray(a);
83
public ArrayList toArrayList()
89
////////////////////////////////////////////////////////////////////////////////
90
class Gate implements Serializable
92
private static final long serialVersionUID = 7;
94
private int numberOfReaders;
95
private int numberOfWriters;
97
public synchronized void enterRead(int i)
99
while (numberOfWriters > 0 || i > arrayList.size() - 1)
104
catch (InterruptedException ie) { }
110
public synchronized void enterWrite()
112
while (numberOfWriters > 0 || numberOfReaders > 0)
117
catch (InterruptedException ie) { }
123
public synchronized void leaveRead()
125
if (numberOfReaders <= 0)
126
throw new Error("number of readers <= 0");
132
public synchronized void leaveWrite()
134
if (numberOfWriters <= 0)
135
throw new Error("number of writers <= 0");
141
////////////////////////////////////////////////////////////////////////////////
b'\\ No newline at end of file'