The basic of Thread
A thread is a part of a process. A thread lives within a process. No process, no thread. To understand better, we cannot keep our brain outside for sometime and place it back; it is because brain is a part of human body. Similarly, a thread cannot be there without a process. Infact, a process is divided into a number of threads.
"". It is something confusing. A thread comprises of a block statements. These statements are executed one-by-one sequentially. Every thread maintains its own execution context within a process context area. We can say, a . For execution, one thread does not depend on other; every thread is independent of other. This tutorial gives you how to divide a process into a number of threads or to say, how to create threads, how to assign different tasks, priorities and synchronization.
If the execution shifts between the threads of the same process, it is known as . If the execution shifts between the threads of different processes, it is known as .
Microprocessor and consequently output is obtained faster. When a program is divided into number of threads, each comprising a few statements, the code becomes , , and becomes easier. Generally, user's input is very slow in GUI environment. This late user's response is best utilized by other threads (reduces overall execution time).
- Playing media player while doing some activity on the system like typing a document.
- Transferring data to the printer while typing some file.
- Downloading some files like images while browsing is going on (downloading images is very slow).
- Running animation while system is busy on some other work.
- Honoring requests from multiple clients by an application/web server.
- Threads are useful in handling events generated by components like mouse etc. A thread is capable to produce a frame with images.
Multithreading is one of the built-in and important feature of Java language. Java's built-in classes, useful to develop a multithreaded program, are hereunder and all are from package.
The following are common exceptions that come in thread programming. All are from package.
- It is thrown when the waiting or sleeping state is disrupted by another thread.
- It is thrown when a thread is tried to start that is already started.
Types of thread:
In java thread is of 2 types
1. system defined thread
2. user defined thread
SYSTEM DEFINED THREAD:
These threads are known as daemon thread which are managed by JVM or Java Runtime system.These are predefined system level codes developed by sun microsystem .
Types of daemon thread:
USER DEFINED THREAD:
These threads are guided by predefined threads. The thread class provides a predefined method through which the user defined threads are converted to daemon thread.
This method is final void setdaemon(boolean b);
States of thread
1. New state :
After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.
2. Runnable (Ready-to-run) state :
A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.
3. Running state – A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.
4. Dead state – A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.
5. Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.