s-day05--.匿名内部类实现多线程--方式一(掌握)
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.inmind.noname_inner_thread03;
|
||||
|
||||
import com.inmind.thread01.MyThread;
|
||||
|
||||
/*
|
||||
6.匿名内部类实现多线程(重点掌握)
|
||||
匿名内部类的语法:
|
||||
new 父类名|接口名(){
|
||||
要重写的方法
|
||||
}
|
||||
匿名内部类的作用:只要使用一次的类,不要单独定义。将父类或者接口的子类(实现类)的定义和对象的创建用一段代码操作完毕
|
||||
|
||||
多线程的启动方法:
|
||||
1.继承Thread
|
||||
2.实现Runnable
|
||||
|
||||
所有的匿名内部类的写法都是多态
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
//普通的线程创建方式一
|
||||
MyThread myThread = new MyThread();
|
||||
myThread.start();
|
||||
//-------------------------------------------
|
||||
//匿名内部类启动线程方式一
|
||||
Thread thread1 = new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
Thread subThread = Thread.currentThread();
|
||||
System.out.println("匿名内部类方式:"+subThread.getName()+"子线程执行了");
|
||||
}
|
||||
};
|
||||
thread1.start();
|
||||
//---------------------------------------------------
|
||||
//匿名对象启动线程方式一
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
Thread subThread = Thread.currentThread();
|
||||
System.out.println("匿名内部类方式:"+subThread.getName()+"子线程执行了");
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user