我想知道如何使这些线程一个接一个地工作,以及如何使第一个线程在第二个线程之后始终运行。我创建了一个信号量,但我不确定如何使用它。使用信号灯
public class A extends Thread {
private int i;
public A(int x) {
i = x;
}@
Override
public void run() {
System.out.println(i);
}
}
/**主**/
A c1 = new A(5);
A c2 = new A(6);
Semaphore s = new Semaphore(1, true);
c1.start();
c2.start();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
System.out.println("+");
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(70);
} catch (InterruptedException e) {}
System.out.println("*");
}
}
}).start();
一个其他=连续之后。为什么你想要多个线程? –
为了行使我在控制线程 – Csi
这没有任何意义。开始第一个线程。 '加入'就可以了。然后'开始'第二个线程。然后'加入'。这里没有理由使用“信号量”。 –