Java Multithreading Concepts
Java Multithreading Concepts
Java Multithreading Concepts
Multiprocessing:
Multithreading:
- A class can extend the java.lang.Thread class and override the run() method.
- Example:
System.out.println("Thread running");
t.start();
- A class can implement the java.lang.Runnable interface and pass it to a Thread object.
- Example:
System.out.println("Thread running");
t.start();
Difference:
- Extending Thread prevents inheriting from other classes, while implementing Runnable allows
multiple inheritance.
- Runnable: The thread is ready to run but waiting for CPU time.
Daemon threads are background threads that provide services to user threads. They are terminated
- To make a thread a daemon thread, use the setDaemon(true) method before starting the thread.
- Daemon threads should not be used for critical tasks as they do not prevent the JVM from shutting
down.