|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Microsoft Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Microsoft Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +import java.io.OutputStream; |
| 15 | +import java.io.PrintWriter; |
| 16 | + |
| 17 | +public class TryVirtualThread implements Runnable { |
| 18 | + /** |
| 19 | + * A <code>Thread</code> object |
| 20 | + */ |
| 21 | + public static Thread fThread; |
| 22 | + |
| 23 | + /** |
| 24 | + * A <code>Thread</code> object representing the main thread |
| 25 | + */ |
| 26 | + public static Thread fMainThread; |
| 27 | + |
| 28 | + public static Thread fVirtualThread; |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * The instance of the <code>MainClass</code> |
| 33 | + */ |
| 34 | + public static TryVirtualThread fObject = new TryVirtualThread(); |
| 35 | + |
| 36 | + /** |
| 37 | + * An integer value |
| 38 | + */ |
| 39 | + public static int fInt = 0; |
| 40 | + |
| 41 | + /** |
| 42 | + * A string initialized to 'hello world' |
| 43 | + */ |
| 44 | + public static String fString = "Hello World"; |
| 45 | + |
| 46 | + /** |
| 47 | + * The name of an event type |
| 48 | + */ |
| 49 | + public static String fEventType = ""; |
| 50 | + |
| 51 | + /** |
| 52 | + * Runs the test program |
| 53 | + * @param args |
| 54 | + */ |
| 55 | + public static void main(java.lang.String[] args) { |
| 56 | + // Ensure at least one carrier thread is created. |
| 57 | + fVirtualThread = Thread.startVirtualThread(() -> { |
| 58 | + System.out.println("Start a test virtual thread."); |
| 59 | + }); |
| 60 | + |
| 61 | + ThreadGroup group = new ThreadGroup("Test ThreadGroup"); |
| 62 | + fThread = new Thread(group, fObject, "Test Thread"); |
| 63 | + fThread.start(); |
| 64 | + |
| 65 | + fMainThread = Thread.currentThread(); |
| 66 | + |
| 67 | + // Prevent this thread from dying |
| 68 | + while (true) { |
| 69 | + try { |
| 70 | + Thread.sleep(1000); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + try { |
| 79 | + while (true) { |
| 80 | + printAndSignal(); |
| 81 | + triggerEvent(); |
| 82 | + try { |
| 83 | + Thread.sleep(1000); |
| 84 | + } catch (InterruptedException e) { |
| 85 | + } |
| 86 | + } |
| 87 | + } finally { |
| 88 | + System.out.println("Running finally block in MainClass.run()"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Prints to System.out and throws an exception to indicate readiness |
| 94 | + */ |
| 95 | + synchronized public void printAndSignal() { |
| 96 | + print(System.out); |
| 97 | + // Signal readiness by throwing an exception |
| 98 | + try { |
| 99 | + throw new NegativeArraySizeException(); |
| 100 | + } catch (NegativeArraySizeException exc) { |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public void print(OutputStream out) { |
| 105 | + String string = fInt++ +". " + fString; |
| 106 | + PrintWriter writer = new PrintWriter(out); |
| 107 | + writer.println(string); |
| 108 | + writer.flush(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Trigger an event for the front-end. |
| 113 | + */ |
| 114 | + private void triggerEvent() { |
| 115 | + /* Ensure we do it only once */ |
| 116 | + String eventType = fEventType; |
| 117 | + fEventType = ""; |
| 118 | + |
| 119 | + /* Trigger event according to the field fEventType */ |
| 120 | + if (eventType.isEmpty()) { |
| 121 | + return; |
| 122 | + } else if (eventType.equals("ThreadStartEvent")) { |
| 123 | + triggerThreadStartEvent(); |
| 124 | + } else if (eventType.equals("ThreadDeathEvent")) { |
| 125 | + triggerThreadDeathEvent(); |
| 126 | + } else { |
| 127 | + System.out.println("Unknown event type: " + eventType); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Trigger a thread end event for the front-end. |
| 133 | + */ |
| 134 | + private void triggerThreadDeathEvent() { |
| 135 | + Thread.startVirtualThread(() -> { |
| 136 | + System.out.println("Test VirtualThread Death Event"); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Trigger a thread start event for the front-end. |
| 142 | + */ |
| 143 | + private void triggerThreadStartEvent() { |
| 144 | + Thread.startVirtualThread(() -> { |
| 145 | + System.out.println("Test VirtualThread Start Event"); |
| 146 | + }); |
| 147 | + } |
| 148 | +} |
0 commit comments