⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadtest1.java

📁 孙鑫JAVA从入门到精通配套练习程序。所有程序都实际运行过
💻 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 + -