32 lines
943 B
Java
32 lines
943 B
Java
package jTest;
|
||
|
||
public class ThreadTest4 {
|
||
// 线程中的内容
|
||
// 线程2 Thread-2 正在运行
|
||
// 线程1 Thread-1 正在运行
|
||
public static void main(String[] args) {
|
||
Thread thread = new Thread(() -> {
|
||
System.out.println("线程中的内容");
|
||
});
|
||
|
||
// 使用start()方法来启动线程,它会调用run()方法。不要直接调用run()方法,因为这不会创建新线程。
|
||
new MyThread().start();
|
||
thread.start();
|
||
new Thread(new MyRunnable()).start();
|
||
}
|
||
}
|
||
|
||
class MyThread extends Thread {
|
||
public void run() {
|
||
// 线程要执行的代码
|
||
System.out.println("线程1 " + Thread.currentThread().getName() + " 正在运行");
|
||
}
|
||
}
|
||
|
||
class MyRunnable implements Runnable {
|
||
public void run() {
|
||
// 线程要执行的代码
|
||
System.out.println("线程2 " + Thread.currentThread().getName() + " 正在运行");
|
||
}
|
||
}
|