Thread are light weight process.
Threads consumes CPU in best possible manner
Reduces idle time of CPU and improves performance
Process can have multiple Threads.
Threads internally maintains their own stacks
Thread Create by two way
-
Extends Thread class
-
implements Runnable
-
new
-
runnable || Start()
-
running || run()
-
waiting || sleep(),wait()
-
dead
Note : user can call start() method. and ThreadSecheduler call run() method user can't call run() method
MIN_PRIORITY : 1
MAX_PRIORITY : 10
NORM_PRIORITY : 5
Note : Thread priority cannot guarantee the order
Daemon thread is a low priority thread
Example : Garbage Collection
JVM terminates itself when all normal threads finish its execution
JVM does not care about Daemon thread is running or not
JVM does not wait for Daemon thread before exiting while it waits for normal threads
volatile is only a keyword, can be used only with variables.
being stored in main memory
Yield()
yield() and sleep() method belongs to java.lang.Thread class.
yield() and sleep() method can be called from outside synchronized block.
yield() and sleep() method are called on Threads not objects.
yield() method stops thread for unpredictable time
Object class in java contains three methods
that allows threads to communicate about the lock status of a resource.
-
wait()
-
notify()
Wakes up a single thread that is waiting on this object's monitor.
- notifyAll()
Wakes up all threads that are waiting on this object's monitor.
ThreadLocal
ThreadPool
ThreadGroup
Thread Starvation
Busy spin