Getting started with Java Threads – Part 2 : join() and sleep()

The thread class provides some methods to define the threads behavior.
Two important methods to consider are:
join(), to make the other instanced threads to wait for it to day. You can provide a timeout as parameter.
sleep();

The sleep() method throws a checked exception called “InterruptedException”,each time the thread receives an interrupt request. You have to handle in the code by wrapping the method in a try-catch block.

The following code will make you understand the join method.
We define a MyThread thread:

public class MyThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then we can observe the behaviour running the following main:

		public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread myJoiningThread = new Thread(new MyThread());

		myThread.setName("myThread");
		myJoiningThread.setName("myJoiningThread");

		System.out.println("Threads will be started...");

		myJoiningThread.start();

		try {
			// waits for this thread to die
			myJoiningThread.join();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		myThread.start();

	}

Without the try-catch block in which the join method is invoked the threads would be started asynchronously (the order is not predictable) and in the console you might get either:
Threads will be started...
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main

or:

Threads will be started...
Implementing the runnable interface - Thread name: myJoiningThread - priority: 5 - group: main
Implementing the runnable interface - Thread name: myThread - priority: 5 - group: main

But if you include the try-catch block, the myJoiningThread will always be invoked first.

To understand the sleep method you can create a MySleepingThread:

public class MySleepingThread implements Runnable {

	public void run() {
		Thread thread= Thread.currentThread();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Implementing the runnable interface"
				+ " - Thread name: " + thread.getName()
				+ " - priority: " + thread.getPriority()
				+ " - group: " + thread.getThreadGroup().getName());

	}

}

Then you can run the following main method:

public static void main(String[] args) {

		// extending Thread
		Thread myThread = new Thread(new MyThread());
		Thread mySleepingThread = new Thread(new MySleepingThread());
		myThread.setName("myThread");
		mySleepingThread.setName("mySleepingThread");

		System.out.println("Threads will be started...");

		mySleepingThread.start();
		myThread.start();

	}

You can see in the console that the sleeping thread will be started after 3 seconds.

The sleep method is static. By invoking “Thread.sleep(3000)” you are delaying the creation of the current thread.

If you use the method in the main method, the sleeping thread will be “main”.

Comments are closed.

Categories
Links: