IllegalMonitorStateException:对象之前未被线程锁定

时间:2013-02-15 10:19:13

标签: android

我不明白为什么我不能创建一个帖子,我怎么能这样做?

    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;


    }

1 个答案:

答案 0 :(得分: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并通知同一个对象

修改

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