wait() 是否需要在局部变量上同步



>我有这段代码(工作正常):

public static void runOnUiThread(Activity c, final Runnable action) {
    // Check if we are on the UI Thread
    if (Looper.getMainLooper() == Looper.myLooper()) {
        // If we are, execute immediately
        action.run();
        return;
    } // Else run the runnable on the UI Thread and wait
    Runnable r = new Runnable() {
        @Override
        public void run() {
            action.run();
            synchronized (this) {
                this.notify();
            }
        }
    };
    synchronized (r) {
        try {
            c.runOnUiThread(r);
            r.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

我得到了Synchronization on local variable警告。按照建议,我删除了局部变量上的同步,以修复警告:

public static void runOnUiThread(Activity c, final Runnable action) {
    // Check if we are on the UI Thread
    if (Looper.getMainLooper() == Looper.myLooper()) {
        // If we are, execute immediately
        action.run();
        return;
    } // Else run the runnable on the UI Thread and wait
    Runnable r = new Runnable() {
        @Override
        public void run() {
            action.run();
            this.notify();
        }
    }
    try {
        c.runOnUiThread(r);
        r.wait();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

现在我在调用此方法时遇到异常:

06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               FATAL EXCEPTION: main
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               java.lang.IllegalMonitorStateException: object not locked by thread before notify()
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at java.lang.Object.notify(Native Method)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at org.matapps.android.simpleappcreator.Utils$100000007.run(Utils.java:673)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:615)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:137)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:4867)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at java.lang.reflect.Method.invokeNative(Native Method)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:511)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-15 09:18:13.252 27282 27282 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)

我很确定我需要同步(因为可运行的线程在不同的线程上运行),但我被告知局部变量不应该同步。有什么提示吗?谢谢!

我不知道Android开发工具,但警告听起来过于热心。 编译器试图帮助您避免在仅对单个线程可见的实例上进行同步。 知道局部变量 r 只能由一个线程看到是足够聪明的,但显然它不够聪明,无法知道原始线程中的r和新线程中的this都引用同一个实例。

我会尝试通过r实例变量来解决此问题。

最新更新