Lock's Lock () method

Source: Internet
Author: User
Tags finally block

Reentrantlock is the only class that JDK implements the lock interface

Lock ()

One of the most commonly used methods is to acquire locks. Waits if the lock has been acquired by another thread.
As mentioned earlier, if lock is used, the lock must be released voluntarily, and the lock will not be released automatically when an exception occurs. Therefore, in general, lock must be used in the try{}catch{} block, and the release of the lock will be placed in the finally block to ensure that the lock must be released to prevent the occurrence of deadlocks.


instance The lock objects of the two threads of the respective lock objects are independent of each other, without any relationship, and do not play a role in the lock

@Test public void TestLock1 () {//Two thread lock objects are independent of each other, have no relationship, do not play a role in the lock new thread () {@Override public void
				Run () {lock lock = new Reentrantlock ();
				String Tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ..."); Lock.lock ()///Active Acquisition lock SYSTEM.OUT.PRINTLN (tname+ "acquire to lock").

				");
					try {int n = 1/0;

					SYSTEM.OUT.PRINTLN (n);
					for (int i = 0; i < i++) {System.out.println (tname+ ":" + i); The catch (Exception e) {System.out.println (tname+) is wrong ...
				");
				}finally {//lock.unlock ();

		}}.start ();
				New Thread () {@Override public void run () {lock lock = new Reentrantlock ();
				String Tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ...");

				Lock.lock (),//Active acquisition of the lock, the lock is not available, because thread 1 error, lock () does not actively release the lock, thread 1 and did not release the lock, so the deadlock. System.out.println (tname+ "Tname" gets to the lock.

				"); for (int i = 0; i < i++) {System.out.println (tname+ ":" + i);
	}}.start (); }

Run Result:

Thread-0 is not locked yet, and starts to acquire the lock actively ...
Thread-0 gets to the lock.
Thread-0 made a mistake ...
Thread-1 is not locked yet, and starts to acquire the lock actively ...
Thread-1tname gets to the lock.
thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9


the lock object is the same

The lock object for two threads is the same, and when the thread that acquires the lock does not release the lock, a deadlock occurs, and the other threads can wait endlessly

@Test public void TestLock2 () {//two threads have the same lock object, a deadlock occurs when the thread acquiring the lock does not release the lock, and other threads can only wait endlessly for lock lock = new Reentrantlock ();
				New Thread () {@Override public void run () {String tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ..."); Lock.lock ()///Active Acquisition lock SYSTEM.OUT.PRINTLN (tname+ "acquire to lock").

				");
					try {int n = 1/0;

					SYSTEM.OUT.PRINTLN (n);
					for (int i = 0; i < i++) {System.out.println (tname+ ":" + i); The catch (Exception e) {System.out.println (tname+) is wrong ...
				");

		}finally {}}}.start ();
				New Thread () {@Override public void run () {String tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ...");

				Lock.lock ()//Active acquisition of the lock, the lock is not available at this time, because there was an error, lock () does not actively release the lock, the thread did not release the lock, so the deadlock. System.out.println (tname+ "Tname" gets to the lock.

				");
				for (int i = 0; i < i++) {System.out.println (tname+ ":" + i);
	}}.start ();
	 }


Run Result:

Thread-0 is not locked yet, and starts to acquire the lock actively ...
Thread-0 gets to the lock.
Thread-0 made a mistake ...
Thread-1 is not locked yet, and starts to acquire the lock actively ...

Release Lock

The lock object for two threads is the same, and when the thread that acquires the lock does not release the lock, a deadlock occurs, and the other threads can wait endlessly

In the try-finally, whether it is a smooth execution, or error, you have to actively release the lock, then other threads can get

@Test public void TestLock3 () {//Two thread lock objects are the same one, when the thread that acquires the lock does not release the lock, it creates a deadlock, and the other threads can only wait endlessly/in the try-finally, whether it is a smooth execution or an error, take the initiative

		Release the lock, the other thread can get lock lock = new Reentrantlock ();
				New Thread () {@Override public void run () {String tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ..."); Lock.lock ()///Active Acquisition lock SYSTEM.OUT.PRINTLN (tname+ "acquire to lock").

				");
					try {int n = 1/0;

					SYSTEM.OUT.PRINTLN (n);
					for (int i = 0; i < i++) {System.out.println (tname+ ":" + i); The catch (Exception e) {System.out.println (tname+) is wrong ...
				"); }finally {System.out.println (tname+) release lock.
					");
				Lock.unlock ();

		}}.start ();
				New Thread () {@Override public void run () {String tname=thread.currentthread (). GetName ();

				System.out.println (tname+ "Not yet locked, start to take the initiative to acquire the lock ...");

				Lock.lock ()//Active acquisition of the lock, the lock is not available at this time, because there was an error, lock () does not actively release the lock, the thread did not release the lock, so the deadlock. System.out.println (tname+ "Tname" gets to the lock.

				"); for (int i = 0; I < 10;
				i++) {System.out.println (tname+ ":" + i);
	}}.start (); }


Run Result:

Thread-0 is not locked yet, and starts to acquire the lock actively ...
Thread-0 gets to the lock.
Thread-0 made a mistake ...
Thread-0 release lock.
Thread-1 is not locked yet, and starts to acquire the lock actively ...
Thread-1tname gets to the lock.
thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9



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.