Kotlin-Demo/jTest/ThreadTest4.java

32 lines
943 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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() + " 正在运行");
}
}