- public class TestThread1 {
- public static void main(String[] args) {
- Runner1 r = new Runner1();
- // r.run();//这是方法调用,而不是开启一个线程
- Thread t = new Thread(r);// 调用了Thread(Runnable target)方法。且父类对象变量指向子类对象。
- t.start();
- for (int i = 0; i < 100000; i++) {
- if(i%10000==0){
- System.out.println("main");
- }
- }
- }
- /*
- * run的运行结果:
- * 先显示10个thread,再来上10个main,
- * 表示使用run方法调用的时候相当于一般的过程调用,并没有加入到线程中区
- * start的运行结果:
- * thread和main是穿插进行的,就表示该方法已经加入了线程中去
- */
- }
- class Runner1 extends Thread {
- public void run() {
- for (int i = 0; i < 100000; i++) {
- if(i%10000==0){
- System.out.println("thread");
- }
- }
- }
- }
java 线程的run和start方法的不同
最新推荐文章于 2022-08-22 21:36:18 发布