Java Thread Interview Questions and Answers | Short – Real

This section contains java multi threading real short interview questions and answers asked in IT industries to freshers and experienced. Generally, this kind of questions are asked in software job interview questions to judge candidate skills over multiple areas in the subject in short span of time.


Q-Which method must be implemented by all java threads?

Answer – All threads must implement run() method.


Q-Why do we use isAlive() method in the java thread class ?

Answer – This method is used to test if a thread is alive. A thread is alive if it has been started and has not yet terminated.

This method returns Boolean value, “true” if the tread is alive otherwise “false”. Prototype of this method is: public final native boolean isAlive()


Q-If we don’t override java run() method, what will happen?

Answer – If we don’t override run() method then Thread class run() method will executed which has empty implementation resulting no output.


Q-What is default value of thread priority and up to what value it can be in java?

Answer – Default priority of thread is = 5. And minimum Priority = 1 & maximum Priority = 10.


Q-If a java thread creates a new child thread, what will be its thread priority?

Answer – The thread priority of a child thread will be same as what parent thread has.


Q-What does currentThread() do?

Answer – Returns a reference to the currently executing thread object.

Syntax of This API is: public static native Thread currentThread();


Q- How do you implement synchronized block in Java? Consider pseudo code given below.

Pseudo code:

class Test
{
static  int  i;
Public  void  function()  {

//Here we have to use
//static variable i;

}
}

Answer:

public class Test {

	static int i = 10;

	public void function() {
		//synchronized block
		synchronized (this) {
			int j = i + 1;
			System.out.println(j);
		}
	}
}

Related Posts