Tag:obj cti Support count timestamp explaining pre his task
Java Concurrency Programming: The release of the lock table of Contents
- 1. Status of the thread
- 2. Wait () notify () and Notifyall ()
In the previous thread synchronization, we talked about how the lock was obtained. Next, let's talk about the release of the lock. First, the locked method or block of code runs, and the lock is definitely released.
So, what is the method by which the active release of the lock is achieved?
If we look at the method of the object class, we will notice that it has three strange methods: Wait (), notify (), and Notifyall (). They are used to release the lock.
1The state of the thread
Before we talk about the release of the lock, let's talk about the three main states of the thread: run, Ready (operational), blocking. Of course, in addition to this, there must be an initial state and an end state, that is not discussed.
When we create a thread, we just go into the initial state, and if we call the run () method, we don't start the new thread at all. When start () is called, the thread state can be changed to a running state, and then the operating system decides which thread to call. When you are lucky to be selected by the operating system, you can go into a real state of operation. When the runtime runs out of time slices, or the yield () comity method is called, the current execution right is handed out and entered into a ready-to-run state. If in the process of running, there is system IO, such as waiting for input, pop-up confirmation dialog box, etc., it will cause the current thread to go into a blocking state, cannot move. You can only wait until the user has been manipulated to proceed down. The sleep () method and the join () method can achieve similar blocking effects.
Then, we add the state of the object lock part. When we use the synchronized adornment method or block of code, we get the lock of the object and go to the waiting queue to get the lock of the object. When the object lock is acquired, it is ready to enter the operational state.
In addition, there is an object's wait () method that allows the thread to discard the lock held by the object and enter the notification wait state. When another thread calls the Notify () or Notifyall () method of the object that waits for the thread to be required, the thread re-enters the acquisition of the lock in the queue that gets the object lock.
2Wait () notify () and Notifyall ()
Synchronized how to get the lock we have already explained it in detail, so let's take a look at how to release the lock voluntarily. Let's say we want to create two threads, let these two threads, do one thing in turn, instead of starting at the same time, it's up to the operating system to determine their order of execution, so what do we do?
That is to first request a lock on the previous thread, then get the lock on your thread, release your lock, and notify the lock object of the wait queue (the next thread!). Hey, wake up don't sleep, get up and work! ), releases the lock on the previous thread, and enters the notification queue that waits for the previous lock object.
This is shown in code as:
Public class Syncdemo Implements Runnable { Private String name;Private Object prev;Private Object cur; Public Syncdemo(String name,Object prev,Object cur) { This. name = name; This. prev = prev; This. cur = cur;} @Override Public void Run() { int Count= 10; while (Count > 0) { synchronized(Prev) { synchronized(Cur) {System.out.println(Name); count--; Cur.notify();} Try {Prev.wait();} Catch (Exception e) {E.printstacktrace();} } } } Public Static void Main(String[] args) { Object a=New Object();Object b=New Object();Syncdemo R1=New Syncdemo("A", B, a);Syncdemo R2=New Syncdemo("B", A, B);New Thread(R1). Start();Try {Thread.Sleep(100);} Catch (Exception e) {E.printstacktrace();} New Thread(R2). Start();}}
Write a bit more generic and support more threads to execute in turn:
Public Static void Main(String[] args) { int Num= 5;Object[] Locks=New Object[Num];String[] names={"A","B","C","D","E"};Syncdemo[] runnable=New Syncdemo[Num]; for (int I= 0; I <Num; i++) {Locks[I]=New Object();} for (int I= 0; I <Num; i++) {Runnable[I]=New Syncdemo(Names[I], locks[(I-1 + num)% num], locks[I]);} for (int I= 0; I <Num; i++) { New Thread(Runnable[I]). Start();Try {Thread.Sleep(100);} Catch (Exception ex) {Ex.printstacktrace();} }}
Date:2017-07-05 22:31
Author:wen YANG
created:2017-07-06 Thu 23:14
Emacs 25.2.1 (ORG mode 8.2.10)
Validate
Java Concurrency Programming: the release of Locks