Binding lock multiple conditions Condition
Manufacturers face questions:
We all know that ArrayList is not thread-safe, please write coding unsafe and gives the case a solution?
Lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.
CountDownLatch, CyclicBarrier, Semaphore used it?
Blocking queue know?
Thread pool used it? ThreadPoolExecutor talk about your understanding?
Thread pool used it? Production How do you set reasonable parameters?
Deadlock coding and positioning analysis?
Binding a plurality of lock code verification if Condition
package com.wwl.juc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Shared resource
*/
class ShareResource {
// A:1 B:2 C:30
private int num = 1;
private Lock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
private Condition conditionC = lock.newCondition();
// loop to print 5 times
public void print5() {
1 // acquiring a lock resources
lock.lock();
try {
// 2, to determine whether the execution of business
while (num != 1) {
conditionA.await();
}
// analog service execution
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "\t" + (i+1));
}
// 3, notify other threads
a = 2;
conditionB.signal();
} catch (InterruptedException e) {
e.printStackTrace ();
} finally {
lock.unlock();
}
}
// loop to print 10 times
public void print10() {
1 // acquiring a lock resources
lock.lock();
try {
// 2, to determine whether the execution of business
while (num != 2) {
conditionB.await();
}
// analog service execution
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "\t" + (i+1));
}
// 3, notify other threads
a = 3;
conditionC.signal();
} catch (InterruptedException e) {
e.printStackTrace ();
} finally {
lock.unlock();
}
}
// loop to print 15 times
public void print15() {
1 // acquiring a lock resources
lock.lock();
try {
// 2, to determine whether the execution of business
while (num != 3) {
conditionC.await();
}
// analog service execution
for (int i = 0; i < 15; i++) {
System.out.println(Thread.currentThread().getName() + "\t" + (i+1));
}
// 3, notify other threads
a = 1;
conditionA.signal();
} catch (InterruptedException e) {
e.printStackTrace ();
} finally {
lock.unlock();
}
}
}
/**
* Lock bind multiple conditions Condition
* Title: Multi-threaded execution order between realized A-> B-> C three thread starts, the following:
* A printed 5 times, B printing 10, C 15 times to print,
* Then
* A printed 5 times, B printing 10, C 15 times to print,
* .
* .
* .
* Loop executes 10
*/
public class LockConditionDemo {
public static void main(String[] args) {
Share Share Resource Resource Resource Share = new ();
new Thread(()->{
for(int i = 0; i < 10; i++){
shareResource.print5 ();
}
},"A").start();
new Thread(()->{
for(int i = 0; i < 10; i++){
shareResource.print10 ();
}
},"B").start();
new Thread(()->{
for(int i = 0; i < 10; i++){
shareResource.print15 ();
}
},"C").start();
}
}
Program execution results are as follows:
Connected to the target VM, address: '127.0.0.1:52817', transport: 'socket'
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
C 1
C 2
C 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
C 11
C 12
C 13
C 14
C 15
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
C 1
C 2
C 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
C 11
C 12
C 13
C 14
C 15
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
C 1
C 2
C 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
C 11
C 12
C 13
C 14
C 15
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
B 6
B 7
B 8
B 9
B 10
C 1
C 2
C 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
C 11
C 12
C 13
C 14
C 15
.
.
.
.
.
.