Java Threads Interview Questions

Java Threads Interview Questions

1 what are the two types of multitasking?

Ans:a. Process-based.
b. Thread-based.

2 what is a Thread?

Ans: A thread is a single sequential flow of control within a program.

3 what are the two ways to create a new thread?

Ans: A.Extend the Thread class and override the run () method.
B.Implement the Runnable interface and implement the run () method.

4 If you have ABC class that must subclass XYZ class, which option will you use to create a thread?

Ans: I will make ABC implement the Runnable interface to create a new thread, because ABC class will not be able to extend both XYZ class and Thread class.

5 which package contains Thread class and Runnable Interface?

Ans: java. Lang package

6 what is the signature of the run () mehod in the Thread class?

Ans: public void run ()

7 shich methods calls the run () method?

Ans: start () method.

8 which interface does the Thread class implement?

Ans: Runnable interface

9 what are the states of a Thread?

Ans: Ready, Running, Waiting and Dead.

10 where does the support for threading lie?

Ans: The thread support lies in java.lang.Thread, java.lang.Object and JVM.

11 In which class would you find the methods sleep () and yield ()?

Ans: Thread class

12 In which class would you find the methods notify (), notify All () and wait ()?

Ans: Object class

13? what will notify () method do?

Ans: notify() method moves a thread out of the waiting pool to ready state, but there is no guaranty which thread will be moved out of the pool.

14 Can you notify a particular thread?

Ans: No.

15 what is the difference between sleep () and yield ()?

Ans: When a Thread calls the sleep () method, it will return to its waiting state. When a Thread calls the yield () method, it returns to the ready state.

16 what is a Daemon Thread?

Ans: Daemon is a low priority thread which runs in the backgrouund.

17 How to make a normal thread as daemon thread?

Ans: We should call set Daemon (true) method on the thread object to make a thread as daemon thread.

18 what is the difference between normal thread and daemon thread?

Ans: Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.

19? Give one good example of a daemon thread?

Ans: Garbage Collector is a low priority daemon thread.

20 what does the start () method of Thread do?

Ans: The thread’s start () method puts the thread in ready state and makes the thread eligible to run. Start () method automatically calls the run () method.