wati和notify实现
public class Test {
public static void main ( String[ ] args) {
LockObject lock= new LockObject ( ) ;
Thread t1 = new Thread ( new Task ( lock, "A" ) ) ;
Thread t2 = new Thread ( new Task ( lock, "B" ) ) ;
Thread t3 = new Thread ( new Task ( lock, "C" ) ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
class LockObject {
volatile int count= 1 ;
}
class Task implements Runnable {
private LockObject lock;
private String str;
public Task ( LockObject lock, String str) {
this . lock= lock;
this . str= str;
}
@Override
public void run ( ) {
for ( int i= 1 ; i<= 10 ; i++ ) {
synchronized ( lock) {
try {
boolean flag= true ;
while ( flag) {
if ( "A" . equals ( this . str) && lock. count% 3 == 1 ) {
System. out. print ( str) ;
++ lock. count;
flag= false ;
lock. notify ( ) ;
lock. wait ( ) ;
} else if ( "B" . equals ( this . str) && lock. count% 3 == 2 ) {
System. out. print ( str) ;
++ lock. count;
flag= false ;
lock. notify ( ) ;
lock. wait ( ) ;
} else if ( "C" . equals ( this . str) && lock. count% 3 == 0 ) {
System. out. print ( str) ;
++ lock. count;
flag= false ;
lock. notify ( ) ;
lock. wait ( ) ;
} else {
lock. notify ( ) ;
lock. wait ( ) ;
}
if ( lock. count== 31 ) {
lock. notify ( ) ;
flag= false ;
}
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
LockSupprot实现
import java. util. concurrent. locks. LockSupport;
public class Test {
static Thread t1= null, t2= null, t3= null;
public static void main ( String[ ] args) {
t1= new Thread ( ( ) - > {
for ( int i= 1 ; i<= 10 ; i++ ) {
System. out. print ( "A" ) ;
LockSupport. unpark ( t2) ;
LockSupport. park ( ) ;
}
} ) ;
t2= new Thread ( ( ) - > {
for ( int i= 1 ; i<= 10 ; i++ ) {
LockSupport. park ( ) ;
System. out. print ( "B" ) ;
LockSupport. unpark ( t3) ;
}
} ) ;
t3= new Thread ( ( ) - > {
for ( int i= 1 ; i<= 10 ; i++ ) {
LockSupport. park ( ) ;
System. out. print ( "C" ) ;
LockSupport. unpark ( t1) ;
}
} ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
RettrentLock Condition
import java. util. concurrent. CountDownLatch;
import java. util. concurrent. locks. Condition;
import java. util. concurrent. locks. ReentrantLock;
public class Test {
static Thread t1= null, t2= null, t3= null;
public static void main ( String[ ] args) {
CountDownLatch latch= new CountDownLatch ( 1 ) ;
ReentrantLock reentrantLock= new ReentrantLock ( ) ;
Condition a = reentrantLock. newCondition ( ) ;
Condition b = reentrantLock. newCondition ( ) ;
Condition c = reentrantLock. newCondition ( ) ;
new Thread ( ( ) - > {
try {
reentrantLock. lock ( ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
System. out. print ( "A" ) ;
try {
latch. countDown ( ) ;
b. signal ( ) ;
a. await ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} finally {
b. signal ( ) ;
reentrantLock. unlock ( ) ;
}
} ) . start ( ) ;
new Thread ( ( ) - > {
try {
latch. await ( ) ;
reentrantLock. lock ( ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
System. out. print ( "B" ) ;
try {
c. signal ( ) ;
b. await ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
c. signal ( ) ;
reentrantLock. unlock ( ) ;
}
} ) . start ( ) ;
new Thread ( ( ) - > {
try {
latch. await ( ) ;
reentrantLock. lock ( ) ;
for ( int i= 0 ; i< 10 ; i++ ) {
System. out. print ( "C" ) ;
try {
a. signal ( ) ;
c. await ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
reentrantLock. unlock ( ) ;
}
} ) . start ( ) ;
}
}