Code
package jtrCyclicBarrierTest;
import java.util.concurrent.CyclicBarrier;
//public class jtrCyclicBarrierTest { }
public class jtrCyclicBarrierTest {
public static void main(String[] args) {
final CyclicBarrier cb = new CyclicBarrier(3, new Runnable()
{
public void run() {
System.out.println("Commit Operation - Completed");
}
});
new Thread() {
@Override
public void run() {
try{sleep(1000);}catch(Exception e){}
System.out.println("T1 pre-commit");
try
{
cb.await();
} catch (Exception e)
{
//do something commit operation failed
}
System.out.println("T1 post-commit");
}
}.start();
new Thread() {
@Override
public void run() {
try{sleep(5000);}catch(Exception e){}
System.out.println("T2 pre-commit");
try
{
cb.await();
}catch (Exception e)
{
//do something commit operation failed
}
System.out.println("T2 post-commit");
}
}.start();
new Thread() {
@Override
public void run() {
try{sleep(2000);}catch(Exception e){}
System.out.println("T3 pre-commit");
try
{
cb.await();
}catch (Exception e)
{
//do something commit operation failed
}
System.out.println("T3 post-commit");
}
}.start();
}
}
Добавлено (2011-10-28, 20:32:30)
---------------------------------------------
Code
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WorkManager {
private CountDownLatch countDownLatch;
private static final int NUMBER_OF_TASKS = 5;
public WorkManager() {
countDownLatch = new CountDownLatch(NUMBER_OF_TASKS);
}
public void finishWork() {
try {
System.out.println("START WAITING");
countDownLatch.await();
System.out.println("DONE WAITING");
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public void startWork() {
ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_TASKS);
for (int i = 0; i < NUMBER_OF_TASKS; i++) {
Worker worker = new Worker(countDownLatch);
executorService.execute(worker);
}
executorService.shutdown();
}
public static void main(String[] args) {
WorkManager workManager = new WorkManager();
System.out.println("START WORK");
workManager.startWork();
System.out.println("WORK STARTED");
workManager.finishWork();
System.out.println("FINISHED WORK");
}
}
Code
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class Worker implements Runnable {
private CountDownLatch countDownLatch;
public Worker(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
Thread.sleep(getRandomSeconds()); // sleep random time to simulate long running task
System.out.println("Counting down: " + Thread.currentThread().getName());
this.countDownLatch.countDown();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
// returns a long between 0 and 9999
private long getRandomSeconds() {
Random generator = new Random();
return Math.abs(generator.nextLong() % 10000);
}
}
Добавлено (2011-10-28, 20:51:07)
---------------------------------------------
Code
package jtrBoundedBuffer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class jtrBoundedBuffer {
private final String[] buffer;
private final int capacity;
private int front;
private int rear;
private int count;
private final Lock lock = new ReentrantLock();
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
public jtrBoundedBuffer(int capacity) {
super();
this.capacity = capacity;
buffer = new String[capacity];
}
public void deposit(String data) throws InterruptedException {
lock.lock();
try {
while (count == capacity) {
notFull.await();
}
buffer[rear] = data;
rear = (rear + 1) % capacity;
count++;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public String fetch() throws InterruptedException {
lock.lock();
try {
while (count == 0) {
notEmpty.await();
}
String result = buffer[front];
front = (front + 1) % capacity;
count--;
notFull.signal();
return result;
} finally {
lock.unlock();
}
}
}