小练习:
首先是一个不太好的示范,虽然结果是正确的,但是很繁琐,且没有灵活发挥condition的作用。
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
Lock lock =new ReentrantLock();
Condition condition =lock.newCondition();
AtomicInteger a =new AtomicInteger(0);
AtomicInteger b =new AtomicInteger(0);
AtomicInteger c =new AtomicInteger(0);
public void out5(int i){
lock.lock();
try {
while (c.get()!=(15*i)){
condition.await();
}
for (int i1= 0;i1<5;i1++){
System.out.println("AAA"+i1);
}
a.compareAndSet(a.get(), 5*(i+1));
condition.signalAll();
System.out.println(a.get());
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void out10(int i){
lock.lock();
try {
while (a.get()!=(5*i)){
condition.await();
}
for (int i1= 0;i1<10;i1++){
System.out.println("BBB"+i1);
}
b.compareAndSet(b.get(), 10*(i));
System.out.println(b.get());
condition.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void out15(int i){
lock.lock();
try {
while (b.get()!=(10*(i-1))){
condition.await();
}
for (int i1= 1;i1<=15;i1++){
System.out.println("CCC"+i1);
}
c.compareAndSet(c.get(), 15*(i-1));
System.out.println(c.get());
condition.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test test =new Test();
new Thread(()->{
for (int i = 0;i<10;i++){
test.out5(i);
}
}, "AAA").start();
new Thread(()->{
for (int i = 1;i<11;i++){
test.out10(i);
}
}, "BBB").start();
new Thread(()->{
for (int i = 2;i<12;i++){
test.out15(i);
}
},"CCC" ).start();
}
}
现在是正确示范
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test1 {
static int num =1;
private Lock lock =new ReentrantLock();
Condition condition1 =lock.newCondition();
Condition condition2 =lock.newCondition();
Condition condition3 =lock.newCondition();
public void print5() {
lock.lock();
try {
while (num!=1){
try {
condition1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i =1;i<5;i++){
System.out.println("AAA"+i);
}
num=2;
condition2.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void print10() {
lock.lock();
try {
while (num!=2){
try {
condition2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i =1;i<=10;i++){
System.out.println("BBB"+i);
}
num=3;
condition3.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void print15() {
lock.lock();
try {
while (num!=3){
try {
condition3.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i =1;i<=15;i++){
System.out.println("CCC"+i);
}
num=1;
condition1.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
Test1 test1 =new Test1();
new Thread(()->{
for (int i = 0;i<10;i++) {
test1.print5();
}
}, "AAA").start();
new Thread(()->{
for (int i = 0;i<10;i++){
test1.print10();
}
}, "BBB").start();
new Thread(()->{
for (int i = 0;i<10;i++){
try {
test1.print15();
} catch (Exception e) {
e.printStackTrace();
}
}
},"CCC" ).start();
}
}