Source: Internet
Author: User

We know that for a thread object T, when the Start method is called, the thread executes all the time.

So what is the conflict problem in threads? If you now have two threads T1, T2, and the Run method in these two threads manipulates the same data at the same time, for example, with a well-defined integer variable C, with an initial value of 10, and then in the Run method to keep C in a self decrement operation and to manipulate the life of the thread through while (C > 0).

```public class ThreadTest extends Thread {

int c = ten;

T1.start ();
T2.start ();
}

public void Run () {while
(c> 0) {
try {
} catch (Interruptedexception e) {
/ /TODO auto-generated Catch block
e.printstacktrace ();
}
c--;
System.out.println ("c =" + c);;
}
}

public static void Main (string[] args) {
//TODO auto-generated method stub
}

```

In the above code, we can see that in the T1 and T2 the Run method of the C at the same time the self-subtraction operation, and then we run, we found that the results of the operation are as follows:

c = 9
c = 8
c = 7
c = 6
c = 5
c = 4
c = 3
c = 2
c = 1
c = 0
c =-1

See, in the end there is a-1, obviously when the C from the reduction to 0 when the thread has died out, why will appear C continue to reduce into-1 it. This is the conflict problem in the thread, let's analyze why-1, we set the thread hibernate 50ms in the above code, so for example, now the program is running to the T1 this thread c1 from reduced to 0, then at the moment, C is equal to 0, right. But then. In T1, C has not yet been reduced to 1, but at this point in the T2 while (c>0) has been successfully judged, then in T1 C since the reduction, and in the T2 of C since the reduction, at this time, there are 1 this situation.

Well, this obviously does not meet the results we want, for such a problem, we can solve this problem through an identification synchronized, how to use the following:

```public void Run () {
synchronized (.) {while
(c> 0) {
try {
} catch ( Interruptedexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
c--;
System.out.println ("c =" + c);;
}}}
```

So what is the synchronized logo? The synchronized identifies a code snippet or method, the part that is locked by the "object Mutex", and how the logo is used to make the code no longer present-1. In fact, the above code in the T1 and T2 two threads start to run, then through this identity, it will be only one thread at a time to execute the code snippet or method, by doing so, can resolve the thread in the conflict problem.
﻿﻿
Related Keywords:

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.