diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java index 1245f6982..33432b14e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java @@ -41,10 +41,6 @@ public class TThread extends TObject implements TRunnable { private boolean alive = true; TRunnable target; - static { - mainThread.setDaemon(true); - } - public TThread() { this(null, null); } @@ -65,7 +61,11 @@ public class TThread extends TObject implements TRunnable { public void start() { if (PlatformDetector.isLowLevel()) { - EventQueue.offer(() -> Fiber.start(this::runThread)); + boolean daemon = this.daemon; + if (!daemon) { + Fiber.userThreadCount++; + } + EventQueue.offer(() -> Fiber.start(this::runThread, daemon)); } else { Platform.startThread(this::runThread); } @@ -207,8 +207,12 @@ public class TThread extends TObject implements TRunnable { TThread current = currentThread(); SleepHandler handler = new SleepHandler(current, callback); if (PlatformDetector.isLowLevel()) { - handler.scheduleId = EventQueue.offer(handler, System.currentTimeMillis() + millis); - current.interruptHandler = handler; + if (current.interruptedFlag) { + handler.interrupted(); + } else { + handler.scheduleId = EventQueue.offer(handler, System.currentTimeMillis() + millis); + current.interruptHandler = handler; + } } else { int intMillis = millis < Integer.MAX_VALUE ? (int) millis : Integer.MAX_VALUE; handler.scheduleId = Platform.schedule(handler, intMillis); diff --git a/core/src/main/java/org/teavm/runtime/Fiber.java b/core/src/main/java/org/teavm/runtime/Fiber.java index 8cfcea40f..2ed1f3dc9 100644 --- a/core/src/main/java/org/teavm/runtime/Fiber.java +++ b/core/src/main/java/org/teavm/runtime/Fiber.java @@ -25,7 +25,7 @@ public class Fiber { public static final int STATE_RUNNING = 0; public static final int STATE_SUSPENDING = 1; public static final int STATE_RESUMING = 2; - private static int daemonCount = 1; + public static int userThreadCount = 1; private int[] intValues; private int intTop; @@ -41,11 +41,13 @@ public class Fiber { private FiberRunner runner; private Object result; private Throwable exception; + private boolean daemon; private static Fiber current; - private Fiber(FiberRunner runner) { + private Fiber(FiberRunner runner, boolean daemon) { this.runner = runner; + this.daemon = daemon; } public void push(int value) { @@ -207,12 +209,12 @@ public class Fiber { static native void setCurrentThread(Thread thread); - public static void start(FiberRunner runner) { - new Fiber(runner).start(); + public static void start(FiberRunner runner, boolean daemon) { + new Fiber(runner, daemon).start(); } static void startMain() { - start(Fiber::runMain); + start(Fiber::runMain, false); } static native void runMain(); @@ -222,7 +224,7 @@ public class Fiber { current = this; runner.run(); current = former; - if (!isSuspending() && Thread.currentThread().isDaemon() && --daemonCount == 0) { + if (!isSuspending() && !daemon && --userThreadCount == 0) { EventQueue.stop(); } }