Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday 4 June 2017

Java Thread Join

 

Thread Join


Java Thread Join

In java, The join() method of thread class can be used to pause the current thread execution until the specified thread is dead or terminate.

Syntax :

The syntax of join method in java multi-threading(thread) concept.
  • public void join()throws InterruptedException 
  • public void join(long milliseconds)throws InterruptedException
Here above we defined some join methods which throw an exceptions

Let's discuss join method in java thread in detail with example

Java Thread Join Example

In this thread join example we are going to use join() method of thread class so that we can easily join one thread with another thread(after the thread execution is complete).Let's see example

class ThreadJoinExample extends Thread
{
public void run()
{
for(int i = 0; i<=5; i++)
{
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[])
{
ThreadJoinExample t1 = new ThreadJoinExample();
ThreadJoinExample t2 = new ThreadJoinExample();
ThreadJoinExample t3 = new ThreadJoinExample();
ThreadJoinExample t4 = new ThreadJoinExample();

//Here starting first thread t1
t1.start();
try
{
t1.join();//apply join() method to the first thread t1
}
catch(Exception e)
{
System.out.println(e);
}
//now we starting remaining threads
t2.start();
t3.start();
t4.start();
}
}

output : 0
              1
              2
              3
              4
              5
              0
              0
              0
              1
              1
              1
              2
              2
              2
              3
              3
              3
              4
              4
              4
              5
              5
              5

As you can see in the above example, when t1 thread execution is finished then remaining thread t2, t3, t4 start executing. Here in this above example there are total 4 thread created.



Java Thread Join Example 2

This is another example of thread join(long milliseconds) method in java. In this example we will pass some value in join method. Let's start


class ThreadJoinExample2 extends Thread

{

public void run()

{

for(int i = 0; i<=5; i++)

{
try

{

Thread.sleep(500);

}

catch(Exception e)

{

System.out.println(e);

}

System.out.println(i);

}

}

public static void main(String args[])
{
ThreadJoinExample2 t1 = new ThreadJoinExample2();
ThreadJoinExample2 t2 = new ThreadJoinExample2();
ThreadJoinExample2 t3 = new ThreadJoinExample2();
ThreadJoinExample2 t4 = new ThreadJoinExample2();

//Here starting first thread t1
t1.start();
try
{
t1.join(1000);//apply join method to the first thread t1
}
catch(Exception e)
{
System.out.println(e);
}
//now we starting remaining threads
t2.start();
t3.start();
t4.start();
}
}

output : 0
              1
              2
              0   
              0 
              0
              1
              1
              1
              3
              2
              4
              2
              2
              3
              3
              5
              3
              4
              4
              4
              5
              5
              5

In the above example you can see when t1 thread completes its task for 1500 milliseconds(3 times) then remaining thread i.e t2, t3, t4 starts executing.


Java Thread Priority

Priority of a thread are a represented by a number between 1 to 10. In java thread, each thread have a priority.

Java thread schedular schedules  the thread according to their priorities.

There are 3 constants in Thread class, These are
  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY
The value of Minimum priority of a thread is 1 
The value of Maximum priority of a thread is 10, 
Normal priority of a thread is 5.(default priority)


Example of Java Thread Priority

This is an example of thread priority, Where we will print the thread's name and thread's priority. We also set the priority of thread.

class ThreadPriorityExample extends Thread
{
public void run()
{
System.out.println(" running thread name "+Thread.currentThread().getName());
System.out.println("running thread priority "+Thread.currentThread().getPriority());
}

public static void main(String args[])
{
ThreadPriorityExample t1 = new ThreadPriorityExample();
ThreadPriorityExample t2 = new ThreadPriorityExample();

//Now we set the priority of thread
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}

output : running thread name thread-1
              running thread priority 10
              running thread name thread-0
              running thread priority 1



Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate