如何使两个线程等待对方的结果?

时间:2015-11-21 09:40:31

标签: java multithreading

这是我的示例代码。

class A implements Runnable{

//stuff
Thread thr = new Thread(this);
boolean flag;
public void run()
{
    while(true){
        if(condition)flag = true;
    }
}
}



class B implements Runnable{

//stuff
A a = new A();
Thread thr = new Thread(this);
public void run()
{
    while(true){
    //i ll do some thing here
    if(a.flag == true)System.out.println("Kaboom");
    }
}
public static void main(String[] args)
{
    B b = new B();
}
}

所以事情就是我在a之前开始b,我希望b等到a.flag == true to fire" Kaboom"当a在run()方法中工作时,a .thr必须等待。我尝试了这个,但它不起作用

class A implements Runnable{

//stuff
Thread thr = new Thread(this);
boolean flag;
public void run()
{
    while(true){
        if(condition)flag = true;
    synchronized(B.class){

        this.flag=true;
        B.class.notifyAll();
    }
    }
}
}



class B implements Runnable{

//stuff
A a = new A();
Thread thr = new Thread(this);
public void run()
{
    while(true){
    synchronized(this){

        while(a.flag!=true)
        {
            this.wait();
        }}
    }
}
public static void main(String[] args)
{
    B b = new B();
}}

我的同步块一定有问题,但我不知道是什么。
这可能是一个愚蠢的问题,但我只是JAVA的初学者,我真的没有得到那些Thread的东西以及它是如何工作的。 Plz帮帮我

2 个答案:

答案 0 :(得分:1)

我喜欢你使用wait / notifyAll的原始方法,使得线程不会使用CPU,直到满足条件才能恢复运行。这是一种保持这种方法的解决方案。

一些注意事项:

1 - 在类对象上进行同步时要小心。除非你真的想要同步整个类,否则创建一个Object并将其用作锁。

2 - 使用volatile关键字确保Java不会创建变量的线程本地版本,并且对其变化的值会立即反映到其他线程。

public class Threads {
    private final Object lock = new Object();
    private volatile boolean flag;

    class RunnableA implements Runnable {
        private volatile boolean condition = false;
        @Override
        public void run() {
            while (true) {
                if (condition) {
                    if (!flag) {
                        synchronized (lock) {
                            System.out.println("Setting Flag to True");
                            flag = true;
                            lock.notifyAll();
                        }
                    }
                } else {
                    System.out.println("Condition is False");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {

                    }
                }
            }
        }
    }

    class RunnableB implements Runnable {
        @Override
        public void run() {
            while (true) {
                while (flag == false) {
                    synchronized (lock) {
                        if (flag == false) {
                            try {
                                lock.wait();
                            } catch (InterruptedException ex) {

                            }
                        }
                    }
                }
                System.out.println("Kaboom");
            }
        }
    }

    public void run() {
        RunnableA runnableA = new RunnableA();
        RunnableB runnableB = new RunnableB();
        Thread t1 = new Thread(runnableA);
        Thread t2 = new Thread(runnableB);
        t1.start();
        t2.start();
        try {
            Thread.sleep(5000L);
        } catch (InterruptedException ex) {

        }
        runnableA.condition = true;

    }
    public static void main(String[] args) {
        new Threads().run();
    }
}

答案 1 :(得分:0)

您创建了一个Runnable但未在线程中启动它。

正确的一个:

UILabel
相关问题