All Downloads are FREE. Search and download functionalities are using the official Maven repository.

blah.concurrency.first.Consumer Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package blah.concurrency.first;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class Consumer implements Runnable {

	final BlockingQueue data;
	final CountDownLatch counter;

	@Override
	public void run() {
		while( counter.getCount() > 0 ) {
			if ( takeData() )
				counter.countDown();
		}
	}

	boolean takeData() {
		try {
			return data.poll(1, TimeUnit.SECONDS) != null;
		} catch (InterruptedException e) {
			return false;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy