Java multithreading interview questions Set 2
Categories: Java 8(JDK1.8)
Describe the purpose and working of sleep() method.
The sleep() method in java is used to block a thread for a particular time, which means it pause the execution of a thread for a specific time. There are two methods of doing so.
Syntax:
public static void sleep(long milliseconds)throws InterruptedException
public static void sleep(long milliseconds, int nanos)throws InterruptedException
What is the difference between wait() and sleep() method?
wait()sleep()
1) The wait() method is defined in Object class.The sleep() method is defined in Thread class.
2) The wait() method releases the lock.The sleep() method doesn't release the lock.
Is it possible to start a thread twice?
No, we cannot restart the thread, as once a thread started and executed, it goes to the Dead state. Therefore, if we try to start a thread twice, it will give a runtimeException "java.lang.IllegalThreadStateException". Consider the following example.
public class Multithread1 extends Thread
{
public void run()
{
try {
System.out.println("thread is executing now........");
} catch(Exception e) {
}
}
public static void main (String[] args) {
Multithread1 m1= new Multithread1();
m1.start();
m1.start();
}
}
Can we call the run() method instead of start()?
Yes, calling run() method directly is valid, but it will not work as a thread instead it will work as a normal object. There will not be context-switching between the threads. When we call the start() method, it internally calls the run() method, which creates a new stack for a thread while directly calling the run() will not create a new stack.
What about the daemon threads?
The daemon threads are the low priority threads that provide the background support and services to the user threads. Daemon thread gets automatically terminated by the JVM if the program remains with the daemon thread only, and all other user threads are ended/died. There are two methods for daemon thread available in the Thread class:
a) public void setDaemon(boolean status): It used to mark the thread daemon thread or a user thread.
b) public boolean isDaemon(): It checks the thread is daemon or not.
Can we make the user thread as daemon thread if the thread is started?
No, if you do so, it will throw IllegalThreadStateException. Therefore, we can only create a daemon thread before starting the thread.
class Testdaemon1 extends Thread{
public void run(){
System.out.println("Running thread is daemon...");
}
public static void main (String[] args) {
Testdaemon1 td= new Testdaemon1();
td.start();
setDaemon(true);// It will throw the exception: td.
}
}
Output
Running thread is daemon...
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1359)
at Testdaemon1.main(Testdaemon1.java:8)
What is shutdown hook?
The shutdown hook is a thread that is invoked implicitly before JVM shuts down. So we can use it to perform clean up the resource or save the state when JVM shuts down normally or abruptly. We can add shutdown hook by using the following method:
public void addShutdownHook(Thread hook){}
Runtime r=Runtime.getRuntime();
r.addShutdownHook(new MyThread());
Some important points about shutdown hooks are :
a) Shutdown hooks initialized but can only be started when JVM shutdown occurred.
b) Shutdown hooks are more reliable than the finalizer() because there are very fewer chances that shutdown hooks not run.
c) The shutdown hook can be stopped by calling the halt(int) method of Runtime class.
When should we interrupt a thread?
We should interrupt a thread when we want to break out the sleep or wait state of a thread. We can interrupt a thread by calling the interrupt() throwing the InterruptedException.
What is the synchronization?
Synchronization is the capability to control the access of multiple threads to any shared resource. It is used:
a) To prevent thread interference.
b) To prevent consistency problem.
When the multiple threads try to do the same task, there is a possibility of an erroneous result, hence to remove this issue, Java uses the process of synchronization which allows only one thread to be executed at a time. Synchronization can be achieved in three ways:
- by the synchronized method
- by synchronized block
- by static synchronization