📄 threadtest1.java
字号:
//演示如何终止一个线程
class ThreadTest1
{
public static void main(String[] args)
{
MyThread1 mt = new MyThread1();
mt.start();
int index = 0;
while(true)
{
if (index++ == 50)
{
System.out.println("Before MyThread1 stopThread method is called!");
mt.stopThread();
System.out.println("After MyThread1 stopThread method is called!");
}
if (index == 60)
{
break;
}
System.out.println(Thread.currentThread().getName());
}
System.out.println("main is stopped!");
}
}
class MyThread1 extends Thread
{
boolean bStop = false;
public void run()
{
while (!bStop)
{
try
{
System.out.println("MyThread is waited!");
wait();
}
catch(InterruptedException ex)
{
}
System.out.println(getName());
}
if(bStop)
{
System.out.println("MyThread1 is stopped");
}
}
public void stopThread()
{
System.out.println("MyThread1 stopThread method is called!");
bStop = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -