Java multithreaded debugging

Source: Internet
Author: User
Tags thread

Summary

The most valuable debugging tools are thread-centric. Most Java errors are related to thread interaction. Multithreaded debugging enables developers to view the execution of each thread running in an application.

SUN Laura Bennett

Multithreaded Debugging Basics

The most valuable debugging tools are thread-centric. Most Java errors are related to thread interaction. Multithreaded debugging enables developers to view the execution of each thread running in an application.

Because of the variability of the execution order, it is much more difficult to find errors in multithreaded applications than in the case of non threading. If you can execute instructions in the same predictable order, then debugging these applications can be very simple. Of course, this will violate the goal of multithreading. As a result, many IDE debuggers do not work in this situation because stepping into the code slows the debugging process and prevents the creation of error events.

Types of multithreaded errors

There are several common multithreaded coding issues that require close attention:

Access violation. This problem occurs when two or more threads try to access the same memory location.

Deadlock. For example, Thread1 locked the resourcea, and Thread2 locked resourceb. Then Thread1 tries to lock the RESOURCEB and waits for RESOURCEB to become available. At the same time, Thread2 tried to lock the resourcea and wait for Resourcea to become available. Result: Deadlock. One way to prevent deadlocks is to not let the process sleep when the lock is set. You can also use synchronization () to ensure that the code for a critical section can be accessed only by one thread at a time.

Data contention error. Data contention conditions lock the application, which can occur frequently, such as double-clicking the left mouse button. Data is often corrupted in the case of data contention. To prevent this error, you should make the variable inaccessible to multiple threads. Now there are tools that can analyze this problem and flag variables that might have data contention errors.

Sync error. This problem may occur when you are collecting unwanted information. Java automatically handles unwanted collection of information. At this point, all threads will change from run state to suspend.

Using the synchronized () method

Different versions of the JVM can implement thread-priority methods, which can affect thread synchronization. We recommend that you test the threading code on more than one operating system to verify that it is really cross-platform.

The synchronized () method creates a block of code that simulates the lock. The code depicted in the synchronization method only allows one process to run it at a time. Do not use too many synchronized calls, however, because they can directly affect code performance. In fact, synchronization stops multithreading.

The following shows a code example that uses the synchronization method. By setting the maximum column size of the table in the instance variable, this code adds an element to the table. You can see that multiple threads updating the same variable value can cause damage. Using the synchronization method helps mitigate 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 horrible threading errors:

It is important to test the various classes of the JVM if you rely on thread priority to keep threads synchronized. Be careful that two threads can be assigned to long and double variables at the same time. The annoying result is that one thread's changes may change a variable, and the second thread may change the same variable again. Consider synchronizing the assignment of those variable types.

Never use the Stop () method. In fact, Java 2 opposes this method. It stops the process immediately, but does not defragment it, which can cause many problems, including deadlocks and memory locks. You should always terminate the thread by returning from the run () method.

Do not restart a stopped thread. The run () method is not invoked, and the IsAlive () method reports an error, indicating that the thread is actually dead.

Do not monopolize the CPU. If a part of your program is exclusive to the CPU, you should run the yield () method, which allows other threads to also have a chance 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
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.