阻塞线程的办法&&创建子线程

Dialog progressDialog = new Dialog(MainActivity.this, R.style.toast_dialog);
progressDialog.setContentView(View.inflate(MainActivity.this, R.layout.dialog_loading_base_layout, null), new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
progressDialog.setCancelable(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.getWindow().setDimAmount(0F);
progressDialog.show();


public class TestThread {

private HandlerThread TestThread;
private Handler mHandler;
private static final String NAME = "TestThread";
private volatile static TestThread instance;

public static TestThread getInstance() {
if (instance == null) {
synchronized (TestThread.class) {
if (instance == null) {
instance = new TestThread();
}
}
}
return instance;
}


private TestThread() {
if (Process.THREAD_PRIORITY_URGENT_AUDIO >= Process.THREAD_PRIORITY_URGENT_AUDIO &&
Process.THREAD_PRIORITY_URGENT_AUDIO <= Process.THREAD_PRIORITY_LOWEST) {
this.TestThread = new safetyHandlerThread(NAME, Process.THREAD_PRIORITY_URGENT_AUDIO);
} else {
this.TestThread = new safetyHandlerThread(NAME, Process.THREAD_PRIORITY_URGENT_AUDIO);
}
this.TestThread.start();
this.mHandler = new Handler(TestThread.getLooper());
}

public Handler getHandler() {
return mHandler;
}

public Looper getLooper() {
return mHandler.getLooper();
}

public void post(final Runnable r) {
this.mHandler.post(r);
}

public void postDelay(Runnable r, long timeDelay) {
this.mHandler.postDelayed(r, timeDelay);
}

public static void destroy() {
if (instance == null || instance.TestThread == null) {
return;
}
instance.TestThread.quit();
}


private static class safetyHandlerThread extends HandlerThread {
public safetyHandlerThread(String name, int priority) {
super(name, priority);
}

@Override
public void run() {
try {
super.run();
} catch (Throwable t) {

}
}

private void restoreLoop() {
while (true) {
try {
Looper.loop();
} catch (Throwable t) {
}
}
}
}

}

posted on 2021-07-07 17:21  zhang11111wei  阅读(73)  评论(0编辑  收藏  举报

导航