Threads in Java

xiaoxiao2021-03-06  68

Does not involve the saving of the CPU register and the protection of process status when switching. Threads implement an asynchronous mode of the entire environment, but sometimes needs synchronization (two threads read the same data at the same time). The thread actively discards the occupation of the CPU via Sleep or Yeild. The Run () method is almost certainly containing some form of loop - they will continue until the thread is no longer needed. Therefore, in order to interrupt and exit this cycle, specific conditions must be specified. Otherwise, unless the external STOP or DESTROY call is sent, it will run forever until the program ends. Blocked (hang) status blocked may be caused by the following five aspects: 1, call SLEEP (), making threads into sleep state; 2, suspend the execution of the thread with ruspend (). Unless the thread receives the resume () message, it will not return to the running state; The operation is complete. 5, thread attempt to call another object "synchronization" method, but which object is locked, temporarily unused. Java does not have a monitor, the solution provided by Java is to have its independent monitors for each object. When a thread enters a function (Sychronized) with synchronous features, the remaining thread will not operate synchronization of the same object. Function, in other words, synchronization mechanisms have been integrated into the Java language. Whether Sleep or Suspend will not be dispensed when they are called, they must wait for time or call their own resume (). WAIT () When hanging internal calling methods, WAIT () will explain the lock of the object, and can freely exit Wait () because a notich () can exercise the time lapse (when Wait is parameter). Wait () and Notify () areica Object () classes, while Sleep, Suspend, and Resume are THREAD classes. Therefore, things specializing in threading are also part of the general basic class (Object). Therefore, we can place a wait () into any synchronization method, regardless of whether or not the process involving threads is in.

Thread pool: Chihua resources: In object-oriented programming, creating and destroying objects is time-time, because creating an object to get memory resources or more resources, even in Java, virtual machines will try to track No object, in order to be able to carry out garbage recovery after the object is destroyed. So a means of improving the efficiency of service procedural is to minimize the number of times of creating and destroying objects, especially some resource-based objects. Generally, a thread pool includes at least the following components. ThreadPoolManager: It is used to create a thread pool, destroy the thread pool, add a new task. Workthread: Thread in the thread pool. Task Interface (Task): Interfaces that each task must be implemented to perform the execution of the working thread scheduling task. It specifies the entrance of the task, the end of the task, and the execution status of the task, etc. Task queue: Task for storing unprocessed tasks. A buffer mechanism is provided.

转载请注明原文地址:https://www.9cbs.com/read-110691.html

New Post(0)