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

📄 threadinterrupt.java

📁 JAVA编程思想源代码 值得一下 很难找的
💻 JAVA
字号:
package chapter13;

public class ThreadInterrupt extends Thread {
	volatile boolean stop = false; // 共享变量,用于指示线程中断

	public static void main(String args[]) throws Exception {
		ThreadInterrupt thread = new ThreadInterrupt();
		System.out.println("Starting thread...");
		thread.start();
		Thread.sleep(3000);
		System.out.println("Asking thread to stop...");
		thread.stop = true; // 改变共享变量,指示线程中断
		Thread.sleep(3000); // 给一段时间让线程状态变为停止
		if (thread.isAlive()) { // 测试线程是否处在活动中
			System.out.println("The thread is not stopped!");
		} else {
			System.out.println("The thread is stopped!");
		}
	}

	public void run() {
		while (!stop) {
			System.out.println("Thread is running...");
			long time = System.currentTimeMillis();
			while ((System.currentTimeMillis() - time < 1000) && (!stop)) {
			} // 系统时间,每隔一秒而且没有被指示中断则循环一次
		}
		System.out.println("Thread is exiting under request...");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -