1 /* 2 * 多线程的创建,方式一:继承于Thread类 3 * 1.创建一个继承于Thread类的子类 4 * 2.重写Thread类的run() -->将此线程执行的操作声明在run()中 5 * 3.创建Thread类的子类的对象 6 * 4.通过此对象调用start() 7 * 8 * 例子:遍历100以内的所有的偶数 9 * */ 10 //1.创建一个继承于Thread类的子类 11 class MyThread extends Thread{ 12 //2.重写Thread类的run() 13 @Override 14 public void run() { 15 for(int i = 0; i < 100; i++){ 16 if(i % 2 == 0){ 17 System.out.println(i); 18 } 19 } 20 } 21 } 22 public class ThreadTest { 23 public static void main(String[] args) { 24 //3.创建Thread类的子类的对象 25 MyThread t1 = new MyThread(); 26 27 //4.通过此对象调用start():①启动当前线程 ②调用当前线程的run() 28 t1.start(); 29 System.out.println("hello"); 30 } 31 32 }
输出hello的位置将随机出现与0到100的偶数之间。
1 /* 2 * 创建多线程的方式二:实现Runnable接口 3 * 1.创建一个实现了Runnable接口的类 4 * 2.实现类去实现Runnable中的抽象方法:run() 5 * 3.创建实现类的对象 6 * 4.将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象 7 * 5.通过Thread类的对象调用start() 8 * */ 9 //1.创建一个实现Runnable接口的类 10 class MThread implements Runnable{ 11 //2.实现类去实现Runnable中的抽象方法 12 @Override 13 public void run() { 14 for (int i = 0; i < 100; i++) { 15 if (i % 2 == 0) { 16 System.out.println(i); 17 } 18 } 19 } 20 } 21 22 public class ThreadTest_2 { 23 public static void main(String[] args) { 24 //3.创建实现类的对象 25 MThread mThread = new MThread(); 26 //4.将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象 27 Thread t1 = new Thread(mThread); 28 //5.通过Thread类的对象调用start():①启动当前线程 ②调用当前线程的run()--》调用了Runnale类型的target的run()
29 //此线程为t1,而不是mThread,谁start()谁是线程 30 t1.start(); 31 } 32 }
当调用start()时有
1 public void run() { 2 if (target != null) { 3 target.run(); 4 } 5 }
1 public Thread(Runnable target) { 2 init(null, target, "Thread-" + nextThreadNum(), 0); 3 }
即重写接口方法的构造函数时重新给target进行了赋值,因此之后调用Thread的run()时由于target已被赋值,所以实际调用的是target的run()。
开发中优先选择实现Runnable接口的方式
原因:1.实现的方式没有类的单继承性的局限性
2.实现的方式更适合来处理多个线程共享数据的情况
联系:public class Thread implements Runnable
相同点:两种方式都需要重写run(),将线程要执行的逻辑声明在run()中
原文:https://www.cnblogs.com/yukinon/p/14965346.html