Eclipse Debug Multithreading

Source: Internet
Author: User

June Wang Forever

Used to a lot of thread development, multithreading development is also a little bit of experience, but at the beginning of multi-threaded development, encountered a lot of walls. But there's always a good tool that can do more with less. I used the tool is eclipse, in the development of multi-threading, its debug mode is able to directly simulate the multi-threaded environment, there are a lot of information on the Internet, but most of them are not very full. I hope I can record some of my experience, hoping to help the development of multi-threading and tangled friends.

Write a simple multithreaded test case first:

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 two lines

Hello World

Next, you will demonstrate how to use the debug mode for step-by execution, followed by a gradual application of knowledge to extend

    1. The location of the breakpoint

Here is a point to note that the location of the breakpoint must be correct, the Run method or the method called after the run, otherwise, the program runs out, debug mode also only a main thread in the run, good, directly see the results.

Here we see two threads have been up, in debug mode also noted Thread-0 and Thread-1, here I add a section of code to more clearly show which thread executed here.

After adding this code, the printed result is

Thread-0 Hello World

Thread-1 Hello World

Now I need to Thread-1 this thread first print out the result, that is, the thread can be controlled, I want to execute which is the first line, how to do? In order to be more obvious, I add a few threads, a total of 5 threads, in order to print out in sequence, I am the language expression hardship, directly look at the picture, the picture is dynamic, a bit big, please wait patiently

I think this dynamic should not be explained more, perhaps someone will ask what is the use of this, it is very useful, we can control which thread starts running, which thread runs to a point, the program pauses there, and then other programs run again. The most typical example is whether the singleton mode is absolutely safe under multi-threading (whether it must be a singleton), here I also demonstrate that thread insecure singleton pattern 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 singleton, print out the reference address, the test environment with two threads to print

Under normal circumstances, should print out two the same value, first run directly, without the debug mode, the result is as follows:

This generally, is the manifestation of the singleton, in most cases, you have the possibility to test many times the result is still the case, but this code does in the multi-threaded situation will be problematic, how to reproduce the problem, the following direct diagram shows:

The final result:

[Email protected]

[Email protected]

As can be seen here, the two results are different and prove not the same object. This is the way the problem recurs, does it feel like a useful tool?

This article is not about the singleton model, which is a typical example of using debug mode to solve problems in multi-threaded situations. I think, with the debug mode, not only to solve the problem, but also to help us understand some of the knowledge points, this has achieved the advantage of edge learning.

Multi-threaded combat: every time when changing

Multi-threaded Combat: Use multithreading in the Web to enhance the user experience

Eclipse Debug Multithreading

Related Article

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.