0

我的代码有多个线程并且Runnable. Runnable我的问题是我在调用的线程中更改了某个变量的值。

调用后,我对该变量值进行检查,但尚未检索到该值。

处理后如何检索值?这是RunnableThread代码:

final Runnable r = new Runnable()
{
    public void run() 
    {
        if(flag==true)
        onSwipe();

        if(SwipeAgain==true)
        handler.postDelayed(this, 1000);
    }
};

private void onSwipe() {

    new Thread() {
        public void run() {
            String data = null;
            decryption_data = null;
            encryption_data = null;
            SwipeAgain=false;
            handler.post(clear_encryption);
            try {
                data = sreader.ReadCard(15000);

            } catch (Exception ex) {
                if (ex instanceof TimeoutException) {
                    return;
                } else
                CloseSinWave();
            }

            if (data == null) {
                SwipeAgain=true;
                encryption_data = sreader.GetErrorString();
                if (encryption_data.equalsIgnoreCase("cancel all"))
                    return;
                handler.post(display_encryptiondata);
            } else {
                encryption_data = "\n" + data;
                handler.post(display_encryptiondata);

    }.start();
}

SwipeAgain是我处理后想要的值

4

2 回答 2

0

You have to use Callable, Runnable interface do not pass values to the parent method.

See this example.

You may require to use Generic Objects

于 2013-07-29T09:56:52.343 回答
0

使用 MONITOR final Object 等待并在处理完成时通知它。

private final MONITOR Object[] = new Object[0];
private AtomicBoolean ready = new AtomicBoolean(false);

final Runnable r = new Runnable() {
    public void run() 
    {
        if(flag==true){
            ready.set(false);
            onSwipe();
            synchronized(MONITOR){
                if(!ready.get()){
                    try{
                        MONITOR.wait();  //will block until it get notified
                    }catch(InteruptedException e){}
                }
            }
        }
        if(SwipeAgain==true)
            handler.postDelayed(this, 1000);
    }
};

private void onSwipe() {

    new Thread() {
        public void run() {
          try{
            String data = null;
            decryption_data = null;
            encryption_data = null;
            SwipeAgain=false;
            handler.post(clear_encryption);
            try {
                data = sreader.ReadCard(15000);

            } catch (Exception ex) {
                if (ex instanceof TimeoutException) {
                    return;
                } else
                    CloseSinWave();
            }

            if (data == null) {
                SwipeAgain=true;
                encryption_data = sreader.GetErrorString();
                if (encryption_data.equalsIgnoreCase("cancel all"))
                    return;
                handler.post(display_encryptiondata);
            } else {
                encryption_data = "\n" + data;
                handler.post(display_encryptiondata);
          }finally{
              synchronized(MONITOR){
                  ready.set(true);
                  MONITOR.notifyAll();  //notify (and so unblock r.run())
              }
          }
    }.start();
}
于 2013-07-29T10:07:34.413 回答