mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-15 01:40:49 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
package DataStructures.Buffers;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@ -9,7 +11,7 @@ public class CircularBuffer {
|
||||
private AtomicInteger _readable_data = new AtomicInteger(0);
|
||||
|
||||
public CircularBuffer(int buffer_size) {
|
||||
if(!IsPowerOfTwo(buffer_size)) {
|
||||
if (!IsPowerOfTwo(buffer_size)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
this._buffer_size = buffer_size;
|
||||
@ -28,7 +30,7 @@ public class CircularBuffer {
|
||||
Character result = null;
|
||||
|
||||
//if we have data to read
|
||||
if(_readable_data.get() > 0) {
|
||||
if (_readable_data.get() > 0) {
|
||||
result = new Character(_buffer[getTrueIndex(_read_index)]);
|
||||
_readable_data.decrementAndGet();
|
||||
_read_index++;
|
||||
@ -41,7 +43,7 @@ public class CircularBuffer {
|
||||
boolean result = false;
|
||||
|
||||
//if we can write to the buffer
|
||||
if(_readable_data.get() < _buffer_size) {
|
||||
if (_readable_data.get() < _buffer_size) {
|
||||
//write to buffer
|
||||
_buffer[getTrueIndex(_write_index)] = c;
|
||||
_readable_data.incrementAndGet();
|
||||
@ -56,6 +58,7 @@ public class CircularBuffer {
|
||||
String _alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
Random _random = new Random();
|
||||
CircularBuffer _buffer;
|
||||
|
||||
public TestWriteWorker(CircularBuffer cb) {
|
||||
this._buffer = cb;
|
||||
}
|
||||
@ -65,10 +68,10 @@ public class CircularBuffer {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while(!Thread.interrupted()) {
|
||||
if(!_buffer.writeToCharBuffer(getRandomChar())){
|
||||
while (!Thread.interrupted()) {
|
||||
if (!_buffer.writeToCharBuffer(getRandomChar())) {
|
||||
Thread.yield();
|
||||
try{
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
@ -80,15 +83,17 @@ public class CircularBuffer {
|
||||
|
||||
private static class TestReadWorker implements Runnable {
|
||||
CircularBuffer _buffer;
|
||||
|
||||
public TestReadWorker(CircularBuffer cb) {
|
||||
this._buffer = cb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Printing Buffer:");
|
||||
while(!Thread.interrupted()) {
|
||||
while (!Thread.interrupted()) {
|
||||
Character c = _buffer.readOutChar();
|
||||
if(c != null) {
|
||||
if (c != null) {
|
||||
System.out.print(c.charValue());
|
||||
} else {
|
||||
Thread.yield();
|
||||
|
Reference in New Issue
Block a user