package com.bytedance.vmsdk.jsbridge.utils;

import X.0HH;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;

public class SystemThread {
    public Thread mAndroidThread;
    public long mNativeThread;

    static {
        Covode.recordClassIndex(40241);
    }

    public static native void nativeRun(long j);

    public void join() {
        MethodCollector.i(12646);
        try {
            this.mAndroidThread.join();
            MethodCollector.o(12646);
        } catch (InterruptedException e) {
            0HH.LIZ(e);
            MethodCollector.o(12646);
        }
    }

    public void stop() {
        Looper myLooper = Looper.myLooper();
        if (Thread.currentThread() == this.mAndroidThread && myLooper != null) {
            myLooper.quit();
        }
    }

    public static void attachMainThread(final long j) {
        MethodCollector.i(12406);
        if (Looper.getMainLooper() == Looper.myLooper()) {
            nativeRun(j);
            MethodCollector.o(12406);
            return;
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            static {
                Covode.recordClassIndex(40243);
            }

            public final void run() {
                MethodCollector.i(13089);
                SystemThread.nativeRun(j);
                MethodCollector.o(13089);
            }
        });
        MethodCollector.o(12406);
    }

    private void createAndroidThread(String str, int i) {
        MethodCollector.i(12188);
        Thread thread = new Thread(new Runnable() {
            static {
                Covode.recordClassIndex(40242);
            }

            public final void run() {
                MethodCollector.i(9282);
                Looper.prepare();
                SystemThread.nativeRun(SystemThread.this.mNativeThread);
                Looper.loop();
                MethodCollector.o(9282);
            }
        }, str);
        this.mAndroidThread = thread;
        thread.setPriority(i);
        this.mAndroidThread.start();
        MethodCollector.o(12188);
    }

    public static SystemThread create(long j, String str, int i) {
        return new SystemThread(j, str, i);
    }

    public SystemThread(long j, String str, int i) {
        this.mNativeThread = j;
        createAndroidThread(str, i);
    }
}
