Android同步线程

时间:2013-07-29 09:46:40

标签: android

我的代码有多个帖子和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是处理后我想要的值

2 个答案:

答案 0 :(得分:0)

您必须使用CallableRunnable接口不要将值传递给父方法。

请参阅此example

您可能需要使用Generic对象

答案 1 :(得分: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();
}
相关问题