The Java operating system relies on threads in many ways, and all class library designs take into account multithreading. In fact, Java uses threads to make the entire environment asynchronous. This facilitates the reduction of invalid portions by preventing wasted CPU cycles.
To better understand the advantages of a multithreaded environment, it can be compared with its control. A single-threaded system is handled by using an event loop method called polling. In this model, single-threaded control runs in an infinite loop, polling an event sequence to decide what to do next. Once the polling device returns a signal indicating that the network file is ready to be read, the event Loop Dispatch control is managed to the appropriate event handler. No other events occur in the system until the event handler returns. This is a waste of CPU time. This results in a part of the program monopolizing the system and preventing the execution of other events. Overall, a single-threaded environment, when a thread waits for resources to block (block, suspend execution), the entire program stops running.
The advantage of Java Multi-threading is that it cancels the main loop/polling mechanism. A thread can pause without affecting other parts of the program. For example, when a thread reads data from the network or waits for user input, the idle time that is generated can be exploited elsewhere. Multithreading allows a live loop to sleep for a second in every frame gap without pausing the entire system. There is a thread blocking in the Java program, only one thread is paused and the other threads continue to run.
Threads exist in several states. The thread can be running (running). It can run as long as the CPU time is obtained. The running thread can be suspended (suspend) and temporarily interrupt its execution. A suspended thread can be resumed (resume, allowing it to continue running from where it was stopped.) A thread can be blocked (block) while waiting for resources.
At any time, the thread can terminate (terminate), which immediately interrupts its run. Once terminated, the thread cannot be recovered.
Thread Priority
Java prioritizes each thread to determine how the thread should be treated when compared to other threads. The thread priority is an integer that details the precedence relationship between threads. As an absolute value, precedence is meaningless; when there is only one thread, the higher priority thread does not run faster than the lower priority thread. Instead, the priority of a thread is used to decide when to switch from one running thread to another. This is called "context switch". The rules that determine the occurrence of context conversions are simple:
- Threads can automatically discard control. In the case where I/O is not determined, sleep or congestion is accomplished by explicit concessions. Under this assumption, all other threads are instrumented and the highest priority thread ready to run is granted to the CPU.
- Threads can be preempted by high-priority threads. In this case, the low-priority thread does not abandon itself, and the processor is simply accounted for-whatever it is doing-the processor is occupied by a high-priority thread. Basically, once a high-priority thread is running, it executes. This is called multi-tasking with priority.
When two threads of the same priority compete for CPU cycles, the situation is a bit more complicated. For an operating system such as WINDOWS98, the priority threads are automatically partitioned in cyclic mode. For other operating systems, such as the Solaris 2.x, the priority line threads is automatically discarded for their peers. If this is not the case, the other threads will not run.
Warning: The context conversion of a priority thread under a different operating system may produce an error.
Synchronization of
Because multithreading introduces an asynchronous behavior into your program, there must be a way to enhance synchronization when you need it. For example, if you want two threads to communicate with each other and share a complex data structure, such as linked list sequences, you need some way to ensure that they do not conflict with each other. That is, you must prevent one thread from writing to the data while another thread is reading the data in the linked list. To this end, Java implements another approach based on the old mode of inter-process synchronization: Enhancement (monitor). Enhancement is a control mechanism defined first by C.a.r.hoare.
You can think of a tube as a small box that controls only one thread. Once the thread enters the pipe, all threads must wait until the thread exits the pipe. In this way, a pipe can be used to prevent shared resources from being manipulated by multiple threads.
Many multithreaded systems take a pipe as the program must explicitly refer to and manipulate the object. Java offers a clear solution. There is no "Monitor" class; instead, each object has its own implicit tube, which is automatically loaded when the synchronization method of the object is invoked. Once a thread is contained in a synchronous method, no other thread can invoke the same object's synchronization method. This allows you to write very clear and concise multithreaded code, because synchronization support is language built-in.
Message delivery
After you divide the program into several threads, you define the connections between the threads. When planning in most other languages, you must rely on the operating system to establish inter-thread communication. This certainly increases the cost. However, Java provides a clean, low-cost way to talk across multiple threads-by invoking a predetermined approach to all objects. The Java messaging system allows a thread to enter a synchronous method of an object, and then waits there until the other thread explicitly notifies it.
Thread class and Runnable interface
The multithreaded system of Java is built on the thread class, its method, and its co-companion interface runnable based on. The thread class encapsulates the execution of threads. Since you cannot directly refer to the state of a running thread, you have to process it through its proxy, and the thread instance is generated. To create a new thread, your program must extend the thread or implement the Runnable interface.
The thread class defines several ways to help manage threads. The methods used in this chapter are shown in table 11-1:
Table 11-1 ways to manage threads
| Method |
meaning |
| GetName |
Get Thread Name |
| GetPriority |
Get Thread Priority |
| Jsalive |
Determines whether the thread is still running |
| Join |
Wait for a thread to terminate |
| Run |
The entry point of the thread. |
| Sleep |
Suspend a thread for a period of time |
| Start |
To start a thread by calling the Run method |
So far, the examples used in this book are single-threaded. The remainder of this chapter explains how to create and manage threads with thread and Runnable. Let's start with the threads that all Java programs have: the main thread.
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (44) exception type
Java know how much (45) uncaught exceptions
How much Java knows (the) use of try and catch
Java know how much (47) use of multiple catch statements
Java knows how much (in) the nesting of Try statements
Java know how much (a) throw: Exception throws
Java know how many () Java throws clauses
Java knows how many (or) finally
Java know how much (52) built-in exceptions
Java know how much (53) Use Java to create your own exception subclasses
Java know how much (54) assertion detailed
Java know how many (55) threads
Java know how much (56) threading Model