Simple intuitive - Essentials for Java multi-threaded programming (3)

xiaoxiao2021-03-06  38

The advanced multi-threaded support thread group thread in the Java programming language is individually created, but they can classify them to the thread group to facilitate debugging and monitoring. You can only associate it with a thread group while creating a thread. In programs that use a large number of threads, use thread group organization threads may be helpful. They can be considered as a directory and file structure on a computer. Inter-threaded information When the thread will wait for a condition before continuing execution, only the synchronized keyword is not enough. Although the Synchronized keyword blocks concurrently updated an object, it does not implement the thread. The Object class provides three functions for this: Wait (), Notify () and NotifyAll (). Take the global climate prediction procedure as an example. These programs are divided into many units in each loop by dividing the earth, and each unit is calculated until these values ​​tend to stabilize, and then some data is exchanged between adjacent cells. Therefore, from essentially, each thread in each cycle must wait for all threads to complete their tasks to enter the next loop. This model is called shield synchronization, the following example shows this model: shielding synchronization

Public class bsync {

INT TOTALTHREADS;

Int CurrentThreads;

Public BSYNC (INT X) {

TotalthReads = x;

CurrentThreads = 0;

}

Public synchronized void waitforall () {

CurrentThreads ;

CurrentThreads

Try {

Wait ();

} catch (exception e) {}

}

Else {

CurrentThreads = 0;

NotifyAll ();

}

}

}

When WAIT () is called for a thread, the thread is effectively blocked, and only the other thread calls notify () or NotifyAll () until the same object. Therefore, in the previous example, different threads will call the waitforlL () function after completing their work, and the last thread will trigger the NotifyAll () function, which will release all threads. The third function notify () only informs a thread that is waiting for, this function is useful when accessing the resource used by a thread can be accessed. However, it is impossible to predict which thread will get this notification because it depends on the Java Virtual Machine (JVM) scheduling algorithm. When the CPU gives another thread to abandon a rare resource (such as database connection or network port), it may call the Yield () function to temporarily reduce its priority so that some other threads can run. The guards have two types of threads: user threads and daemons. The user thread is those threads that complete useful work. Guarding threads are threads that only provide auxiliary function. The THREAD class provides a setDaemon () function. The Java program will run to all user threads, and then it will destroy all the daemons. In the Java Virtual Machine (JVM), even after the MAIN is over, if another user thread is still running, the program can continue to run.

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

New Post(0)