Thread is Java’s way of making a single JVM look like many machines, all running at the same time. Start () method on Thread will register thread with thread scheduler, which determines thread to run on each available JVM.
We could execute thread by three ways as mentioned below:
1. Thread can execute its own thread- extends thread
Counter ct=new Countr()
Ct.start() //in case of extends
2. The tread can execute the run() method of some other objects –implements Runnable
Counter ct=new Counter()
Thread t=new Thread(ct);
3. Thread could be run using Thread pool provided java.util.concurrent
Difference between processes and threads
A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process
A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety
1. Runnable: waiting for its turn to be picked for execution by the thread scheduler based on thread priorities.
2. Running: The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn with this static method
3. Yield: Thread.yield () which is static method is use to make currently executing thread to yield. Because of context switching overhead, yield () should not be used very frequently.
4. Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish. The wait () method puts an executing thread into Waiting state , and notify() and notifyAll() methods waiting thread out of the Waiting state. They are implemented in the Object class not in Thread and they may only be called in synchronized code.
5. Sleeping: Java threads are forcibly put to sleep (suspended) with this overloaded method: such as Thread.sleep (milliseconds), Thread.sleep (milliseconds, nanoseconds);
6. Blocked on I/O: Will move to runnable after I/O condition like reading bytes of data etc. changes. Blocked on synchronization: Will move to Runnable when a lock is acquired.
7. Dead: The thread is finished working.
8. Blocking: If a method needs to wait an interminable amount of time until some I/O occurrence take place, then a thread executing that method should graciously step out of Running state. A thread that has graciously stepped out in this fashion is said to be blocked.
Daemon threads are low priority background thread in JVM run in background which doesn’t overload on machine e.g. Garbage collector. To make a thread as a daemon thread in Java
Difference between sleep and wait
1. Wait called on object, which is synchronized whereas sleep could be called on Thread.
2. Sleep does not release the lock whereas wait release the lock on object.
3. Wait is called only from synchronized context but sleep is called without synchronized.
4. Waiting thread can be awake by calling notify/notifyAll whereas sleeping thread can be awake after specified time interval.
5. Thread.sleep () method is a static method and applies on current thread, while wait () is an instance specific method and only got wake up if some other thread calls notify method on same object.
6. Also in case of sleep, sleeping thread immediately goes to Runnable state after waking up while in case of wait, waiting thread first acquires the lock and then goes into Runnable state.
7. If you require a specified second of pause use sleep () method or if you want to implement inter-thread communication use wait method.
Difference between yield and sleep in java
Yield method pause the current executing thread and give chance to other waiting thread with same priority or higher priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution.
Note: Sleep method in Java has two variants one which takes millisecond as sleeping time while other which takes both millis and nano second for sleeping duration.
sleep (long millis)
sleep (long millis,int nanos)
Cause the currently executing thread to sleep for the specified number of milliseconds plus the specified number of nanoseconds.
Benefits on Runnable Thread over extend thread
• Runnable provide multi inheritance
• Single responsibility principle violated by extend Thread as it support multithreading and execution behavior i.e. run implementation
• The run() allow to access private data or call private method
1. When thread is dead it can’t start again. Although you cannot restart a dead thread but you use submit the old runnable instance to new thread.
2. The thread is relatively heavy weight operation, involving significant kernel resources. It is better to create a pool of reusable worker threads that can be assigned chores as needed.
3. Every thread has a priority an integer 1 to 10.