Used to have a lot of thread development, multithreading development is a little bit of experience, but the beginning of multithreading development, encountered many walls. But in a sense, a good tool always gets the best of it. The tool I use is eclipse, in the development of multithreading, its debug mode is able to directly simulate the multithreaded environment, there are a lot of data online, but most of them are not very full. I hope I can record some of my experience, I hope to help also for multi-threaded development and tangled friends.
First write a simple multithreaded test case:
package Com.wxw.debug;
Public class Testmain extends Thread {
@Override
Public void Run () {
System.out.println ("Hello World");
}
Public Static void Main (string[] args) {
Testmain T1 = new testmain ();
Testmain t2 = new testmain ();
T1.start ();
T2.start ();
}
}
This piece of code, the console prints out two lines
Hello World
Next, you'll demonstrate how to use the debug mode for step-by-step execution, and then gradually apply the knowledge to the location of the extended breakpoint
Here is a point to note that the location of the breakpoint must be correct, run method or run after the call method, otherwise, the program ran out, debug mode is also only a main thread in the run, well, directly to see the results.
Here we see two threads already up and in the debug mode also annotated Thread-0 and Thread-1, where I add a snippet of code to show more clearly which thread is executing here.
After adding this code, the result of printing is
Thread-0 Hello World
Thread-1 Hello World
Now I need to Thread-1 this thread first to print out the result, that is, the thread can be controlled, I want to perform whichever is OK, how to do. In order to be more obvious, I add a few threads, a total of 5 threads, in order to print out, I am the language to express the needy, directly look at the picture, the picture is dynamic, a bit big, please be patient
I think this dynamic should not be explained more, perhaps some people will ask what is the use, this is useful, we can control which thread to start running, which thread to run to a point, the program paused there, and then other programs to run. The most typical example is whether the single example mode is absolutely secure in multithreading (whether it must be a single case), and here I also demonstrate the thread-unsafe single-mode code, excerpted from: http://www.blogjava.net/kenzhh/archive/2013/03/15/357824.html
Public class Singleton {
Private Static Singleton instance;
Private Singleton () {
}
Public Static Singleton getinstance () {
if (instance = null) {
System.out.println (instance);
Instance = New Singleton ();
}
return instance;
}
}
Whether it is a single case, print out the reference address, the test environment with two threads to print
Normally, you should print out two of the same values, go straight to run without debug mode, and the results are as follows:
In general, it is a single example of performance, in most cases, you may test many times the result is still the case, but this code does in a multi-threaded situation will be problematic, how to reproduce the problem, the following directly illustrated:
The final result:
Com.wxw.debug.singleton@1fc0f04
Com.wxw.debug.singleton@13ded59
Here you can see that two results are different and prove to be not the same object. The question recurs, does it feel like this tool is useful?
This article is not a single example of the model, a typical example, mainly talk about using the debug mode to solve the problem of multithreading. I think, in the debug mode, not only to solve the problem, but also to help us understand some knowledge points, this is to achieve the advantages of edge learning.