s-day05--.匿名内部类实现多线程--方式二(重点掌握)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.inmind.noname_inner_thread03;
|
||||
|
||||
import com.inmind.runnable_thread02.RunnableImpl;
|
||||
import com.inmind.thread01.MyThread;
|
||||
|
||||
/*
|
||||
@@ -40,5 +41,29 @@ public class Demo03 {
|
||||
System.out.println("匿名内部类方式:"+subThread.getName()+"子线程执行了");
|
||||
}
|
||||
}.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{
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.println(Thread.currentThread().getName()+"------"+i);
|
||||
}
|
||||
|
||||
System.out.println("子线程结束了");
|
||||
System.out.println(Thread.currentThread().getName()+"执行了");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user