-
What is the difference between Process and Thread?
Program
- A program is set of instructions(a rule or a statement). Program is passive.
Process
- A program under execution is called a Process. Process is active. Processes do not share memory.
Thread
- A unit or portion of Process that executes independently and A Process can have many threads running parallely.
- Multiple Threads share common heap memory of the parent Process. Each Thread has Its own Thread Stack.
-
What are the benefits of multithreaded programming?
- To create high performing applications.
- Independent tasks can be executed by multiple threads such applications can use multi threading and hence improve performance.
-
What is difference between user Thread and daemon Thread?
- In Java, a daemon thread is a low priority thread that runs in the background and does not prevent the JVM (Java Virtual Machine) from exiting. These threads are typically used for background tasks that do not have a significant impact on the overall performance of the application, such as garbage collection or logging.
- Daemon threads are different from user threads in that they do not prevent the JVM from shutting down. When the JVM determines that all user threads have completed, it will exit, even if there are still daemon threads running. This is why it's important to ensure that any data structures or resources that a daemon thread is using are properly handled and cleaned up, as the JVM may exit without warning.
- By default, all the threads are user threads in Java.
- In Java, you can set a thread to be a daemon thread by calling the setDaemon() method on the Thread object, and passing in "true". For example:
-
-
What are different states in lifecycle of Thread?
NEW
RUNNABLE
TIMED_WAITING
WAITING
BLOCKED
TERMINATED
-
Can we call run() method of a Thread class?
- ThreadObject.run(); This will not create a
new
Thread instead will usemain
thread.
- ThreadObject.run(); This will not create a
-
How can we pause the execution of a Thread for specific time?
- Using Thread.sleep(milliseconds) pauses currently running thread for specified amt of time but holds the current object lock.
-
What do you understand about Thread Priority?
- Sets the priority for the current thread using method thread.setPriority(int num);
-
What is Thread Scheduler and Time Slicing?
-
What is Thread Context-Switching in Multi-Threading?
- JVM takes out one thread from its execution and saves its TCB and then gives opportunity to another thread to execute shared block/code/method.
-
How can we make sure main() is the last thread to finish in Java Program?
- Use all thread as user threads and not the daemon thread. Main thread will terminate after executing all user threads.
-
Which is more preferred - Synchronized method or Synchronized block?
-
What is Deadlock(explain using Reader/Writer Problem)? How to analyze/detect and avoid deadlock situation?
-
How does threads communicate with each other?
- Different threads communicate with each other using
wait()
,notify()
andnotifyAll()
methods.
- Different threads communicate with each other using
-
Why thread communication methods wait(), notify() and notifyAll() are in Object class?
-
Why wait(), notify() and notifyAll() methods have to be called from synchronized method or block?
-
Why Thread sleep() and yield() methods are static?
-
How can we achieve thread safety in Java?
-
What is volatile keyword in Java
-
What is ThreadLocal?
-
What is Thread Group? Why it’s advised not to use it?
-
What is Java Thread Dump, How can we get Java Thread dump of a Program?
-
What is Java Timer Class? How to schedule a task to run after specific interval?
-
What is Thread Pool? How can we create Thread Pool in Java?
-
What will happen if we don’t override Thread class run() method?
- What is atomic operation? What are atomic classes in Java Concurrency API?
- What is Lock interface in Java Concurrency API? What are its benefits over synchronization?
- What is Executors Framework?
- What is BlockingQueue? How can we implement Producer-Consumer problem using Blocking Queue?
- What is Callable and Future?
- What is FutureTask class?
- What are Concurrent Collection Classes?
- What is Executors Class?
- What are some improvements in Concurrency API in Java 8?