Created
September 21, 2023 17:11
-
-
Save muthuishere/1802a7093c662d03d75dc26a132c3d11 to your computer and use it in GitHub Desktop.
Virtual Thread Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class ClassicThread { | |
void handleRequest() { | |
try { | |
// do some work with Request | |
System.out.println("Handling request on Thread " + Thread.currentThread().threadId() + " started"); | |
Thread.sleep(3000); | |
System.out.println("Handling request on Thread " + Thread.currentThread().threadId() + " completed"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("Hello, World!, Classic Thread Demo"); | |
startClassicalThreads(5000); | |
} | |
public static void startClassicalThreads(int n) { | |
try { | |
List<Thread> threads = new ArrayList<>(); | |
for (int i = 0; i < n; i++) { | |
// create a thread | |
Thread thread = new Thread(() -> { | |
new ClassicThread().handleRequest(); | |
}); | |
// start the thread | |
thread.start(); | |
// add the thread to the list | |
threads.add(thread); | |
System.out.println("Thread " + thread.threadId()+ " started"); | |
} | |
// wait for all the threads to finish | |
for (Thread thread : threads) { | |
thread.join(); | |
} | |
System.out.println("All "+n+" threads completed successfully"); | |
} catch (Throwable e) { | |
System.err.println("Error in completing threads"); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class VirtualThread { | |
void handleRequest() { | |
try { | |
// do some work with Request | |
System.out.println("Handling request on Thread " + Thread.currentThread().threadId() + " started"); | |
Thread.sleep(3000); | |
System.out.println("Handling request on Thread " + Thread.currentThread().threadId() + " completed"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("Hello, World!, Virtual Thread Demo"); | |
startVirtualThreads(500000); | |
} | |
public static void startVirtualThreads(int n) { | |
try { | |
List<Thread> threads = new ArrayList<>(); | |
for (int i = 0; i < n; i++) { | |
// create a thread | |
Thread thread = Thread.ofVirtual().unstarted(() -> { | |
new VirtualThread().handleRequest(); | |
}); | |
// start the thread | |
thread.start(); | |
// add the thread to the list | |
threads.add(thread); | |
System.out.println("Thread " + thread.threadId()+ " started"); | |
} | |
// wait for all the threads to finish | |
for (Thread thread : threads) { | |
thread.join(); | |
} | |
System.out.println("All "+n+" threads completed successfully"); | |
} catch (Throwable e) { | |
System.err.println("Error in completing threads"); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment