0

我不明白为什么我不能启动一个线程,我该怎么做?

    scheduleTaskExecutor = Executors.newScheduledThreadPool(1);
        scheduleTaskExecutor.scheduleAtFixedRate(runUpdatePreOrders, 0,
                timeRepeat, TimeUnit.MILLISECONDS);

private Runnable runUpdatePreOrders = new Runnable() {
        public void run() {
            synchronized  (runUpdatePreOrders) {

                    try {
                        if (pause) wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                new UpdateOrders(min_price, listener).execute();
                Log.e("UpdateBrodcast", "StartPreOrder");
            }
        }
    };

@Override
    public void onStart() {
        super.onStart();

        if (pause) {
            runUpdatePreOrders.notify();   // this IllegalMonitorStateException: object not locked by thread before
        }

        pause = false;


    }
4

1 回答 1

4
      synchronized  (runUpdatePreOrders) {

                try {
                    if (pause) runUpdatePreOrders.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            new UpdateOrders(min_price, listener).execute();
            Log.e("UpdateBrodcast", "StartPreOrder");
        }
    }

如果你在一个对象上同步,你应该在同一个对象上调用 wait 和 notify

编辑

  if (pause) {
      synchronized  (runUpdatePreOrders) {
            runUpdatePreOrders.notify();   // this IllegalMonitorStateException: object not locked by thread before
       }
   }
于 2013-02-15T10:24:55.883 回答