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

📄 innerselfrun.java

📁 java多线程编程实例_Source
💻 JAVA
字号:
public class InnerSelfRun extends Object {
	private Thread internalThread;
	private volatile boolean noStopRequested;

	public InnerSelfRun() {
		// other constructor stuff should appear here first ...
		System.out.println("in constructor - initializing...");

		// just before returning, the thread should be created and started.
		noStopRequested = true;

		Runnable r = new Runnable() {
				public void run() {
					try {
						runWork();
					} catch ( Exception x ) {
						// in case ANY exception slips through
						x.printStackTrace(); 
					}
				}
			};

		internalThread = new Thread(r);
		internalThread.start();
	}

	private void runWork() {
		while ( noStopRequested ) {
			System.out.println("in runWork() - still going...");

			try {
				Thread.sleep(700);
			} catch ( InterruptedException x ) {
				// Any caught interrupts should be habitually re-asserted
				// for any blocking statements which follow.
				Thread.currentThread().interrupt(); // re-assert interrupt
			}
		}
	}

	public void stopRequest() {
		noStopRequested = false;
		internalThread.interrupt();
	}

	public boolean isAlive() {
		return internalThread.isAlive();
	}
}

⌨️ 快捷键说明

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