【问题标题】:trying to stop execution for 2 seconds试图停止执行 2 秒
【发布时间】:2014-09-23 02:57:24
【问题描述】:

我有一个按钮,单击时会调用一个启动线程的方法。但是那个线程有停止,很好。我希望它先完成线程,然后执行其余代码。

我尝试使用 wait() 但不起作用。

public void configure(Button arg0,TextView arg1) throws InterruptedException{

    //calling OpenDeviceListener
    readText=arg1;
    button= arg0;

    //handler
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d("Handler","running");
            if (actualNumBytes != 0x00) {
                Log.d("handler","if");
                readText.append(String.copyValueOf(readBuffer, 0,
                        actualNumBytes));
                Log.d("handler","if 312");
                actualNumBytes = 0;
                Log.d("handler","if end");
            }
            Log.d("handler","closed");
        }
    };


    Log.d("163","tab");
    uartInterface = new CH34xAndroidDriver(
            (UsbManager) getSystemService(Context.USB_SERVICE), this,
            ACTION_USB_PERMISSION);
    Log.d("167","tab");
    act_string = getIntent().getAction();
    if(-1 != act_string.indexOf("android.intent.action.MAIN"))
    {
        Log.d(TAG, "android.intent.action.MAIN 171");
    } 
    else if(-1 != act_string.indexOf("android.hardware.usb.action.USB_DEVICE_ATTACHED"))
    {
        Log.d(TAG, "android.hardware.usb.action.USB_DEVICE_ATTACHED 175");
    }

    if(!uartInterface.UsbFeatureSupported())
    {
        Toast.makeText(this, "No Support USB host API", Toast.LENGTH_SHORT).show();
        Log.d("182","tab");
        readText.setText("No Support USB host API");
        Log.d("184","tab");
        uartInterface = null;
    }

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    if(READ_ENABLE == false) {
        READ_ENABLE = true;
        Log.d("192","tab");
        handlerThread = new readThread(handler);
        handlerThread.start();
    }
    Log.d("192","End");
    try{
    this.wait(100);
    }
    catch(InterruptedException e){
        e.printStackTrace();
        Log.d("Exception",e.getMessage());
    }
    // TODO Auto-generated method stub
                Log.d("205","OpenDeviceListener");
                boolean flags;
                if(false == isConfiged) {
                    Log.d("209","OpenDeviceListener");
                    Toast.makeText(global_context,""+((Boolean)uartInterface.isConnected()), Toast.LENGTH_LONG).show();
                    isConfiged = true;
            //      writeButton.setEnabled(true);
                    if(uartInterface.isConnected()) {
                        Toast.makeText(global_context,""+(Boolean)uartInterface.isConnected(), Toast.LENGTH_SHORT).show();
                        Log.d("213","OpenDeviceListener");
                        flags = uartInterface.UartInit();
                        if(!flags) {
                            Log.d(TAG, "Init Uart Error 2");
                            Toast.makeText(global_context, "Init Uart Error", Toast.LENGTH_SHORT).show();
                        } else {
                            if(uartInterface.SetConfig(baudRate, dataBit, stopBit, parity, flowControl)) {
                                Log.d(TAG, "Configed");
                            }
                        }
                    }

                }
}

并请清除线程不调用 stop() 的一件事,直到线程将运行。 谢谢

【问题讨论】:

    标签: java android multithreading


    【解决方案1】:
        final Runnable runnable = new Runnable() {
    
                            @Override
                            public void run() {
    //this method will be called after 2 seconds
                                //do your task here
                            }
                        };
                        final Handler h = new Handler();
                        h.removeCallbacks(runnable); // cancel the running action (the
                                                        // hiding process)
                        h.postDelayed(runnable, 2000);
    

    【讨论】:

      【解决方案2】:

      试试这个:

      Object lock = new Object;
      synchronized(lock){
          lock.wait();
      }
      

      这将阻止当前线程运行这些代码。 什么时候通知被阻塞的代码

      synchronized (lock) {
          lock.notify();
      }
      

      如果只想让线程休眠特定时间为什么不使用

      Thread.sleep(1000);
      

      【讨论】:

      • 不,我不想停止线程,我想停止执行威胁调用之后的主代码。我是否应该放置一个新线程类并使用 wait() 在新线程中运行它。
      • 在你的主线程中启动一个线程首先使用thread.start()运行它;然后 thread.join();那么主线程将在您的线程完成后继续运行。