Java Multithreading Basic Summary VII: Reentrantlock

Source: Internet
Author: User

Before summing up the part of the multithreading of the mechanism of the basis, the ideal state of course is to use the lock-free synchronous solution to the problem of multithreaded programming. But the actual problem is that in many cases, we have to use lock synchronization to ensure thread safety. Since the beginning of JDK5, there are two mechanisms to mask the interference of the code block in parallel access, synchronized keyword has been introduced some content, so this simple to say another lock mechanism: Reentrantlock.

For the shortcomings of the synchronized is also simple to say some, the actual use of the more disturbing points is: a. Only one "condition" is associated with the lock, which is difficult to manage for a large number of concurrent threads (wait and wake) B. When multithreading competes for a lock, the remaining threads that have not been locked are kept trying to acquire the lock without interruption. This situation can result in a decline in performance for a large number of competing threads. JDK5 provides a reentrantlock synchronization mechanism to improve the two situations mentioned earlier. Below I still write a small example analysis:

Java code

Import Java.util.concurrent.locks.ReentrantLock;

/**
* @author: Yanxuxin
* @date: 2010-1-4
*/
public class Reentrantlocksample {

public static void Main (string[] args) {
Testsynchronized ();
Testreentrantlock ();
}

public static void Testreentrantlock () {
Final SAMPLESUPPORT1 support = new SampleSupport1 ();
Thread A = new Thread (new Runnable () {
public void Run () {
try {
Support.dosomething ();
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
});

Thread second = new Thread (new Runnable () {
public void Run () {
try {
Support.dosomething ();
}
catch (Interruptedexception e) {
System.out.println ("Second Thread interrupted without executing counter++,beacuse it waits a long time.");
}
}
});

Executetest (the second);
}

public static void Testsynchronized () {
Final SampleSupport2 Support2 = new SampleSupport2 ();

Runnable Runnable = new Runnable () {
public void Run () {
Support2.dosomething ();
}
};

Thread third = new Thread (runnable);
Thread fourth = new Thread (runnable);

Executetest (third, fourth);
}

/**
* Make thread a run faster than thread B,
* Then thread B is interruted after about 1s.
* @param a
* @param b
*/
public static void Executetest (thread A, thread B) {
A.start ();
try {
Thread.Sleep (100);
B.start (); The main thread sleep 100ms, and then start the second thread.

Thread.Sleep (1000);
1s later, the main thread decided not to allow the second thread wait any longer.
B.interrupt ();
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

Abstract class Samplesupport {

protected int counter;

/**
* A Simple countdown,it'll stop after about 5s.
*/
public void Startthecountdown () {
Long currenttime = System.currenttimemillis ();
for (;;) {
Long diff = System.currenttimemillis ()-currenttime;
if (diff > 5000) {
Break
}
}
}
}

Class SampleSupport1 extends Samplesupport {

Private final Reentrantlock lock = new Reentrantlock ();

public void DoSomething () throws Interruptedexception {
Lock.lockinterruptibly (); (1)
System.out.println (Thread.CurrentThread (). GetName () + "'ll execute counter++.");
Startthecountdown ();
try {
counter++;
}
finally {
Lock.unlock ();
}
}
}

Class SampleSupport2 extends Samplesupport {

Public synchronized void DoSomething () {
System.out.println (Thread.CurrentThread (). GetName () + "'ll execute counter++.");
Startthecountdown ();
counter++;
}
}

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.