Skip to content

Instantly share code, notes, and snippets.

@icyrhyme
Created July 3, 2020 06:27
Show Gist options
  • Save icyrhyme/ad7db8b7c37f506479bb77b3ee850bf1 to your computer and use it in GitHub Desktop.
Save icyrhyme/ad7db8b7c37f506479bb77b3ee850bf1 to your computer and use it in GitHub Desktop.
ThreadTest
package com.icyrhyme.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static java.lang.Thread.sleep;
public class ThreadTest {
public static class MyThread extends Thread {
@Override
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello, Thread!");
}
}
public static void main(String[] args) {
Thread t1 = new MyThread();
Runnable r = new Runnable() {
@Override
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello, Runnable");
}
};
Thread t2 = new Thread(r);
Thread t3 = new Thread(() -> System.out.println("Hello, lambda"));
t1.start();
t2.start();
t3.start();
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
executorService.submit(() -> System.out.println("Hello, executor"));
} finally {
executorService.shutdown();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment