一、创建线程
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....