yutaoaijingjing132 2011-09-26 10:41
浏览 266
已采纳

关于多线程问题,signalAll,await问题

 

package ThreadTest;

 

import java.util.concurrent.TimeUnit;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

public class AwaitTest1 extends Thread{

private final Lock lock = new ReentrantLock();

private Condition waitCondition = lock.newCondition();

public static void main(String[] args) throws InterruptedException {

new AwaitTest1().threads();

}

public void threads() throws InterruptedException {

Thread t1 = new Thread(){

public void run(){

try {

lock.lock();

System.out.println("do somthing xxx long time");

waitCondition.signalAll();

System.out.println("sign other");

Thread.sleep(500);

lock.unlock();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

};

Thread t2 = new Thread(){

public void run(){

System.out.println(waitResult());

}

};

t1.start();

t2.start();

}

public Boolean waitResult() {

System.out.println("x");

lock.lock();

Boolean result = false;

try {

System.out.println("y");

result = waitCondition.await(1000, TimeUnit.MILLISECONDS);

} catch (InterruptedException e) {

e.printStackTrace();

}

lock.unlock();

return result;

}

}

 

说下我写这个目的,我也是才学学习多线程,在此遇到了不少问题,我本来想些个异步的通信 就是说当我运行线程T1的时候,他唤醒T2,就像是Future那个异步回调,但是不知道该如何写,也不知道这个result什么时候返回true,我感觉是当线程T1完成复杂的计算后,然后在await在没有超时的情况下,会返回为true,但是现在我程序写的有问题,请各位帮忙解决下,谢谢


  • 写回答

1条回答 默认 最新

  • _1_1_7_ 2011-09-26 11:28
    关注

    要想获得你预期的结果,就必须是
    t2先获得锁(lock.lock()),然后执行 waitCondition.await,释放锁并等待信号;t1获得锁执行完相关操作后执行waitCondition.signalAll,t2从而获得锁继续执行,此时如果等待时间未超时就返回true.
    await时间应该比t1执行时间长。
    [code="java"]

    import java.util.concurrent.TimeUnit;

    import java.util.concurrent.locks.Condition;

    import java.util.concurrent.locks.Lock;

    import java.util.concurrent.locks.ReentrantLock;

    public class AwaitTest {

    private final Lock lock = new ReentrantLock();
    
    private Condition waitCondition = lock.newCondition();
    
    public static void main(String[] args) throws InterruptedException {
    
        new AwaitTest().threads();
    
    }
    
    public void threads() throws InterruptedException {
        final Thread t1 = new Thread() {
            public void run() {
                try {
                    lock.lock();
                    System.out.println("do somthing...");
                    Thread.sleep(1000);
                    waitCondition.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        };
    
        Thread t2 = new Thread() {
            public void run() {
    
                System.out.println(waitResult());
    
            }
        };
    
    
        t2.start();
        //保证t2先开始
        Thread.sleep(1000);
        t1.start();
    }
    
    public boolean waitResult() {
        boolean result = false;
        lock.lock();
        try {
            result = waitCondition.await(3000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return result;
    }
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来