wait¬ify实现多线程的顺序执行
package com. cxf. test ;
public class test5 {
public static void main ( String [ ] args) {
WaitNofity wn = new WaitNofity ( 5 , 1 ) ;
new Thread ( ( ) -> {
wn. print ( "a" , 1 , 2 ) ;
} ) . start ( ) ;
new Thread ( ( ) -> {
wn. print ( "b" , 2 , 3 ) ;
} ) . start ( ) ;
new Thread ( ( ) -> {
wn. print ( "c" , 3 , 1 ) ;
} ) . start ( ) ;
}
}
class WaitNofity {
private Integer loopNumber;
private Integer flag;
public WaitNofity ( Integer loopNumber, Integer flag) {
this . loopNumber = loopNumber;
this . flag = flag;
}
public WaitNofity ( ) {
}
public void print ( String str, Integer waitFlag, Integer nextFlag) {
for ( int i = 0 ; i < loopNumber ; i++ ) {
synchronized ( this ) {
while ( waitFlag!= flag) {
try {
this . wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
System . out. print ( str) ;
flag= nextFlag;
this . notifyAll ( ) ;
}
}
}
}
ReentrantLock的条件变量实现多线程的顺序执行
package com. cxf. test ;
import java. util. concurrent. TimeUnit ;
import java. util. concurrent. locks. Condition ;
import java. util. concurrent. locks. ReentrantLock ;
public class test6 {
public static void main ( String [ ] args) {
AwaitSinal awaitSinal = new AwaitSinal ( 5 ) ;
Condition A = awaitSinal. newCondition ( ) ;
Condition B = awaitSinal. newCondition ( ) ;
Condition C = awaitSinal. newCondition ( ) ;
new Thread ( ( ) -> awaitSinal. print ( "a" , A , B ) ) . start ( ) ;
new Thread ( ( ) -> awaitSinal. print ( "b" , B , C ) ) . start ( ) ;
new Thread ( ( ) -> awaitSinal. print ( "c" , C , A ) ) . start ( ) ;
try {
TimeUnit . SECONDS. sleep ( 1 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
awaitSinal. lock ( ) ;
try {
A . signalAll ( ) ;
} finally {
awaitSinal. unlock ( ) ;
}
}
}
class AwaitSinal extends ReentrantLock {
private Integer loopNumber;
public AwaitSinal ( Integer loopNumber) {
this . loopNumber= loopNumber;
}
public void print ( String str, Condition current, Condition next) {
for ( int i = 0 ; i < loopNumber ; i++ ) {
lock ( ) ;
try {
current. await ( ) ;
System . out. print ( str) ;
next. signalAll ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
unlock ( ) ;
}
}
}
}
park&unpark实现多线程的顺序执行
package com. cxf. test ;
import java. util. concurrent. TimeUnit ;
import java. util. concurrent. locks. LockSupport ;
public class test7 {
static Thread t1;
static Thread t2;
static Thread t3;
public static void main ( String [ ] args) {
ParkUnpark parkUnpark = new ParkUnpark ( 5 ) ;
t1= new Thread ( ( ) -> parkUnpark. print ( "a" , t2) ) ;
t2= new Thread ( ( ) -> parkUnpark. print ( "b" , t3) ) ;
t3= new Thread ( ( ) -> parkUnpark. print ( "c" , t1) ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
try {
TimeUnit . SECONDS. sleep ( 1 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
LockSupport . unpark ( t1) ;
}
}
class ParkUnpark {
private int loopNumber;
public ParkUnpark ( int loopNumber) {
this . loopNumber= loopNumber;
}
public void print ( String str, Thread next) {
for ( int i= 0 ; i< loopNumber; i++ ) {
LockSupport . park ( ) ;
System . out. print ( str) ;
LockSupport . unpark ( next) ;
}
}
}