Parallel programming is more prevalent when CPUs are in the multi-core era, but writing parallel programs is more prone to error. During the development process, an engineer can notice that the same program is correct in a single-threaded run, but it can be wrong in multiple threads. The causes of the errors associated with parallelism are often obscure, and in a single test, their appearance is highly random. Because multiple threads in a program may be interleaved in any way, even if a parallel program is running correctly hundreds of times, a new error may still occur the next time it is run.
Multi-thread Run-time Analysis Tool is a runtime profiling tool developed by IBM for multithreaded Java programs that can be used to analyze and look for potential parallel program errors in Java code that are not easily discovered, such as data competition and deadlock (deadlock), thereby improving the code quality of the parallel program. This article will introduce a new tool for detecting random parallel errors in Java programs (Http://alphaworks.ibm.com/tech/mtrat), and check for potential parallel program errors in Java code to improve the security and stability of your code, and demonstrate its ability to exploit potential but not-so-possible errors.
Overview
The Java programming language provides powerful language support for writing multithreaded applications. However, it is still difficult to write a useful multithreaded program with no errors. The programming language has many challenges in its threads. Among these challenges, the most important is the improvement of programming complexity. These programming complexities are caused by the access of synchronous shared variables, potentially dependent on timing errors, and the complexity of debugging and optimizing parallel programs.
Mtrat only so that different technologies are integrated into a single development tool to cover up the complexity within the tool and make mtrat easy to use. The Mtrat is mainly composed of the following parts,
A simple command-line interface and Eclipse plug-in. The output Mtrat a parallel error that was checked.
Dynamic Java byte code modification engine. You can modify Java classes when Java class files are loaded by a Java virtual machine.
The program runtime information collector. Collects dynamic information about the program, such as memory access, thread synchronization, creation and completion.
An efficient run-time analysis engine. The collected run-time information is analyzed online, and if a potential parallel error is found, it will be reported to the user through the interface.
Test Data Competition
In a parallel program, a data competition error occurs when two parallel threads, without any constraints, access a shared variable or a domain of a shared object, and at least one operation is a write operation. The most powerful feature of Mtrat is the discovery of potential data competition errors in parallel programs. The Java program below hides a potential data competition error.
package sample;
class Value
{
private int x;
public Value()
{
x = 0;
}
public synchronized void add (Value v)
{
x = x + v.get();
}
public int get() {return x;}
}
class Task extends Thread
{
Value v1, v2;
public Task (Value v1, Value v2)
{
this.v1 = v1;
this.v2 = v2;
}
public void run() {v1.add(v2);}
}
public class DataRace
{
public static void main (String[] args) throws InterruptedException
{
Value v1 = new Value ();
Value v2 = new Value ();
Thread t1 = new Task(v1, v2);
Thread t2 = new Task (v2, v1);
t1.start();
t2.start();
t1.join();
t2.join();
}
}