A THREAD is a single sequential
flow of control within a program.
A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process, e.g. memory and CPU time, are allocated to it via the operating system.
A thread is a so called lightweight process. It has its own call stack but can access shared data of other threads in the same process. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data.
Example:
The HotJava Web browser is an example of a multithreaded application. Within the HotJava browser you can scroll a page while it's downloading an applet or image, play animation and sound concurrently, print a page in the background while you download a new page, or watch three sorting algorithms race to the finish.
How to Create a Thread!
Multithread
Java is a multithreaded application that allows multiple thread execution at any particular time.
Single Thread
In a single-threaded application, only one thread is executed at a time because the application or program can handle only one task at a time.
this one is used for setting that a thread should run in Daemon (background) mode or not. When a thread is in daemon mode, it means it runs until any other user thread (non-daemon) is alive in the application. Whenever there is no user-thread alive, then JVM kills the daemon threads by force (silently). You cannot even get any exception about the exit signal. These kinds of threads are useful when your application needs to have a background-service thread or an event-handler. These threads are usually run in lower priority. Once again, these threads are running within your process, but JVM does not look at them as user threads for checking the life of the process
- All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program’s start, when the main() method is invoked with the main thread
A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process, e.g. memory and CPU time, are allocated to it via the operating system.
A thread is a so called lightweight process. It has its own call stack but can access shared data of other threads in the same process. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data.
Example:
The HotJava Web browser is an example of a multithreaded application. Within the HotJava browser you can scroll a page while it's downloading an applet or image, play animation and sound concurrently, print a page in the background while you download a new page, or watch three sorting algorithms race to the finish.
How to Create a Thread!
- creating a thread is accomplished by implementing an interface and extending a class.
- Every Java thread is created and controlled by the java.lang.Thread class.
- When a thread is created, it is assigned a priority.
- The thread with higher priority is executed first, followed by lower-priority threads.
- If the exit method has been invoked and authorized by the security manager.
- All the daemon threads of the program have died.
Multithread
Java is a multithreaded application that allows multiple thread execution at any particular time.
Single Thread
In a single-threaded application, only one thread is executed at a time because the application or program can handle only one task at a time.
Properties
- Thread id Each thread has a unique ID (int value) which is provided by either the runtime or the OS; this is very helpful when you want to get the actual reference by.
- Thread name A name for the thread, it could be the same through many threads, also could be empty. It's useful to categorize threads belonging to a specific module or task.
- Thread State The current state of the thread. The state of a thread is determined by the State enumeration, and a thread state could be one of the following:
- Runnable: indicates that the thread has started and is running its task.
- New: indicates the thread has been created, but hasn't invoked (started) yet; when you just create an instance of
the thread and haven't attempted invoke the
start()
. - Blocked: when a thread is in running state and is blocked because it is waiting for locking a resource which has been locked by another thread.
- Waiting: when the thread is idle and is waiting for a signal (notify).
- Timed_Waiting: when the thread is sleeping for an amount of time or is waiting for a signal from any another thread with timeout.
- Terminated: when a thread either has finished its job, note that a terminated thread would not start over from the beginning, and it's ready for finalizing.
- Interrupted: this indicates if an interrupt signal has been sent to the thread or not, note that this is not an actual state, a thread would get interrupted when it's in either running or waiting states (not in new and terminated states). You cannot get the interrupt state by the State enumeration.
- Dead: when thread A is waiting for a signal from thread B, where thread B is waiting for thread A too, or maybe thread B has sent the signal just before thread A attempts to wait for it (this is the actual dead lock), here there is nothing left behind to signal the waiting thread. You need to just program your application in such a way that you ensure there is no dead state. Note that there is no property or method to determine if a thread is in dead state or not.
Thread Priority
- It indicates the priority of the thread, a higher priority causes
more process attention (context switch) to the thread, that could be one
of the following:
MAX_PRIORITY
MIN_PRIORITY
NORM_PRIORITY
(default value)
this one is used for setting that a thread should run in Daemon (background) mode or not. When a thread is in daemon mode, it means it runs until any other user thread (non-daemon) is alive in the application. Whenever there is no user-thread alive, then JVM kills the daemon threads by force (silently). You cannot even get any exception about the exit signal. These kinds of threads are useful when your application needs to have a background-service thread or an event-handler. These threads are usually run in lower priority. Once again, these threads are running within your process, but JVM does not look at them as user threads for checking the life of the process
Thread's Behaviors
- Start (simultaneously): when the thread needs to run simultaneously with another thread(s), this operation is done by
the thread
start()
method. - Sleep: whenever a thread needs to sleep for a particular of time
Thread.sleep(time in ms)
would help, note that there is no warranty about the exact time, it could get more, the sleep process (reminded time) could get skipped if another thread Interrupts the sleeping thread. - Interrupt: it's used when a thread needs to interrupt another thread where it is in either wait (and
join()
), sleep, or even runnable states. For example, thread A is waiting for a signal, then thread B interrupts thread A, here the interrupt message: - wakes up the target thread if it's either in wait (wait/join) or sleep state
- throws
InterruptedException
to the target thread to notify it about the interrupt signal - sets the interrupted flag to true if and only if the thread was in running state.
- every sleep, join, or wait operation should be surrounded with a try-catch block because of any interruption signal (exception)
- the interruption is just like a signal, it doesn't force the thread to shutdown
- Getting the current thread: you would access the current thread by
Thread.currentThread()
. It returns a reference to the currently executing thread object, but outside the thread you may need to check all the threads or have a references of the thread. - Join: it means as it says, when thread A joins thread B, it means thread A has to wait until thread B is finished (termination).
- Yield: a thread calls
yield
it means the OS can switch to another thread but the first thread still needs to work. wait()
It releases the synchronized (locked) object, and waits until another thread notifies (notify, stop, or interrupt) it, then after it gets notified (if by notify) tries to acquire the lock again and continues its work. Note that a waiting thread has to acquire its lock again on the related object.
sleep()
ID doesn't release any locked object, and sleeps for a specified amount of time, it could get canceled (wake up) by either stop or interrupt signals.
No comments:
Post a Comment