Implement WeakHashMap.
[SquirrelJME.git] / modules / cldc-compact / src / test / java / lang / TestVMInterrupt.java
blobf2049fe8eeccdaf696f97250bbe2925cf16ca697
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package lang;
12 import net.multiphasicapps.tac.TestRunnable;
14 /**
15 * Tests interrupted, monitors, and threads as well.
17 * @since 2018/11/20
19 public class TestVMInterrupt
20 extends TestRunnable
22 /** The object to lock on. */
23 protected final Object lock =
24 new Object();
26 /** The order of things, is a check for locks. */
27 protected volatile int order;
29 /**
30 * {@inheritDoc}
31 * @since 2018/11/20
33 @Override
34 public void test()
36 Thread self = Thread.currentThread();
38 // Note
39 this.secondary("a-before-lock", order++);
41 // Lock on this object
42 synchronized (this.lock)
44 // Note
45 this.secondary("a-after-lock", order++);
47 // Setup thread to run
48 Thread runner = new Thread(new __Runner__(
49 self), "VMInterruptChild");
50 runner.start();
52 // Note
53 this.secondary("a-thread-created", order++);
54 this.secondary("a-before-wait", self.isInterrupted());
56 // Wait for a notification
57 try
59 this.lock.wait(20000);
61 // Note
62 this.secondary("a-was-not-interrupted", order++);
64 catch (InterruptedException e)
66 // Note
67 this.secondary("a-was-interrupted", order++);
70 // Note
71 this.secondary("a-after-wait", self.isInterrupted());
74 // Note
75 this.secondary("a-done", order++);
78 /**
79 * Performs the second part of the test run.
81 * @since 2018/11/20
83 final class __Runner__
84 implements Runnable
86 /** The thread to signal. */
87 protected final Thread signal;
89 /**
90 * Initializes the runner.
92 * @param __t The thread to signal.
93 * @throws NullPointerException On null arguments.
94 * @since 2018/11/20
96 public __Runner__(Thread __t)
97 throws NullPointerException
99 if (__t == null)
100 throw new NullPointerException("NARG");
102 this.signal = __t;
106 * {@inheritDoc}
107 * @since 2018/11/20
109 @Override
110 public void run()
112 // Lock
113 synchronized (TestVMInterrupt.this.lock)
115 // Note
116 TestVMInterrupt.this.secondary("b-in-lock",
117 TestVMInterrupt.this.order++);
119 // Interrupt A
120 this.signal.interrupt();
122 // Note
123 TestVMInterrupt.this.secondary("b-interrupt-a",
124 TestVMInterrupt.this.order++);
126 // Sleep for a bit
129 // Note
130 TestVMInterrupt.this.secondary("b-dosleep",
131 TestVMInterrupt.this.order++);
133 // Sleep for a bit longer than the main thread
134 Thread.sleep(3000);
136 // Note
137 TestVMInterrupt.this.secondary("b-sleepdone",
138 TestVMInterrupt.this.order++);
140 catch (InterruptedException e)
142 TestVMInterrupt.this.secondary("b-interrupted",
143 TestVMInterrupt.this.order++);
146 // About to unlock
147 TestVMInterrupt.this.secondary("b-about-to-unlock",
148 TestVMInterrupt.this.order++);