Thread Interview Questions Part II

Can the 'start()' method of the Thread class be overridden? If yes should it be overridden?

Yes, the 'start()' method can be overridden. But it should not be overridden as it's implementation in thread class has the code to create a new executable thread and is specialized.

Which methods of the thread class are used to schedule the threads?

The methods are as follows:

  • public static void sleep(long millis) throws InterruptedException

  • public static void yield()

  • public final void join() throws InterruptedException

  • public final void setPriority(int priority)

  • public final void wait() throws InterruptedException

  • public final void notify()

  • public final void notifyAll()

Which thread related methods are available in Object class?

The methods are:

  • public final void wait() throws Interrupted exception

  • public final void notify()

  • public final void notifyAll()

Which thread related methods are available in Thread class?

Methods which are mainly used are as follows:

  • public static void sleep(long millis) throws Interrupted exception

  • public static void yield() public final void join() throws Interrupted exception

  • public final void setPriority(int priority)

  • public void start()

  • public void interrupt()

  • public final void join()

  • public void run()

  • public void resume()

List the methods which when called the thread does not release the locks held?

Following are the methods:

  • notify()

  • join()

  • sleep()

  • yield()

Does each thread have its own thread stack?

Yes, each thread has its own call stack.

In the below example t1 and t3 will have the same stack and t2 will have its own independent stack.

What is thread starvation?

In a multi-threaded environment thread starvation occurs if a low priority thread is not able to run or get a lock on the resource because of presence of many high priority threads. This is mainly possible by setting thread priorities inappropriately.

What is threadLocal variable?

ThreadLocal is a class. If a variable is declared as threadLocal then each thread will have a its own copy of variable and would not interfere with the other's thread copy. Typical scenario to use this would be giving JDBC connection to each thread so that there is no conflict.

ThreadLocal_JBK class by JAVA API


Implementation of ThreadLocal


Learn in Detail about Java Programming