我想在第一个线程完成后启动第二个线程。但是,如果线程的实现方式不同,我无法弄清楚如何使用wait()/ notify()函数。 首先尝试在单独的类中使用它,但是当它完成时我无法获得第一个线程信号。
public class Oblig1 {
static void threadMessage(String message) {
String threadName =
Thread.currentThread().getName();
System.out.format("%s: %s%n",
threadName,
message);
}
private boolean start = false;
public void StartThread(){
start = true;
}
class Thread1 implements Runnable{
private int X;
Thread2 obj = new Thread2(5);
public Thread1(int x) {
X = x;
}
public synchronized void run() {
for (int i=1; i<21; i++) {
System.out.print(X*i + " ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
StartThread();
notifyAll();
}
}
class Thread2 extends Thread {
private int X;
public Thread2(int x) {
X = x;
}
public synchronized void run() {
while (!start){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i=1; i<21; i++) {
System.out.print(X*i + " ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
notifyAll();
}
}
public static void main(String [ ] args) {
int inputT1 = 3;
int inputT2 = 5;
Thread t1 = new Thread(new Thread1(inputT1));
Thread t2 = new Thread(new Thread2(inputT2));
t1.start();
t2.start();
}
}
答案 0 :(得分:1)
你在某个对象上等待并通知某个对象(在你当前的代码中你没有指定对象,所以它是this
)。结果,一个人调用了自己的通知,但没有人在等待它。虽然Thread2正在等待它自己,但没有人唤醒它(因为没有人在这个Thread2实例上调用notify)。
要唤醒Thread2,你需要在该对象上调用notify(它的这个),这样你的Thread1应该调用obj.notify()(因为obj是代码中的Thread2)。
然而它仍然无法工作,因为你没有将Thread2实例传递给Thread1(你只是在Thread1中创建一个新的),所以你通知的thread2刚刚创建并且从未启动过。您的主程序中的Thread2已启动,但从未通知。
对代码的可能修复
static class Thread1 extends Thread {
private int X;
final Thread2 second;
public Thread1(int x,Thread2 second) {
X = x;
this.second = second;
}
public void run() {
for(){
//....
}
second.start = true;
second.notify();
}
}
static class Thread2 extends Thread {
private int X;
public boolean start = false;
public Thread2(int x) {
X = x;
}
public void run() {
while(!start){
synchronized(this) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for(){
....
}
}
}
public static void main(String [ ] args)
{
int inputT1 = 3;
int inputT2 = 5;
Thread2 t2 = new Thread2(inputT2);
Thread1 t1 = new Thread1(inputT1,t2);
t1.start();
t2.start();
}
答案 1 :(得分:0)
wait()
和notify()
方法是低级原语,最好避免使用更高级别的并发API。滚动自己的并发代码可能会有问题(例如,代码中的start
字段需要是易变的!)。
一个简单的解决方法是使用CountDownLatch
。替换这个:
private boolean start = false;
有了这个:
private final CountDownLatch latch = new CountDownLatch(1);
然后,您可以拨打notifyAll()
latch.countDown();
Thread2的run
方法如下所示:
public void run() {
latch.await();
for (int i=1; i<21; i++) {
System.out.print(X*i + " ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
threadMessage("Done");
}
答案 2 :(得分:0)
使用lock / notify:
一个接一个地运行的2个线程的示例public class Thread2Test {
public static void main(String[] args)
{
final Object sync = new Object();
final Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized(lock) {
synchronized(sync) {
sync.notify(); // Notify main() to let it start t2.
}
for (int n=0; n<10; n++)
{
System.out.println(n);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
System.out.println("t1 finished.");
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("t2 started...");
synchronized(lock) {
// don't need to do anything but wait for lock to be released
}
char c = 'a';
for (int n=0; n<10; n++)
{
System.out.println(c);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
c++;
}
System.out.println("t2 finished.");
}
});
try {
System.out.println("Wait for t1 to start...");
synchronized(sync) {
t1.start();
sync.wait();
}
} catch (InterruptedException e) {
}
System.out.println("end wait");
t2.start();
}
请注意,同步对象用于保证t1始终在t2之前启动。