1. public class Main {
    2. public static void main(String[] args) {
    3. Thread t = new Thread();
    4. t.start();
    5. System.out.println("启动了一个新的线程");
    6. }
    7. }
    1. public class ThreadDemo {
    2. public static void main(String[] args) {
    3. MyThread t = new MyThread();
    4. t.start();
    5. }
    6. }
    7. class MyThread extends Thread {
    8. @Override
    9. public void run() {
    10. System.out.println("start new thread!");
    11. }
    12. }
    1. public class ThreadDemo01 {
    2. public static void main(String[] args) {
    3. Thread t = new Thread(new MyRunnable());
    4. t.start();
    5. }
    6. }
    7. class MyRunnable implements Runnable {
    8. @Override
    9. public void run() {
    10. System.out.println("start new thread!");
    11. }
    12. }
    1. public class ThreadDemo02 {
    2. public static void main(String[] args) {
    3. Thread t = new Thread(() -> {
    4. System.out.println("start new thread!");
    5. });
    6. t.start();//启动新线程
    7. }
    8. }
    1. public class ThreadDemo03 {
    2. public static void main(String[] args) {
    3. System.out.println("main start...");
    4. Thread t = new Thread(() -> {
    5. System.out.println("thread run...");
    6. try {
    7. Thread.sleep(10);
    8. } catch (InterruptedException ignored) {
    9. }
    10. System.out.println("thread end.");
    11. });
    12. t.start();
    13. try {
    14. Thread.sleep(20);
    15. } catch (InterruptedException ignored) {
    16. }
    17. System.out.println("main end...");
    18. }
    19. }