Simple intuitive - Equipment for Java multi-threaded programming (4)

xiaoxiao2021-03-06  39

The way to avoid not advocating the use is to support those methods reserved backward compatibility, which may appear in future versions or may not appear. Java multi-threaded support has made significant revisions in version 1.1 and version 1.2, and STOP (), Suspend () and resume () functions are not advocated. These functions may introduce subtle errors in the JVM. Although the function name may sound attracted, please call the temptation not to use them.

Debugging threaded procedures In threaded programs, certain common and annoying conditions that may happen are deadlocks, lock, memory damage, and resource depletion. Dead lock locks may be the most common problem of multi-threaded procedures. When a thread requires a resource while another thread holds the lock of the resource, a deadlock occurs. This situation is usually difficult to detect. However, the solution is quite good: all resource locks are obtained in the same order in all threads. For example, if there are four resources -a, b, c, and d - and a thread may get the lock of any of the four resources, make sure you first get the lock of A before getting the lock of B. Such push. If "thread 1" wants to get the lock of B and C, "Thread 2" acquires the lock of A, C, and D, which can cause blocking, but it will never cause death in these four locks. lock. Lock When a thread is busy accepting new tasks, it will never have a chance to complete any tasks, there will be live locks. This thread will eventually exceed the buffer and cause the program to crash. Imagine a secretary to enter a letter, but she has been busy answering the phone, so this letter will never be entered. Memory damage If you use the Synchronized keyword inefficient, you can avoid memory of this madness. Resources run out of certain system resources are limited, such as file descriptors. Multi-threaded programs may deplete resources because each thread may wish to have such resources. If the number of threads is quite large, or the number of candidates for a resource far exceeds the number of available resources, it is best to use the resource pool. One of the best examples is the database connection pool. As long as the thread needs to use a database connection, it takes one from the pool and returns it back to the pool. The resource pool is also called a repository. Debugging a large number of threads Sometimes a program is extremely difficult because there are a lot of threads running. In this case, this class below may be manually used:

Public class probe extends thread {

Public probe () {}

Public void run () {

While (true) {

Thread [] x = new thread [100];

Thread.Enumerate (X);

For (int i = 0; i <100; i ) {

Thread t = x [i];

IF (t == null)

Break;

Else

System.out.println (T.GetName () "/ T" T.GetPriority ()

"/ t" T.isalive () "/ t" t.isdaemon ());

}

}

}

}

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

New Post(0)