📄 threadfromthread.java
字号:
package chapter10;
public class ThreadFromThread
{
public static void main(String args[])
{
new ChildThread();//定义新线程
try{
for(int i=5;i>0;i--)
{
System.out.println("|-主线程: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Thread interrupted.");
}
System.out.println("主线程退出.");
}
}
//由Thread类派生出线程类ChildThread
class ChildThread extends Thread
{
//构造函数
ChildThread()
{
super("ChildThread");
System.out.println("子线程信息: "+this+"\n");
start(); // 调用run()方法启动新线程
}
//重载run()方法
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("|--子线程: "+i);
Thread.sleep(300);
}
}
catch(InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("子线程退出.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -