1 package com.eamon.thread;
 2 
 3 /**
 4  * 创建线程的两种方式
 5  * 
 6  * @author Eamon
 7  *
 8  */
 9 public class CreateThread {
10 
11     public static void main(String[] args) {
12         // 第一种 继承Thread类
13         Thread thread1 = new Thread(new MyThread1());
14         thread1.start();
15         // 第二种 实现Runable接口
16         Thread thread2 = new Thread(new MyThread2());
17         thread2.start();
18         
19         //Thread 中 run() 方法源码
20         /*@Override
21         public void run() {
22             if (target != null) {
23                 target.run();
24             }
25         }*/
26     }
27 }
28 class MyThread1 extends Thread{
29     @Override
30     public void run() {
31         System.out.println("hello thread1....");
32     }
33 }
34 class MyThread2 implements Runnable{
35     @Override
36     public void run() {
37         System.out.println("hello thread2....");
38     }
39 }

运行结果:

hello thread1....
hello thread2....

 

相关文章:

  • 2021-07-05
  • 2021-10-07
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-10-01
  • 2021-10-30
  • 2022-01-13
猜你喜欢
  • 2021-10-10
  • 2021-12-13
  • 2022-12-23
  • 2021-07-21
  • 2021-11-11
  • 2022-12-23
  • 2022-01-01
相关资源
相似解决方案