愚见
Thread类实现了Runnable接口 所有新建方式都与Thread对象有关 所有的线程启动方式都是Thread.start()方法 Thread.run()为线程执行 的方法,Thread.start()为线程启动 的方法;线程启动后会自动调用run();
public class Thread implements Runnable {
public Thread ( ) {
init ( null, null, "Thread-" + nextThreadNum ( ) , 0 ) ;
}
public Thread ( Runnable target) {
init ( null, target, "Thread-" + nextThreadNum ( ) , 0 ) ;
}
}
1、继承Thread
public class MyThread extends Thread {
@Override
public void run ( ) {
System. out. println ( "执行中。。。" ) ;
}
@Test
public void test ( ) {
MyThread myThread = new MyThread ( ) ;
myThread. start ( ) ;
}
}
2、实现Runnable接口
public class MyThread implements Runnable {
@Override
public void run ( ) {
System. out. println ( "执行中。。。" ) ;
}
@Test
public void test ( ) {
MyThread myThread = new MyThread ( ) ;
Thread thread = new Thread ( myThread) ;
thread. start ( ) ;
}
}
3、匿名类实现Runnable
public class MyThread {
@Test
public void test ( ) {
Thread thread = new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
System. out. println ( "执行中。。。" ) ;
}
} ) ;
thread. start ( ) ;
}
}
4、lambda表达式
public class MyThread {
@Test
public void test ( ) {
Thread thread = new Thread ( ( ) - > {
System. out. println ( "执行中。。。" ) ;
} ) ;
thread. start ( ) ;
}
}