Java Threads Interview Question and Answer for experienced

Categories: Interview questions and answers Experienced Java Java Script

Java Threads Interview Question and Answer for experienced

 

Q1. What is the difference between processes and threads? 

AnswerA process is an execution of a program, while a Thread is a single execution sequence within a process. A process can contain multiple threads. A Thread is sometimes called a lightweight process.

 

Q2. Explain different ways of creating a thread. Which one would you prefer and why? 

Answer:  There are three ways that can be used in order for a Thread to be created: • A class may extend the Thread class.

• A class may implement the Runnable interface. 

• An application can use the Executor framework, in order to create a thread pool. The Runnable interface is preferred, as it does not require an object to inherit the Thread class. In case your application design requires multiple inheritance, only interfaces can help you. Also, the thread pool is very efficient and can be implemented and used very easily.

 

Q3. Explain the available thread states in a high-level. 

Answer

During its execution, a thread can reside in one of the following states: 

  • Runnable: A thread becomes ready to run, but does not necessarily start running immediately. 
  • Running: The processor is actively executing the thread code. 
  • Waiting: A thread is in a blocked state waiting for some external processing to finish. 
  • Sleeping: The thread is forced to sleep. 
  • Blocked on I/O: Waiting for an I/O operation to complete. 
  • Blocked on Synchronization: Waiting to acquire a lock. 
  • Dead: The thread has finished its execution

 

Q4. What is the difference between a synchronized method and a synchronized block? 

AnswerIn Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in a method level (coarse grained lock) or block level of code (fine grained lock).

 

Q5. How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply? 

AnswerThe JVM uses locks in conjunction with monitors. A monitor is basically a guardian that watches over a sequence of synchronized code and ensuring that only one thread at a time executes a synchronized piece of code. Each monitor is associated with an object reference. The thread is not allowed to execute the code until it obtains the lock.

 

Q6. What’s a deadlock? 

AnswerA condition that occurs when two processes are waiting for each other to complete, before proceeding. The result is that both processes wait endlessly.

 

Q7. How do you ensure that N threads can access N resources without deadlock? 

AnswerA very simple way to avoid deadlock while using N threads is to impose an ordering on the locks and force each thread to follow that ordering. Thus, if all threads lock and unlock the mutexes in the same order, no deadlocks can arise.

Top articles
Servlet interview questions and answers for experienced Published at:- Spring Interview Questions and Answer for Experienced Published at:- Spring Boot Features for Java Developers (2022) Published at:- Detecting build version and time at runtime in Spring Boot Published at:- AWS Interview Questions Answers for Freshers and Experienced Published at:- Laravel Interview Question Answer for Freshers and Experienced Published at:- Splunk Interview Questions and Answers for freshers and Experienced Published at:- IOS Interview Questions for freshers and experienced Published at:- Red Hat Interview Question Answers for Freshers and experienced Published at:- Python Interview Question and Answer Published at:- Basic Indian History General Knowledge Question and Answer (Quiz) Published at:- AWS Interview Question and Answer for freshers and Experienced Published at:- Angular 13 interview Question and Answer Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 2) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Set 3) Published at:- Limited Departmental Competitive examination (LDC) examination (Water supply) Published at:- Interview Question and answer for Limited Departmental Competitive Examination Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination (Soil mechanics) Published at:- Question bank for Limited Departmental Competitive Examination (LDC) examination Sanitary Engineering Published at:- Interview Question for Limited Departmental Competitive Examination (LDC) Principles of Surveying Published at:- Interview MCQ question for railways New Lines, Doublings & Gauge Conversion Projects Published at:- MCQ Interview Question and answer for Railway Station and Passenger Amenities Published at:- Interview Question and Answer for Railway preparation Published at:- PHP MCQ Quiz Question with Answer Published at:- PHP MCQ Quiz Question with Answer (set 2) Published at:- Angular MCQ Quiz Question and Answer Published at:- Angular MCQ Quiz Question and Answer Set 2 Published at:- Java General Interview Questions and Answer Published at:- Java Threads Interview Question and Answer for experienced Published at:- Java Collections Interview Question and Answer for freshers and experienced Published at:- Garbage Collector in Java Frequently Asked Questions and Answers Published at:- Difference between Exception and Error in java Exception Handling Published at:- Java Applets Interview Questions with Answer Published at:- Java Swing Interview Questions with Answer for freshers and experienced Published at:- Java Database Connectivity (JDBC) Interview Question and answer Published at:- Remote Method Invocation (RMI) Interview Question and Answer Published at:- Jakarta Server Page (JSP) programming language Interview Question and Answer Published at:- Write a program in Java to display the first 10 terms of the following series: 10, 20, 30, 40, …….. Published at:- Write the program in Java to display the first ten terms of the following series: 1, 4, 9, 16, Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.