- 继承自
Thread
重写run
方法public class MyThread extends Thread { @Override public void run() { System.out.println(getName()); String name = getName(); for (int i = 0; i < 20; i++) { System.out.println(name + "---" + i); } } }
- 实现
Runable
接口及run
方法public class RunableImp implements Runnable { @Override public void run() { String name = Thread.currentThread().getName(); for (int i = 0; i < 20; i++) { System.out.println(name + "---" + i); } } }
- 匿名内部类实现
run
方法// Runable匿名 Thread thread1 = new Thread(new Runnable() { @Override public void run() { String name = Thread.currentThread().getName(); for (int i = 0; i < 20; i++) { System.out.println(name + "---" + i); } } }); thread1.start(); // Thread匿名 new Thread() { @Override public void run() { String name = Thread.currentThread().getName(); for (int i = 0; i < 20; i++) { System.out.println(name + "---" + i); } } }.start();
Java多线程的三种开启方式
Leave a reply