Java multithreaded programming
First, the concept of the operating system thread and process
The operating system now is a multitasking operating system. Multithreading is a way to achieve multitasking. A process is an application running in memory in which each process has its own separate piece of memory, and a process can start multiple threads. For example, in a Windows system, a running EXE is a process. A thread is an execution process in a process that can run multiple threads in a process. For example, a java.exe process can run many threads. Threads are always part of a process, and multiple threads in a process share the memory of the process. At the same time "execution is a human feeling, which actually rotates between threads."
Second, threads in Java
In Java, "thread" refers to two different things: an instance of the Java.lang.Thread class, 2, the execution of a thread. Use the Java.lang.Thread class or the Java.lang.Runnable interface to write code to define, instantiate, and start new threads. A thread class instance is just an object that, like any other object in Java, has variables and methods that live and die on the heap. In Java, each thread has a call stack, and the thread runs in the background even if no new threads are created in the program. A Java application always runs from the main () method, and the Mian () method runs inside a thread, which is called the main thread. Once a new thread is created, a new call stack is generated. Threads are divided into two categories: the user thread and the waiting thread. The JVM shuts down automatically when all user threads have finished executing. But the waiting thread is not independent of the JVM, and the waiting thread is typically created by the operating system or the user itself.
Java threading: What are the benefits of multithreading (1) Multithreaded technology makes the program more responsive, because the user interface can be active while doing other work, and (2) the processor time can be ceded to other tasks when there are no tasks currently being processed; (3) Tasks that take up a lot of processing time can periodically give processor time to other tasks, (4) You can stop tasks at any time, and (5) prioritize individual tasks to optimize performance. Second, when to use multithreading technology (1) time consuming or a large number of processor-intensive tasks blocking the user interface operation; (2) Each task must wait for an external resource (such as a remote file or Internet connection). Third, the disadvantage of multithreading is what (1) waiting to use shared resources causes the program to run slower. These shared resources are primarily exclusive resources, such as printers. (2) Managing threads requires additional CPU overhead. The use of threads can impose an additional burden on the system for context switching. When this burden exceeds a certain level, the characteristics of multithreading are mainly manifested in its shortcomings, such as the use of independent threads to update each element in the array. (3) The deadlock of the thread. That is, long waits or resource contention, as well as deadlocks and other multi-threading symptoms. (4) Read or write to the public variable at the same time. When multiple threads need to write to a public variable, the latter thread tends to modify the data held by the previous thread, thus allowing the parameters of the previous thread to be modified, and, when the read and write operations of the public variable are non-atomic, the uncertainty of the time on different machines, Results in an error in the operation of the data within one thread, resulting in an inexplicable error that the programmer cannot predict. Java threads: Creating and starting
One, define thread 1, extend the Java.lang.Thread class. 2, realize java.lang.Runnable interface. Second, the instantiation thread 1, if is the extension Java.lang.Thread class thread, then the direct new can. 2, if the implementation of the Java.lang.Runnable interface class, then use the thread construction method. Call the Start () method on the thread object that initiates the threads, rather than run () or any other method. Before the start () method is called: The thread is in the new state, the new state refers to a thread object, but there is no real thread. After the start () method is called: a complex set of things starts a new execution thread (with a new call stack), the thread is moved from new state to the operational state, and the target run () method runs when the thread gets an opportunity to execute.
Note: For Java, the run () method has nothing special. Like the main () method, it is just a new thread that knows the method name (and signature) of the call. Therefore, it is legal to raise the Run method on runnable or thread. But does not start a new thread. Java Thread: Line stacks model line stacks refers to the stack of memory thread scheduling at a certain time, the method that is currently called is always at the top of the stack. The content of the line stacks is dynamically changing as the program runs, so the research thread stack must choose a running moment (actually where the code is running). Java Threads: Transition of thread State
The state transitions of thread state threads are the basis of thread control. The total thread state can be divided into five states, namely: second, to prevent the execution of the thread we only consider three states, which are the three? 1, Sleep Thread.Sleep (Longmillis) and Thread.Sleep (long millis, int Nanos) static methods force the currently executing thread to hibernate (pause execution) to "slow down the thread." When the thread sleeps, it sleeps somewhere and does not return to the operational state until it wakes up. When the sleep time expires, it returns to the operational state. Reason for thread sleep: Thread execution is too fast, or it needs to be forced into the next round because Java specifications do not guarantee a reasonable rotation.
Note:1. Thread sleep is the best way to help all threads get the chance to run. 2, thread sleep expires automatically wake up and return to the operational state, not the running state. The time specified in sleep () is the shortest time the thread will not run. Therefore, the sleep () method does not guarantee that the thread will start executing after the sleep expires. 3. Sleep () is a static method that only controls the currently running thread. 4, Thread priority and thread concession yield () thread concessions are achieved through Thread.yield (). The yield () method acts by suspending the currently executing thread object and executing other threads. To understand yield (), you must understand the concept of thread precedence. The thread always has a priority and the priority range is between 1~10. The JVM thread Scheduler is a priority-based preemptive scheduling mechanism. In most cases, the currently running thread priority will be greater than or equal to the priority of any thread in the thread pool. But that's just the majority of the situation. Note: When designing multithreaded applications, be sure not to rely on the priority of the thread. Because thread scheduling priority operations are not guaranteed, thread precedence can only be used as a way to improve program efficiency, but ensure that the program does not rely on such operations. When thread pool threads have the same priority, the Scheduler's JVM implementation is free to select the thread it likes. There are two possible actions for the scheduler: one is to select a thread to run until it blocks or runs. The second is the Time Shard, which provides equal running opportunities for each thread in the pool.
set the priority of the thread: The default priority of the thread is the priority of the execution thread that created it. The priority of the thread can be rerouted via SetPriority (intnewpriority).
The thread priority is a positive integer between 1~10, and the JVM never changes the priority of a thread. However, the value between 1~10 is not guaranteed. While some JVMs may not recognize 10 different values, and each two or more of these priorities are merged into less than 10 priority, two or more priority threads may be mapped to a priority. 3. The Thread.yield () method Thread.yield () method acts by suspending the currently executing thread object and executing other threads. Yield () should be done to get the current running thread back to the operational state to allow other threads with the same priority to get the run opportunity. Therefore, the purpose of using yield () is to allow appropriate rotation between threads of the same priority. However, in practice there is no guarantee that yield () will be compromised, as the thread of the concession may be checked again by the thread scheduler. 4, Join () method The non-static method for thread join () lets one thread B "join" to the tail of another thread A. b does not work until a is complete. In addition, the join () method also has an overloaded version with a time-out limit. For example, T.join (5000); Let the thread wait for 5000 milliseconds, and if it exceeds this time, stop waiting and become a running state. The result of threading the Join join () on the thread stack is that the thread stack has changed, and of course these changes are instantaneous.
Initial deep thread