Thread Foundation (i)

Source: Internet
Author: User

Tag: The int code locks the lock class ring and Xtend the object

---restore content starts---

Thread Safety concept: a Class (object or method) is thread-safe when multiple threads access a class (object or method) and the class is always able to behave correctly.

Synchronized: Can be locked on any object and method, and the lock code is called "Mutex" or "critical section".

Here's a look at the code:

public class MyThread extends Thread {

private int count = 5;

public void Run () {
count--;
System.out.println (This.currentthread (). GetName () + "Count =" + count);
}

public static void Main (string[] args) {
MyThread MyThread = new MyThread ();
thread T1 = new Thread (myThread, "T1");
Thread t2 = new Thread (myThread, "T2");
thread t3 = new Thread (myThread, "T3");
thread T4 = new Thread (myThread, "T4");
Thread T5 = new Thread (MyThread, "T5");
T1.start ();
T2.start ();
T3.start ();
T4.start ();
T5.start ();
}
}

The result of this code run is:

T3 count = 2
T5 Count = 0
T4 count = 1
T1 count = 3
T2 count = 3

This result is not what we want, the result we want is that one thread comes in, count first prints 4, then the next thread comes in, prints 3, and so on. Description The Mythread class is not thread-safe. To achieve thread safety, simply modify the synchronized (synchronous lock) on the Run () method, which results in the following:

T1 count = 4
T3 count = 3
T4 count = 2
T2 count = 1
T5 Count = 0

Summary: When more than one thread accesses the Mythread run method, it is processed in a queued manner (where the queue is based on the order of the CPU allocations), and a thread wants to execute the code in the synchronized-modified method, first of all trying to get the lock, if it gets the lock, Executes the synchronized code body content; Without the lock, the thread will constantly try to get the lock, direct to get it, and multiple threads competing for the lock at the same time. (That is, there will be a problem of lock competition).

Similar to 5 children to go to the toilet, a child to rob, the rest of the children can only wait outside the door, there is a lock concept, and so the first child out, the remaining 4 children in a scramble.

---restore content ends---

Thread Foundation (i)

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.