Java Multithreading Interview Questions: Thread is a lightweight subprocess. It is a separate path of execution because each thread runs in a separate stack from the memory area of the process. Thread class extends Object and implements Runnable.
Java Multithreading Interview Questions
1) Explain about Multitasking?
A) The concept of executing multiple functionalities simultaneously one with the other is known as Multitasking.
2) Explain the advantage of Multitasking?
A) The advantage of multitasking is shown below:
1) By applying the concept of multitasking we can totally avoid the idle state of the CPU
2) By applying the concept of multitasking we can make the functionalities independent of each other.
3) Actually, the concept of multitasking was invented in order to overcome the idle state of CPU. But we apply the concept of multitasking to make the functionalities independent of each other.
3) What happens in Multitasking?
A) In Multitasking multiple functionalities would be executed part by part with very small & negligible time gap. Because of which it looks multiple functionalities are getting executed simultaneously one with the other.
4) What are the two types of Multitasking?
A) The two types of Multitasking are:
1) Process-based Multitasking.
2) Thread-based Multitasking.
5) Explain about Process based Multitasking?
A) Executing multiple programs belonging to the different languages & technologies simultaneously one with the other is known as process-based Multitasking.
6) Explain about Thread based Multitasking?
A) Executing multiple functionalities (or) methods belonging to the same process (or) belonging to the same application simultaneously is known as Thread based Multitasking.
7) Why Java support Thread based Multitasking?
A) In thread-based multitasking, the control would be switching between the two different functionalities (or) methods belonging to the same application and process which would not increase any overhead on the system. Thus Java supports only thread based multitasking.
8) What is a Thread?
A) A thread is nothing but a functionality (or) method which could be executed simultaneously with the other part of the program.
9) What is the difference between Thread and Method?
A) Every Thread is a method. But every method cannot be a thread.
10) What is a Process?
A) Any program which is under execution is called as a process.
11) Explain the relation between thread and process?
A) A thread is a part of the process.
12) Explain the steps using which we implement the concept of thread-based multitasking?
A) The two steps using which we implement the concept of thread-based multitasking in Java:
1) Defining a functionality has a thread.
2) Executing run method of Runnable Interface has a thread.
13) Explain Start() method of thread class?
A) The functionality of start method of thread class is only to handover the run method of Runnable Interface to the JVM for scheduling and request JVM to execute run method of the runnable interface has a thread.
14) What is Multithreading?
A) The concept of defining multiple functionalities has threads and executing multiple functionalities has threads simultaneously one with the other is known as Multithreading.
Java Multithreading Interview Questions & Answers
15) What is scheduling?
A) The concept of deciding, when the execution of a thread has to start (or) begin and deciding what part of a thread has to get executed and deciding which thread has to continue next is known as scheduling.
16) what is Rescheduling?
A) JVM bringing thread which is under wait state into Action is known as Rescheduling.
17) Explain about Priorities?
A) Priority means preference. Using priorities we can control the Rescheduling of threads.
18) Explain about Notify method?
A) Whenever notify method is called, it would be bringing a thread which is in the wait state back into action. Based on the priorities mention.
19) What is Deadlock?
A) It is the state of a thread application, where multiple threads belonging to the same application would enter into wait state simultaneously & multiple threads belonging to the same application would continue in the wait state forever, which is known as Deadlock.
20) How to avoid Deadlock?
A) We can avoid Deadlock using wait method of the Object class.
21) How many types of Threads are there in Java?
A) There are two types of Threads in Java:
1) User Thread.
2) Daemon Thread.
22) What is User Thread?
A) The normal and usual child thread which is joined to the end of its parent is known as User Thread.
23) What is Daemon Thread?
A) Any child thread which is not joined to the end of its parent is known as Daemon Thread.
24) What is the Minimum priority of Thread?
A) “1” is the Minimum priority of Thread.
25) What is the Normal priority of Thread?
A) “5” is the Normal priority of Thread.
26) What is the Maximum priority of Thread?
A) “10” is the Maximum priority of Thread.
Java Threads Interview Questions
27) Can we make the user thread as daemon thread if the thread is started?
A) No, if you do so, it will throw IllegalThreadStateException.
28) What are the methods used for preventing Thread execution?
A) We can prevent the Thread Execution by using the following methods of threads class.
29) Explain about the Yield() method?
A) This is used to pause the currently executing thread, for giving chance to remaining waiting threads of some priority. If there is no thread waiting or all the threads waiting have low priority, then the same thread is once again given a chance for the execution.
30) Explain about the Join() method?
A) If a thread wants to wait until completion of other thread, then we should go for join() method.
31) Explain about the Sleep() method?
A) If a thread doesn’t want to perform any operation for the particular amount of time when a pausing is required, then we should go far sleep() method.
32) How to Interrupt a Thread?
A) A Thread can interrupt a sleeping or waiting thread by using interrupt() method of Thread class.
33) How to stop a Thread?
A) To stop a thread execution in the middle by using stop() method.
34) How to Suspend() and Resume() a Thread?
A) A Thread can Suspend another thread by using suspend() method. And Resume a suspend thread by using resume() method.
35) Explain about the Synchronized?
A) Synchronized is the keyword applicable only for methods but not for variables and class.
36) What is the use of Synchronized Keyword?
A) If a method is declared as Synchronized, then at a time only one method is allowed to execute.
37) How are locks used in Synchronization?
A) Internally Synchronization concept is implemented by using lock concept. Whenever we use the synchronized keyword, then lock concept comes into existence.
38) Explain about the wish() method as Synchronized?
A) If we declare wish() method as synchronized then the threads will be executed one by one and hence we will get regular output.
39) Explain about the Inter-Thread Communication?
A) The process of executing threads in a sequence of the loop with communication is called Inter-Thread Communication.
40) What are the different states of a Thread?
A) The different states of Thread are:
1) New state.
2) Runnable state.
3) Running state.
4) Dead state.
5) Blocked state.
41) Explain about Time-sliced or Round-Robin Scheduling?
A) A running thread is allowed to execute for a fixed length of time, after which it moves to the ready to run state to wait for its turn to run again.