www.fatihkabakci.com

Personal Website and Computer Science TUR EN

JAVA CONCURRENT CYLICBARRIER

Last update: 11/9/2014 1:26:00 AM

Yazan:Fatih KABAKCI

CylicBarrier, belirli sayıdaki thread' lerin tümünün çalışması bitene kadar, bu thread'leri , cyclic barrier olarak adlandırılan bir noktada, bekleten eş zamanlı nesnelerdir. İşini bitiren her thread, await() metodunu çağırarak, aynı bariyere ait diğer thread' leri beklemeye başlar. Toplamdaki tüm thread' lerin beklemesi, program kilidini bariyer noktasında açması anlamına gelir.

CyclicBarrier sınıfının, beklenecek thread sayısı ve bariyer noktasında çalıştırılacak Runnable sınıflarını belirten kurucu metotları aşağıda verilmektedir.

public CyclicBarrier(int paramInt)
public CyclicBarrier(int paramInt, Runnable paramRunnable)

CylicBarrier nesnesini, bir grup arkadaştan birisinin; ' herkes işini bitirdikten sonra şu köşede buluşalım ! ' demesi gibi düşünebilirsiniz. Gerçekten de işini bitiren her thread await() ile buluşmanın ayarlandığı noktada diğerlerini beklemektedir. Konuyu bir örnek ile anlamaya çalışalım.

Aşağıda 4 farklı bölgede(Güney, Kuzey, Doğu, Batı) yapılacak anket süreci verilmektedir.

Pollster Sınıfı

package Concurrent.CyclicBarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author www.fatihkabakci.com
 */
public class Pollster implements Runnable {

   CyclicBarrier area;
   String        name;

   public Pollster(CyclicBarrier area, String name) {
      this.area = area;
      this.name = name;
      new Thread(this).start();
   }

   @Override
   public void run() {
      System.out.println("The survey results of " + name);
      double result = Math.abs(new Random().nextGaussian());
      try {
         area.await();
         System.out.println("Announced " + result + "% by " + name);
      }      
      catch (InterruptedException e) {
         e.printStackTrace();
      }
      catch (BrokenBarrierException e) {
         e.printStackTrace();
      }
   }
}

Announcement Sınıfı

package Concurrent.CyclicBarrier;

/**
 * @author www.fatihkabakci.com
 */
public class Announcement implements Runnable {

   @Override
   public void run() {
      System.out.println("The results of survey were announced !");
   }
}

Survey - Main Sınıfı

package Concurrent.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author www.fatihkabakci.com
 * This class shows that how CyclicBarrier is implemented
 */
public class Survey {
   
   public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
      CyclicBarrier area = new CyclicBarrier(4, new Announcement());

      new Pollster(area, "South");
      new Pollster(area, "West");
      new Pollster(area, "East");
      new Pollster(area, "North");
   }
}

Yukarıda program şu mantıkla çalışır:

Bölgelere göre 4 adet anketör oluşturulmuş olup, her bir bölgeye aynı bariyer nesnesi atanmıştır. Bölge anketörleri sonuçları hesapladıktan sonra await() ile diğer anketörleri bariyer noktasında bekler. Tüm anketörler işini bitirdikten sonra, Announcement thread' i bir kez çalışır ve akabinde tüm bölge anketörleri buluşma noktasında sonuçları ilan eder.

CyclicBarrier nesneleri bir grup thread' in çalışmasını belirli bir bariyer noktasında bekleterek program senkronizasyonunu sağlar. CountDownLatch Sınıfı ile benzetilebilir ama çalışma algoritması farklıdır. CountDownLatch Sınıfı bir sayma sayacı ile belirtilen olay sayısı arasında bağlantı kurar ve program çalışmasını bekleme noktasından devam ettirir. CyclicBarrier ise bir grup thread' in tümünün çalışmasının bitmesini bekler ve program çalışmasını yine thread' lerin belirlediği bir bekleme noktasından devam ettirir.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.