Day 11 – Threads part – 2

This is continuation of Threads-1 post.

 join(), yield(), sleep()

We can prevent or stop a Thread from execution by using join(), yield(), sleep() methods

yield()

yield() causes to pause current executing thread to give  chance for remaining waiting threads of same priority.

If there is no waiting thread or all waiting threads have a lesser priority then the same thread will continue its execution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MyThread extends Thread 
{
	public void run()
	{
		for (int i=0;i<10 ;i++ )
		{
			System.out.println("This is a new thread!");
		}
	}
}
class ThreadDemo7
{
	public static void main(String[] args)
	{
		MyThread t = new MyThread();
		t.start();
		Thread.currentThread().yield();
		for (int i=0;i<10 ;i++ )
		{
			System.out.println("This is from main!");
		}
	}
}

join()

If a thread wants to wait untill completion of other Thread, then we need to use join() method.

Eg: If a thread t1 want to wait until the completion of t2, then it has to call t2.join(). Once thread executes t2.join() then t1 will immediately enter into a waiting stage until t2 Thread completes.

Three variants of join method

  1. public final void join() throws InterruptedException
  2. public final void join(long ms) throws InterruptedException
  3. public final void join(long ms , int ns) throws InterruptedException

Example programs using join() method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//demonstration of join() 
class MyThread extends Thread 
{
	public void run()
	{
		for (int i=0;i<10 ;i++ )
		{
		 System.out.println("Seetha Thread!");
			try
			{			 
			  Thread.sleep(100);
			}
			catch (InterruptedException e)
			{  }			
		}
	}
}
class ThreadDemo8
{
	public static void main(String[] args) throws InterruptedException
	{
		MyThread t = new MyThread();
		t.start();
		t.join(); // Main thread will wait untill completion of t
		for (int i=0;i<10 ;i++ )
		{
			System.out.println("Rama Thread!");		  
			    Thread.sleep(100);
			}
		}
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Calling  join() on child thread 
class MyThread extends Thread 
{
	static Thread mt;
	public void run() 
	{
	    try
			{
			mt.join();		
			for (int i=0;i<10 ;i++ )
				{
				System.out.println("Child Thread!");		  
				 Thread.sleep(100);
				}
			}
		catch(InterruptedException e) 
			{		}
	} }	
class ThreadDemo9
{
	public static void main(String[] args) throws InterruptedException
	{
		MyThread.mt = Thread.currentThread();
		MyThread t = new MyThread();
		t.start();
		for (int i=0;i<10 ;i++ )
		{
			System.out.println("Main Thread!");		  
		    Thread.sleep(100);
			}
		}
	}

What happens when a thread calls join() on itself ??
Well, the program will be hunged 😛

sleep()
If we want a Thread to suspend it temporarily then we should call sleep() on it.

Daemon Threads

  • The threads which run in background are known as Daemon Threads.
  • Their main purpose is to provide support for non-daemon threads.
  • Non-daemon threads terminates when last daemon thread terminates.
  • We can check whether the Thread is daemon or not by using isDaemon() method and we can change daemon nature of thread by using setDaemon() method.

An example of Daemon Thread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyThread extends Thread 
{
	public void run()
	{
		for (int i=0;i<5 ;i++ )
		{
			System.out.println("Lazy thread!");
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{   }
		}
	}
}
class ThreadDemo10
{
	public static void main(String[] args)
	{
		MyThread t = new MyThread();
		t.setDaemon(true);
		t.start();
		System.out.println("End of main!");
	}
}

Leave a Reply