Android java.lang.IllegalMonitorStateException: object not locked by thread before wait()

后端 未结 4 775
夕颜
夕颜 2020-12-19 02:13

I define a global static object as a synchronization lock.

public static Object ConfirmationSynObj = new Object();

The following function i

4条回答
  •  误落风尘
    2020-12-19 02:48

    @Kayaman speaks correctly, as far as I can tell, however if I may humbly suggest: java.util.concurrent can save you lots of time!

    What I'd use there is a semaphore.

    From the docs: "Each acquire() blocks if necessary until a permit is available, and then takes it.".

    But there are other choices too- I strongly recommend using this where possible, as you should avoid lots of pit falls as in your case.

            Semaphore semaphore = new Semaphore(0);
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    //this is a http request
                    appSignInfo = getAPKSignature(context, pkinfo.packageName);
                    semaphore.release();
                }
            }).start();
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    

提交回复
热议问题