Java debugging tutorial-multi-thread debugging-Linux general technology-Linux programming and kernel information. The following is a detailed description. Summary
The most valuable debugging tool is thread-centered. Most Java errors are related to thread interaction. Multi-threaded debugging allows developers to view the execution status of each thread running in the application.
--------------------------------------------------------------------------------
By Wing, source: Laura Bennett
The most valuable debugging tool is thread-centered. Most Java errors are related to thread interaction. Multi-threaded debugging allows developers to view the execution status of each thread running in the application.
As the execution sequence is variable, it is much more difficult to find errors in multi-threaded applications than in non-threaded applications. If commands can be executed in the same predictable order, debugging these applications can become very simple. Of course, this will violate the goal of multithreading. As a result, many IDE debuggers cannot play any role in this case, because the single-step debugging code slows down the debugging process and prevents re-creation of error events.
Multithreading Error Type
There are several common multi-threaded coding issues that require close attention:
Access violation. This problem occurs when two or more threads attempt to access the same memory location.
Deadlock. For example, Thread1 locks ResourceA, while Thread2 locks ResourceB. Then Thread1 tries to lock ResourceB and waits until ResourceB becomes available. Meanwhile, Thread2 tries to lock ResourceA and waits until ResourceA becomes available. Result: a deadlock occurs. One way to prevent deadlocks is to prevent the process from sleeping when the lock is set. You can also use synchronization () to ensure that the code of the key part can only be accessed by one thread at a time.
Data contention error. Data contention conditions lock the application, which often happens, for example, double-click the left mouse button. In the case of data contention, data is usually damaged. To prevent such errors, the variables should not be accessed by multiple threads. There are tools available to analyze such problems and Mark variables that may cause data contention errors.
Synchronization error. This problem may occur when collecting useless information. Java automatically collects useless information. At this time, all threads will change from running to temporarily suspended.
Use the synchronized () method
Different Versions of JVM may have different methods to implement thread priority, which affects thread synchronization. We recommend that you test the threaded code on multiple operating systems to verify whether it is truly cross-platform.
The synchronized () method creates a block of code that simulates locking. The Code depicted in the synchronous method allows only one process to run at a time. But do not use too many synchronized calls because they will directly affect the code performance. In fact, synchronization has stopped multithreading.
The following shows the sample code for using the synchronization method. By resetting the maximum column size of the table in the instance variable, this Code adds an element to the table. It can be seen that updating the same variable value by multiple threads may cause damage. Using the synchronization method can help ease this problem.
/** Synchronized method to add an element to a table **/
Public void addElement (Patient newPatient)
{
Synchronized (lock)
{
Log query = incomingPatient. getLog ();
Results = query. getAllSearchResults ();
For (int k = 0; k <results. length; k ++)
{
... // Add to table
SetMaxColSize (MyTable );
TableContents. add (MyTable );
}
}
}
Avoid multithreading errors
There are some ways to avoid terrible thread errors:
If the thread is synchronized Based on the thread priority, it is very important to test various types of JVM. Be careful that two threads may assign values to both long and double variables at the same time. The annoying result is that a thread change may change a variable, while the second thread may change the same variable again. Consider assigning values to those variable types synchronously.
Never use the stop () method. In fact, Java 2 is opposed to this method. It immediately stops the process, but does not sort it out, which causes many problems, including deadlocks and memory locks. The thread should always be terminated by returning from the run () method.
Do not restart a stopped thread. The run () method is not called, and the isAlive () method reports an error, indicating that the thread is actually dead.
Do not exclusively occupy the CPU. If a portion of the program exclusively occupies the CPU, run the yield () method, which can give other threads the opportunity to run. See this small example:
Double answer = 0;
For (int I = 0; I <10000; I ++ ){
For (int j = 0; I <10000; j ++ ){
Answer = (answer * I) + j)/j;
}
Thread. yield (); // Now other threads may run while this
// Runs in the background
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service