方法一:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadTest {
private static int task = 0;
public static void main(String args[]) {
final Lock lock = new ReentrantLock();
Thread th1 = new Thread(new Runnable() {
public void run() {
while (true) {
lock.lock();
if (task % 3 == 0) {
System.out.println("1");
task++;
}
lock.unlock();
}
}
});
Thread th2 = new Thread(new Runnable() {
public void run() {
while (true) {
lock.lock();
if (task % 3 == 1) {
System.out.println("2");
task++;
}
lock.unlock();
}
}
});
Thread th3 = new Thread(new Runnable() {
public void run() {
while (true) {
lock.lock();
if (task % 3 == 2) {
System.out.println("3");
task++;
}
lock.unlock();
}
}
});
th1.start();
th2.start();
th3.start();
}
}