mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
fix: Thread.interrupt() not working
This commit is contained in:
parent
e296c0ebd8
commit
174e1d933c
@ -3,6 +3,7 @@ package com.stardust.autojs.core.looper;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@ -87,7 +88,7 @@ public class MainThreadProxy {
|
||||
}
|
||||
|
||||
public static boolean interrupted() {
|
||||
return Thread.interrupted();
|
||||
return ThreadCompat.interrupted();
|
||||
}
|
||||
|
||||
public boolean isInterrupted() {
|
||||
|
||||
@ -318,7 +318,7 @@ public class ScriptRuntime {
|
||||
|
||||
public void onExit() {
|
||||
//清除interrupt状态
|
||||
Thread.interrupted();
|
||||
// Thread.interrupted();
|
||||
//悬浮窗需要第一时间关闭以免出现恶意脚本全屏悬浮窗屏蔽屏幕并且在exit中写死循环的问题
|
||||
ignoresException(floaty::closeAll);
|
||||
try {
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
package com.stardust.lang;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
@ -10,72 +12,58 @@ import java.util.WeakHashMap;
|
||||
|
||||
public class ThreadCompat extends Thread {
|
||||
|
||||
// private static volatile WeakHashMap<Thread, Boolean> interruptStatus = new WeakHashMap<>();
|
||||
// FIXME: 2017/12/29 是否需要用synchronizedMap?这里虽然线程不安全,但竞争很小
|
||||
private static final Set<Thread> interruptedThreads = Collections.newSetFromMap(new WeakHashMap<Thread, Boolean>());
|
||||
|
||||
public ThreadCompat() {
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// interruptStatus.put(this, false);
|
||||
}
|
||||
|
||||
public ThreadCompat(Runnable target) {
|
||||
super(target);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(ThreadGroup group, Runnable target) {
|
||||
super(group, target);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(String name) {
|
||||
super(name);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(ThreadGroup group, String name) {
|
||||
super(group, name);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(Runnable target, String name) {
|
||||
super(target, name);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(ThreadGroup group, Runnable target, String name) {
|
||||
super(group, target, name);
|
||||
init();
|
||||
}
|
||||
|
||||
public ThreadCompat(ThreadGroup group, Runnable target, String name, long stackSize) {
|
||||
super(group, target, name, stackSize);
|
||||
init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
super.run();
|
||||
// interruptStatus.remove(this);
|
||||
} catch (Throwable e) {
|
||||
// interruptStatus.remove(this);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInterrupted() {
|
||||
//Boolean isInterrupted = interruptStatus.get(this);
|
||||
return super.isInterrupted();// || isInterrupted == null || isInterrupted;
|
||||
return super.isInterrupted() || interruptedThreads.contains(this);
|
||||
}
|
||||
|
||||
public static boolean interrupted() {
|
||||
boolean interrupted = Thread.currentThread().isInterrupted();
|
||||
interruptedThreads.remove(Thread.currentThread());
|
||||
Thread.interrupted();
|
||||
return interrupted;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void interrupt() {
|
||||
super.interrupt();
|
||||
//interruptStatus.put(this, true);
|
||||
interruptedThreads.add(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user