Q)
Which is better process based multitasking or thread based multitasking and why?
Thread based multitasking is better.
Multitasking threads require less overhead than multitasking processes.
Processes are heavyweight tasks that require their own separate address spaces.
Threads, on the other hand, are lighter weight. They share the same address space
and cooperatively share the same heavyweight process.
Interprocess communication is expensive and limited. Context switching from one
process to another is also costly
Q)
Types of Thread in java
There are two types of thread - user thread and daemon thread.
User Threads are created by java developers. Eg Main thread. all threads created
inside main method (child threads of main thread) are non-daemon / user thread by
default, because main thread is non-daemon.
Daemon thread is a low priority thread (in context of JVM) that runs in background
to perform tasks such as garbage collection (gc) etc., they do not prevent the JVM
from exiting (even if the daemon thread itself is running) when all the user
threads (non-daemon threads) finish their execution. JVM terminates itself when all
user threads (non-daemon threads) finish their execution, JVM does not care whether
Daemon thread is running or not, if JVM finds running daemon thread (upon
completion of user threads), it terminates the thread and after that shutdown
itself
Q) Why is the output not in an order
If multiple Threads are waiting to execute then which Thread will execute 1st is
decided by "Thread Scheduler" which is part of JVM hence its vendor dependent and
thats why we cant expect exact execution order / output
So when ever situations comes to multithreading the guarantee in behaviour is very
less. We can tell the possible output but not the exact one.
1. Basic Thread Concepts
What is a thread? How is it different from a process? 19
Explain multithreading. Why is it important? 112
What are the two ways to create a thread in Java? 112
Can we call run() directly instead of start()? What happens? 15
What is the main thread in Java? 312
2. Thread Lifecycle & States
List the states in a thread’s lifecycle. 23
What is the NEW state? How do you transition to RUNNABLE? 312
What happens when a thread is in TIMED_WAITING state? 12
How does sleep() differ from yield()? 25
What is context switching? 712
3. Synchronization & Locks
What is a race condition? Provide an example. 511
Explain the synchronized keyword. Can it be applied to a class? 112
What is the difference between class-level and object-level locks? 13
Why is wait() called in a synchronized block? 27
What happens if a thread calls notify() without holding the lock? 2
4. Daemon Threads
What is a daemon thread? How is it different from a user thread? 13
How do you create a daemon thread? 19
Can we convert a user thread to a daemon thread after starting it? 13
What happens if all user threads finish execution? 37
5. Deadlocks & Inter-Thread Communication
What is deadlock? Write a simple deadlock scenario. 511
How can deadlock be detected? 711
How to avoid deadlocks in Java? 712
Explain the producer-consumer problem. How to solve it? 512
What is the purpose of wait(), notify(), and notifyAll()? 27
6. Thread Methods & Behavior
What does Thread.join() do? 27
Why is sleep() a static method? 212
What happens if you call start() twice on the same thread? 15
How to set a thread’s priority? Does it guarantee execution order? 37
What is the default priority of a thread?
*Bonus: Code-Based Questions**
47. **Write a program to create two threads: one prints even numbers, the other odd
numbers.**
48. **Write code to reverse a string using threads.**
49. **Implement a thread-safe singleton class.** :cite[4]:cite[8]
50. **Write a program where a thread prints numbers 1-10, but pauses for 1 second
after each print.**
Basics of Thread Creation & Lifecycle
What is the difference between the start() and run() methods of the Thread class?
InterviewBit
How do you create a thread in Java by extending Thread versus implementing
Runnable?
GeeksforGeeks
How do you create a daemon thread, and what are its typical use cases?
DigitalOcean
What happens if you call start() twice on the same Thread instance?
GeeksforGeeks
What is the effect of calling Thread.yield() and when might you use it?
Site Title
What’s the purpose of the sleep() method, and does it release locks when sleeping?
Indeed
How does Thread.join() work and how can you use it to sequence thread execution?
Java Revisited
What’s the difference between thread priority levels, and does the JVM guarantee
strict ordering?
Indeed
What is a ThreadGroup, and why is its use generally discouraged?
DigitalOcean
Explain the thread lifecycle states in Java (NEW, RUNNABLE, BLOCKED, WAITING,
TIMED_WAITING, TERMINATED).
GeeksforGeeks
Synchronization & Locks
What is the difference between a synchronized method and a synchronized block?
Medium
What is a lock object (java.util.concurrent.locks.Lock), and how does it differ
from synchronized?
DigitalOcean
What is the volatile keyword, and how does it affect visibility and ordering?
DigitalOcean
Define atomicity; what are Java’s atomic classes (AtomicInteger, etc.)?
DigitalOcean
How do you avoid deadlock? Give an example.
DigitalOcean
What is livelock, and how does it differ from deadlock?
GeeksforGeeks
What is thread starvation, and what can cause it?
Indeed
Explain memory consistency errors in Java concurrency.
Indeed
What does the Java Memory Model’s “happens-before” guarantee?
GeeksforGeeks
What is a spin lock, and when might it outperform a mutex?
Site Title
Inter-Thread Communication
How do wait(), notify(), and notifyAll() work, and how do they differ?
Medium
What is a thread dump, and how can you obtain and analyze it?
DigitalOcean
What is ThreadLocal, and when would you use it?
DigitalOcean
What is the difference between a process and a thread in Java?
Medium
Explain the ABA problem in concurrent algorithms.
Site Title
What is safe publication, and why is it important?
Site Title
Why was Thread.stop() deprecated, and what should you use instead?
GeeksforGeeks
What is the purpose of Thread.interrupt(), and how do you check for an interrupt?
Indeed
Compare isInterrupted() vs interrupted().
GeeksforGeeks
What are the pitfalls of using Thread.sleep() versus Object.wait() in loops?