twothread1.java
来自「JAVA的一些基础教程」· Java 代码 · 共 30 行
JAVA
30 行
public class TwoThread1 extends Thread {
private Thread creatorThread;
public TwoThread1() {
// 获取调用构造函数的线程
creatorThread = Thread.currentThread();
}
public void run() {
for ( int i = 0; i < 5; i++ ) {
showMsg();
}
}
public void showMsg() {
// 获取运行该函数的线程
Thread t = Thread.currentThread();
if ( t == creatorThread ) {
System.out.println("创建线程的线程");
} else if ( t == this ) {
System.out.println("新线程");
} else {
System.out.println("其他线程");
}
}
public static void main(String[] args) {
TwoThread1 tt = new TwoThread1();
tt.start();
for ( int i = 0; i < 5; i++ ) {
tt.showMsg();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?