feat: 使用继承方式创建线程2

This commit is contained in:
Bunny 2024-12-26 18:33:12 +08:00
parent 39a323df18
commit 20fb679804
2 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package thead.thread_2;
public class MyRunnable implements Runnable {
/**
* When an object implementing interface {@code Runnable} is used
* to create a thread, starting the thread causes the object's
* {@code run} method to be called in that separately executing
* thread.
* <p>
* The general contract of the method {@code run} is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
System.out.println("运行线程...");
}
}

View File

@ -0,0 +1,27 @@
package thead.thread_2;
public class RunnableApplication {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
// 重写方式
Thread thread2 = new Thread() {
@Override
public void run() {
System.out.println("内部写法");
}
};
thread2.setName("t2");
thread2.start();
// lambda表达式写法
Runnable runnable = () -> {
System.out.println("Runnable简化写法");
};
Thread thread1 = new Thread(runnable);
thread1.start();
}
}