java lock condition example


public class FileMock {
private String content[];
private int index;

public FileMock(int size, int length){
content = new String[size];
for(int i=0; i<size; i++){
StringBuilder buffer = new StringBuilder(length);
for(int j=0; j<length; j++){
int indice = (int)Math.random() * 255;
buffer.append((char)indice);
}
content[i] = buffer.toString();
}
index = 0;
}

public boolean hasMoreLines(){
return index < content.length;
}

public String getLine(){
if(this.hasMoreLines()){
System.out.println("Mock: " + (content.length-index));
return content[index++];
}

return null;
}
}



import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
/**
* Created by Administrator.
*/
public class Buffer {
private LinkedList<String> buffer;
private int maxSize;
private ReentrantLock lock;
private Condition lines;
private Condition space;
private boolean pendingLines;

public Buffer(int maxSize){
this.maxSize = maxSize;
buffer = new LinkedList<String>();
lock = new ReentrantLock();
lines = lock.newCondition();
space = lock.newCondition();

pendingLines = true;
}

public void insert(String line){
lock.lock();
try{
while(buffer.size() == maxSize){
space.await();
}

buffer.offer(line);
System.out.printf("%s: Inserted Line: %d\n", Thread.currentThread().getName(), buffer.size());
lines.signalAll();
}catch(InterruptedException e){
e.printStackTrace();
}finally{
lock.unlock();
}
}

public String get(){
String line = null;
lock.lock();
try{
while((buffer.size() == 0) && (hasPendingLines())){
lines.await();
}

if(hasPendingLines()){
line = buffer.poll();
System.out.printf("%s: Line Readed: %d\n", Thread.currentThread().getName(), buffer.size());
space.signalAll();
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
lock.unlock();
}
return line;
}

public void setPendingLines(boolean pendingLines){
this.pendingLines = pendingLines;
}

public boolean hasPendingLines(){
return pendingLines || buffer.size() > 0;
}
}



public class Producer implements Runnable {
private FileMock mock;
private Buffer buffer;

public Producer(FileMock mock, Buffer buffer){
this.mock = mock;
this.buffer = buffer;
}

@Override
public void run(){
buffer.setPendingLines(true);
while(mock.hasMoreLines()){
String line = mock.getLine();
buffer.insert(line);
}
buffer.setPendingLines(false);
}
}



import java.util.Random;

/**
* Created by Administrator.
*/
public class Consumer implements Runnable {
private Buffer buffer;
public Consumer(Buffer buffer){
this.buffer = buffer;
}

@Override
public void run(){
while(buffer.hasPendingLines()){
String line = buffer.get();
processLine(line);
}
}

private void processLine(String line){
try{
Random random = new Random();
Thread.sleep(random.nextInt(100));
}catch(InterruptedException e){
e.printStackTrace();
}
}
}



public class Main {
public static void main(String[] args){
FileMock mock = new FileMock(100, 10);
Buffer buffer = new Buffer(20);

Producer producer = new Producer(mock, buffer);
Thread threadProducer = new Thread(producer, "Producer");

Consumer consumers[] = new Consumer[3];
Thread threadConsumers[] = new Thread[3];
for(int i=0; i<3; i++){
consumers[i] = new Consumer(buffer);
threadConsumers[i] = new Thread(consumers[i], "Consumer " + i);
}

threadProducer.start();
for(int i=0; i<3; i++){
threadConsumers[i].start();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值