Java CyclicBarrier示例

时间:2020-02-23 14:34:59  来源:igfitidea点击:

在本教程中,我们将看到Java中的CyclicBarrier。
CyclicBarrier在Java 5中引入了Java 5,其他并发utils,如CountDownLatch,ConcurrentHashMap和BlockingQueue。

CyclicBarrier是同步的辅助辅助辅助装置,其允许在公共屏障点处彼此等待彼此等待。
它被称为循环,因为一旦释放等待线程就可以重复使用。

例如:让我们说你有3个线程,你想要所有线程(作为派对的条款)来达到一个共同点,然后只有他们应该继续前进。
在这种情况下,我们可以使用带有3个派对的CyclicBarrier,并且一次3个线程达到常见点,我们可以调用将执行运行接口的事件,并且将释放三个线程。

CountDownLatch和CyclicBarrier之间的差异

CyclicBarrier和CoundDownlatch之间的主要区别是CyclicBarrier可以重复使用。
我们不能使用次数使用CountDownLatch。
我们可以阅读CountDownLatch和CyclicBarrier之间的更多差异。

Java CyclicBarrier示例:

第1步:在Package .src.org.igi.onitoad中创建名为"runnableTask.java"的文件

package org.igi.theitroad;
 
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
 
public class RunnableTask implements Runnable{
 
	CyclicBarrier cyclicBarrier;
	long sleepTime;
 
	RunnableTask(CyclicBarrier cyclicBarrier,long sleepTime){
		this.cyclicBarrier=cyclicBarrier;
		this.sleepTime=sleepTime;
	}
 
	@Override
	public void run() {
 
		try {
			Thread.sleep(sleepTime);
			System.out.println(Thread.currentThread().getName() +
					" is waiting for "+(cyclicBarrier.getParties()-cyclicBarrier.getNumberWaiting()-1)+ 
					" other threads to reach common barrier point");
			/*
			 * when  3 parties will call await() method (i.e. common barrier point)
			 * CyclicBarrrierEvent will be triggered and all waiting threads will be released.
			 */
			cyclicBarrier.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}          
 
		System.out.println("As "+cyclicBarrier.getParties()+ " threads have reached common barrier point "
				+ Thread.currentThread().getName() +
				" has been released");
	}
 
}

这是一个可追踪任务,它将被每个线程执行。
第2步:在Package .src.org.igi.theitroad中创建名为"cyclicbarrierfinishevent.java"的文件

package org.igi.theitroad;
 
public class CyclicBarrierFinishEvent implements Runnable{
 
    public void run() {
    	
           System.out.println("As 3 threads have reached common barrier point "
                        + ", CyclicBarrrierFinishEvent has been triggered");
           System.out.println("You can update shared variables if any");
    }
    
}

当3派对(用CyclicBarrier对象初始化)达到共同障碍点时,将调用CyclicBarrierFinishEvent。
第3步:在包中创建名为"cyclicbarriermain.java"的文件.Onitread.theitroad

package org.igi.theitroad;
 
import java.util.concurrent.CyclicBarrier;
 
/** @author igi Mandliya*/
public class CyclicBarrierMain {
	public static void main(String[] args) {
		/*
		 * Create CountDownLatch with 3 parties, when all 3 parties
		 * will reach common barrier point CyclicBarrrierFinishEvent will be
		 * triggered 
		 */
		CyclicBarrier cyclicBarrier=new CyclicBarrier(3 ,new CyclicBarrierFinishEvent());
	
		RunnableTask runnableTask1=new RunnableTask(cyclicBarrier,1000);
		RunnableTask runnableTask2=new RunnableTask(cyclicBarrier,2000);
		RunnableTask runnableTask3=new RunnableTask(cyclicBarrier,3000);
 
 
		//Create and start 3 threads
		new Thread(runnableTask1,"Thread-1").start();
		new Thread(runnableTask2,"Thread-2").start();
		new Thread(runnableTask3,"Thread-3").start();
    
        /*
         * We are reusing cyclic barrier using below threads
         * */
		RunnableTask runnableTask4=new RunnableTask(cyclicBarrier,4000);
		RunnableTask runnableTask5=new RunnableTask(cyclicBarrier,5000);
		RunnableTask runnableTask6=new RunnableTask(cyclicBarrier,6000);
		
		//Create and start 3 more threads
		new Thread(runnableTask4,"Thread-4").start();
		new Thread(runnableTask5,"Thread-5").start();
		new Thread(runnableTask6,"Thread-6").start();
 
 
	}
 
}

让我们运行该程序,然后我们将得到输出:

Thread-1 is waiting for 2 other threads to reach common barrier point
Thread-2 is waiting for 1 other threads to reach common barrier point
Thread-3 is waiting for 0 other threads to reach common barrier point
As 3 threads have reached common barrier point ,CyclicBarrrierFinishEvent has been triggered
You can update shared variables if any
As 3 threads have reached common barrier point Thread-3 has been released
As 3 threads have reached common barrier point Thread-1 has been released
As 3 threads have reached common barrier point Thread-2 has been released
Thread-4 is waiting for 2 other threads to reach common barrier point
Thread-5 is waiting for 1 other threads to reach common barrier point
Thread-6 is waiting for 0 other threads to reach common barrier point
As 3 threads have reached common barrier point ,CyclicBarrrierFinishEvent has been triggered
You can update shared variables if any
As 3 threads have reached common barrier point Thread-6 has been released
As 3 threads have reached common barrier point Thread-4 has been released
As 3 threads have reached common barrier point Thread-5 has been released