A multithreaded program contains two or more parts that can run
concurrently. Each part of such a program is called a thread, and each thread
defines a separate path of execution. Thus, multithreading is a
specialized form of multitasking. Multithreading enables you to write very
efficient programs that make maximum use of the CPU, because idle time can be
kept to a minimum. This is especially important for the interactive, networked
environment in which Java operates, because idle time is common.
The value of a multithreaded environment is best understood in
contrast to its counterpart. Single-threaded systems use an approach called an event loop with polling. In this model,
a single thread of control runs in an infinite loop, polling a single event
queue to decide what to do next. Once this polling mechanism returns with, say,
a signal that a network file is ready to be read, then the event loop
dispatches control to the appropriate event handler. Until this event handler
returns, nothing else can happen in the system. This wastes CPU time. It can
also result in one part of a program dominating the system and preventing any other
events from being processed. In general, in a singled-threaded environment,
when a
thread blocks (that is, suspends execution) because it is waiting for some
resource, the entire program stops running.
The benefit of Java’s multithreading is that the main
loop/polling mechanism is eliminated. One thread can pause without stopping
other parts of your program.
For example, the idle time
created when a thread reads data from a network or waits for user input can be
utilized elsewhere. Multithreading allows animation loops to sleep for a second
between each frame without causing the whole system to pause. When a thread
blocks in a Java program, only the single thread that is blocked pauses. All
other threads continue to run.
Threads exist in several states.
- A thread can be running. It can be ready to run as soon as it gets CPU time.
- A running thread can be suspended, which temporarily suspends its activity. A suspended thread can then be resumed, allowing it to pick up where it left off.
- A thread can be blocked when waiting
for a resource. At any time, a thread can be terminated, which halts its
execution immediately. Once terminated, a thread cannot be resumed.
Thread PrioritiesEvery thread has a priority. By default, a thread inherits the priority of its parent thread, that is, the thread that started it. You can increase or decrease the priority of any thread with the setPriority method. You can set the priority to any value between MIN_PRIORITY (defined as 1 in the Thread class) and MAX_PRIORITY (defined as 10). NORM_PRIORITY is defined as 5.Whenever the thread-scheduler has a chance to pick a new thread, it prefers threads with higher priority.
In cases where two threads with the same priority are competing for CPU cycles, the situation is a bit complicated. For operating systems such as Windows, threads of equal priority are time-sliced automatically in round-robin fashion. For other types of operating systems, threads of equal priority must voluntarily yield control to their peers. If they don’t, the other threads will not run.
Synchronization
When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. In most practical multithreaded applications, two or more threads need to share access to the same objects. Because multithreading introduces an asynchronous behavior to your programs, there must be a way for you to enforce synchronicity when you need it.For example, if you want two threads to communicate and share a complicated data structure, such as a linked list, you need some way to ensure that they don’t conflict with each other. That is, you must prevent one thread from writing data while another thread is in the middle of reading it.For this purpose, Java implements a model of interprocess synchronization: the monitor (also called a semaphore). A monitor is an object that is used as a mutually exclusive lock, or mutex. Only one thread can own a monitor at a given time. When a thread acquires a lock, it is said to have entered the monitor. All other threads attempting to enter the locked monitor will be suspended until the first thread exits the monitor. These other threads are said to be waiting for the monitor. A thread that owns a monitor can reenter the same monitor if it so desires.
No comments:
Post a Comment