forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBuffer.java
More file actions
Latest commit
124 lines (105 loc) · 3.56 KB
/
Copy pathCircularBuffer.java
File metadata and controls
124 lines (105 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
importjava.util.Random;
importjava.util.concurrent.atomic.AtomicInteger;
publicclassCircularBuffer {
privatechar[] _buffer;
publicfinalint_buffer_size;
privateint_write_index = 0;
privateint_read_index = 0;
privateAtomicInteger_readable_data = newAtomicInteger(0);
publicCircularBuffer(intbuffer_size) {
if(!IsPowerOfTwo(buffer_size)) {
thrownewIllegalArgumentException();
}
this._buffer_size = buffer_size;
_buffer = newchar[buffer_size];
}
privatebooleanIsPowerOfTwo(inti) {
return (i & (i - 1)) == 0;
}
privateintgetTrueIndex(inti) {
returni % _buffer_size;
}
publicCharacterreadOutChar() {
Characterresult = null;
//if we have data to read
if(_readable_data.get() > 0) {
result = newCharacter(_buffer[getTrueIndex(_read_index)]);
_readable_data.decrementAndGet();
_read_index++;
}
returnresult;
}
publicbooleanwriteToCharBuffer(charc) {
booleanresult = false;
//if we can write to the buffer
if(_readable_data.get() < _buffer_size) {
//write to buffer
_buffer[getTrueIndex(_write_index)] = c;
_readable_data.incrementAndGet();
_write_index++;
result = true;
}
returnresult;
}
privatestaticclassTestWriteWorkerimplementsRunnable {
String_alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
Random_random = newRandom();
CircularBuffer_buffer;
publicTestWriteWorker(CircularBuffercb) {
this._buffer = cb;
}
privatechargetRandomChar() {
return_alphabet.charAt(_random.nextInt(_alphabet.length()));
}
publicvoidrun() {
while(!Thread.interrupted()) {
if(!_buffer.writeToCharBuffer(getRandomChar())){
Thread.yield();
try{
Thread.sleep(10);
} catch (InterruptedExceptione) {
return;
}
}
}
}
}
privatestaticclassTestReadWorkerimplementsRunnable {
CircularBuffer_buffer;
publicTestReadWorker(CircularBuffercb) {
this._buffer = cb;
}
publicvoidrun() {
System.out.println("Printing Buffer:");
while(!Thread.interrupted()) {
Characterc = _buffer.readOutChar();
if(c != null) {
System.out.print(c.charValue());
} else {
Thread.yield();
try {
Thread.sleep(10);
} catch (InterruptedExceptione) {
System.out.println();
return;
}
}
}
}
}
publicstaticvoidmain(String[] args) throwsInterruptedException {
intbuffer_size = 1024;
//create circular buffer
CircularBuffercb = newCircularBuffer(buffer_size);
//create threads that read and write the buffer.
Threadwrite_thread = newThread(newTestWriteWorker(cb));
Threadread_thread = newThread(newTestReadWorker(cb));
read_thread.start();
write_thread.start();
//wait some amount of time
Thread.sleep(10000);
//interrupt threads and exit
write_thread.interrupt();
read_thread.interrupt();
}
}