@author Jaroslav Bachorik
| 33 | * @author Jaroslav Bachorik |
| 34 | */ |
| 35 | abstract public class TestApp implements TestPrinter { |
| 36 | final public void start() throws Exception { |
| 37 | Thread t = new Thread(new Runnable() { |
| 38 | @Override |
| 39 | public void run() { |
| 40 | startWork(); |
| 41 | } |
| 42 | }, "Worker Thread"); |
| 43 | System.out.println("ready:" + getPID()); |
| 44 | System.out.flush(); |
| 45 | t.setDaemon(true); |
| 46 | t.start(); |
| 47 | |
| 48 | do { |
| 49 | String resp = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)).readLine(); |
| 50 | System.out.println("Received " + resp + " - " + "done".contains(resp)); |
| 51 | if ("done".contains(resp)) { |
| 52 | System.out.flush(); |
| 53 | System.out.println(System.currentTimeMillis() + ": Interrupting the worker thread"); |
| 54 | t.interrupt(); |
| 55 | System.out.println(System.currentTimeMillis() + ": Waiting for the worker thread to finish"); |
| 56 | t.join(1000); |
| 57 | if (t.isAlive()) { |
| 58 | Thread.dumpStack(); |
| 59 | throw new RuntimeException("Dangling worker thread"); |
| 60 | } |
| 61 | System.out.println(System.currentTimeMillis() + ": Worker thread finished"); |
| 62 | break; |
| 63 | } |
| 64 | } while (true); |
| 65 | } |
| 66 | |
| 67 | private static long getPID() { |
| 68 | String processName |
| 69 | = java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); |
| 70 | return Long.parseLong(processName.split("@")[0]); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * The work here should be done repeatedly until the thread gets interrupted |
| 75 | */ |
| 76 | abstract protected void startWork(); |
| 77 | } |
nothing calls this directly
no outgoing calls
no test coverage detected