• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

J2EE

CountDownLatch in java

April 24, 2017 By j2eereference 3 Comments

CountDownLatch is a java synchronizer that comes under java.util.concurrent package and is available in java from jdk 1.5.

It is used in the scenario where multiple threads are working together and one or more threads need to wait until a set of task is finished by other threads.

Important methods used
await() ; The current thread which called this method will go into waiting state and will wait until the count becomes 0.

CountDown() ;This method when called will decrements the count and when count becomes 0, the current thread will notify the thread which called await() on the same CountDownLatch instance.

getCount() : This method will return the current count.

How it works:

When we create an object of CountDownLatch, we pass an int value  to its constructor which  specify the number of threads it should wait for, all the thread are required to decrement this count by calling countDown() . As soon as count becomes zero, the waiting thread on which await method is called completes its waiting time and resumes further processing.

Example application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.concurrent.CountDownLatch;
 
public class CountDownLatchExample{
public static void main(String args[]) throws InterruptedException
{
CountDownLatch latch = new CountDownLatch(3);
System.out.println("Initial count is :"+latch.getCount());
// Let us create three worker threads (subTask-1,subtask-2 and subTask-3)and start them.
for(int i=0;i<=2;i++){
new Thread(new MyRunnableDemo(latch),"subTask-"+i).start();
}
 
// The main task wait for three threads
latch.await();
System.out.println("count after completing all subtasks is :"+latch.getCount());
// Main thread has started
System.out.println("All sub tasks are finished and the main thread is starting now ...");
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() +" thread has finished");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.concurrent.CountDownLatch;
 
class MyRunnableDemo implements Runnable
{
private CountDownLatch latch;
public MyRunnableDemo(CountDownLatch latch)
{
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" finished the task");
latch.countDown();
}
}

OutPut :

Initial count is :3
subTask-0 finished the task
subTask-1 finished the task
subTask-2 finished the task
count after completing all subtasks is :0
All sub tasks are finished and the main thread is starting now …
main thread has finished

Real-time use cases

  • In an amusement park, there are certain rides in which it is mandate to have atleast 2 people (2 is count) to share seat in order to take that ride. So, ride keeper waits for 2 persons for each seat so that he can start the ride.
  • Suppose a big heavy rectangular box can be lifted by 4 people from four corners, so you will wait for all 4 to come then only you can lift that heavy box.

 

 

Related Posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof

Filed Under: Core Java Tagged With: countDownLatch example, J2EE, java countDownLatch

Java EE or J2EE Architecture

April 17, 2017 By j2eereference Leave a Comment

What is Java EE?

Java EE is the short form of Java Enterprise Edition. Intention of Java EE is to offer an enterprise application development platform containing influential APIs to the developers thereby reducing the development time, lessening complexity of the application, and refining performance of application. Java Enterprise Edition is developed with the help of Java Community Process, which is held accountable for all the Java technologies.

Why the name J2EE?

When Java EE was introduced, its first significant release was termed as Java 2 Enterprise Edition, in short J2EE. Though several versions of Java EE are in place, predominantly Java EE is still called as J2EE.

Latest Version of Java EE

Latest version of Java EE is Java EE 7, which was introduced on 12th June 2013. However, most awaited stable release of Java EE 8 is expected to happen soon within the end of this year 2017.

J2EE / Java EE Architecture

Java EE provides an environment for development and deployment of web-based enterprise applications using multi-tier architecture, as signified below:

The diagram shown above demonstrates J2EE multi-tier architecture that encompasses several J2EE containers each including its own J2EE components. Here is an overview about the different tiers of J2EE architecture:

  • Client Tier – Components of Client Tier will run in the client devices / containers. Client Tier components are standalone java applications, static and dynamic HTML pages, and applets.

  • Middle Tier
    • Web Tier – The web tier components namely JSP’s and Servlets execute with the help of J2EE web server in a web container.

    • Business Tier – Enterprise Java Beans (EJB) are the business tier components that are executed within the EJB container using J2EE Application Server.

  • EIS Tier – In the EIS tier, the application related data are stored in a database. EIS tier may also include ERP’s or legacy systems that can be accessed via an API representing Java EE Connector Architecture.

Related Posts

  • Differences between Point to Point Messaging Model and Publish Subscribe Messaging Model
  • Point to Point Messaging Model Architecture
  • Publish Subscribe Messaging Model Architecture
  • How JMS is different from RPC?
  • Different types of messages available in JMS API
  • Advantages of Java Message Service (JMS)
  • Java Message Service and JMS Programming Model
  • What is JavaServer Pages (JSP)?

Filed Under: J2EE Tagged With: applet, client tier, EIS tier, EJB, enterprise application, HTML, J2EE, J2EE architecture, Java EE, Java EE 7, Java EE 8, Java Enterprise Edition, JSP, middle tier, multi-tier architecture, servlets, web tier

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.