目录
纯手记
1.ReentrantLock
2.Synchronized
3.Semaphore
结束
package com.company.demo16;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class HelloTest {
private volatile static int flag = 1;
public static void main(String[] args) throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();
Condition conditionB = lock.newCondition();
Condition conditionC = lock.newCondition();
new Thread(new Worker("C", lock, conditionC, conditionA, 10, 3)).start();
new Thread(new Worker("A", lock, conditionA, conditionB, 10, 1)).start();
new Thread(new Worker("B", lock, conditionB, conditionC, 10, 2)).start();
}
static class Worker implements Runnable {
private String name;
private ReentrantLock lock;
private Condition current;
private Condition next;
private Integer count;
private Integer targetFlag;
public Worker(String name, ReentrantLock lock, Condition current, Condition next, Integer count, Integer targetFlag) {
this.name = name;
this.lock = lock;
this.current = current;
this.next = next;
this.count = count;
this.targetFlag = targetFlag;
}
@Override
public void run() {
while (true) {
lock.lock();
while (flag != targetFlag) {
try {
current.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag++;
if (flag > 3) {
flag = 1;
}
next.signal();
lock.unlock();
}
/*for (Integer integer = 0; integer < count; integer++) {
lock.lock();
while (flag != targetFlag) {
try {
current.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag++;
if (flag > 3) {
flag = 1;
}
next.signal();
lock.unlock();
}*/
}
}
}
package com.company.demo16;
import java.util.concurrent.TimeUnit;
public class HelloTest {
private volatile static int flag = 1;
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();
new Thread(new Worker("C", lock, 10)).start();
new Thread(new Worker("A", lock, 10)).start();
new Thread(new Worker("B", lock, 10)).start();
}
static class Worker implements Runnable {
private String name;
private Object lock;
private Integer count;
public Worker(String name, Object lock, Integer count) {
this.name = name;
this.lock = lock;
this.count = count;
}
@Override
public void run() {
while (true) {
synchronized (lock) {
int value = 0;
if ("A".equals(name)) {
value = 1;
} else if ("B".equals(name)) {
value = 2;
} else {
value = 3;
}
while (flag != value) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ("A".equals(name)) {
flag = 2;
} else if ("B".equals(name)) {
flag = 3;
} else {
flag = 1;
}
lock.notifyAll();
}
}
/*for (Integer integer = 0; integer < count; integer++) {
synchronized (lock) {
int value = 0;
if ("A".equals(name)) {
value = 1;
} else if ("B".equals(name)) {
value = 2;
} else {
value = 3;
}
while (flag != value) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ("A".equals(name)) {
flag = 2;
} else if ("B".equals(name)) {
flag = 3;
} else {
flag = 1;
}
lock.notifyAll();
}
}*/
}
}
}
package com.company.demo16;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class HelloTest {
private volatile static int flag = 1;
public static void main(String[] args) throws InterruptedException {
Semaphore semaphoreA = new Semaphore(1);
Semaphore semaphoreB = new Semaphore(0);
Semaphore semaphoreC = new Semaphore(0);
new Thread(new Worker("C", semaphoreC, semaphoreA, 1)).start();
new Thread(new Worker("A", semaphoreA, semaphoreB, 1)).start();
new Thread(new Worker("B", semaphoreB, semaphoreC, 1)).start();
}
static class Worker implements Runnable {
private String name;
private Semaphore current;
private Semaphore next;
private Integer count;
public Worker(String name, Semaphore current, Semaphore next, Integer count) {
this.name = name;
this.current = current;
this.next = next;
this.count = count;
}
@Override
public void run() {
while (true) {
try {
current.acquire();
System.out.println(name);
TimeUnit.SECONDS.sleep(1);
next.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*for (Integer integer = 0; integer < count; integer++) {
try {
current.acquire();
System.out.println(name);
next.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
}
}
}