题目:有3个线程,ID号分别为ABC,启动这3个线程,使其输出它的ID号3次,ABCABCABC。
思路一 要按照顺序输出ABC,循环3次,就要控制三个线程的同步,也就是说要让三个线程轮流输出,直到3个ABC全部输出则结束线程。这里可以使用Lock对象来控制三个线程的同步,用一个int型变量state标识由哪个线程输出。
public class ThreadTest {
private static int state = 0;
private static final Lock lock = new ReentrantLock();
public static void main(String[] args) throws Exception {
Thread A = new Thread(new Runnable() {
@Override
public void run() {
while (state <= 8) {
lock.lock();
//再进行一次 state <= 8的判断
if (state % 3 == 0 && state <= 8) {
System.out.println(Thread.currentThread().getName() + ":" + state);
state++;
}
lock.unlock();
}
}
}, "A");
Thread B = new Thread(new Runnable() {
@Override
public void run() {
while (state <= 8) {
lock.lock();
if (state % 3 == 1 && state <= 8) {
System.out.println(Thread.currentThread().getName() + ":" + state);
state++;
}
lock.unlock();
}
}
}, "B");
Thread C = new Thread(new Runnable() {
@Override
public void run() {
while (state <= 8) {
lock.lock();
if (state % 3 == 2 && state <= 8) {
System.out.println(Thread.currentThread().getName() + ":" + state);
state++;
}
lock.unlock();
}
}
}, "C");
A.start();
B.start();
C.start();
}
}
思路二 上述思路中引入了一个state来控制轮到谁来执行,同时也要保证在输出打印线程名之前再次判断state <= 8,防止打印次数超出。我们也可以给每个线程指定信号量Semaphore来控制输出的顺序,实现代码如下:
public class ThreadTest {
private static Semaphore semaphoreA = new Semaphore(1);
private static Semaphore semaphoreB = new Semaphore(1);
private static Semaphore semaphoreC = new Semaphore(1);
public static void main(String[] args) throws Exception {
Thread A = new Thread(new Runnable() {
@Override
public void run() {
for (int state = 0; state < 3; state++) {
try {
semaphoreA.acquire();
System.out.println(Thread.currentThread().getName() + ":" + state);
semaphoreB.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "A");
Thread B = new Thread(new Runnable() {
@Override
public void run() {
for (int state = 0; state < 3; state++) {
try {
semaphoreB.acquire();
System.out.println(Thread.currentThread().getName() + ":" + state);
semaphoreC.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "B");
Thread C = new Thread(new Runnable() {
@Override
public void run() {
for (int state = 0; state < 3; state++) {
try {
semaphoreC.acquire();
System.out.println(Thread.currentThread().getName() + ":" + state);
semaphoreA.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "C");
semaphoreB.acquire();
semaphoreC.acquire();
A.start();
B.start();
C.start();
}
}