s-day05--.匿名内部类实现多线程--方式二(重点掌握)

This commit is contained in:
xx
2026-10-19 13:45:13 +08:00
parent 7f57ffb2b4
commit 703ebdfa6e
2 changed files with 26 additions and 5 deletions
@@ -1,5 +1,6 @@
package com.inmind.noname_inner_thread03; package com.inmind.noname_inner_thread03;
import com.inmind.runnable_thread02.RunnableImpl;
import com.inmind.thread01.MyThread; import com.inmind.thread01.MyThread;
/* /*
@@ -40,5 +41,29 @@ public class Demo03 {
System.out.println("匿名内部类方式:"+subThread.getName()+"子线程执行了"); System.out.println("匿名内部类方式:"+subThread.getName()+"子线程执行了");
} }
}.start(); }.start();
//------------------------以下是线程启动方式二---------------------------------------
//接口的实现类,普通方式
/*RunnableImpl runnable = new RunnableImpl();
Thread thread = new Thread(runnable);*/
Thread thread = new Thread(new RunnableImpl());
thread.start();
//接口的匿名内部类方式
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
System.out.println("接口的匿名内部类:"+Thread.currentThread().getName()+"执行了");
}
});
thread2.start();
//接口的匿名内部类的匿名对象的方式启动线程
new Thread(new Runnable(){
@Override
public void run() {
System.out.println("接口的匿名内部类:"+Thread.currentThread().getName()+"执行了");
}
}).start();
} }
} }
@@ -4,10 +4,6 @@ package com.inmind.runnable_thread02;
public class RunnableImpl implements Runnable{ public class RunnableImpl implements Runnable{
@Override @Override
public void run() { public void run() {
for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+"执行了");
System.out.println(Thread.currentThread().getName()+"------"+i);
}
System.out.println("子线程结束了");
} }
} }