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

📄 calcworkertwo.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
import java.io.*;
import java.net.*;

public class CalcWorkerTwo extends Object {
	private DataInputStream dataIn;
	private DataOutputStream dataOut;
	
	private Thread internalThread;
	private volatile boolean noStopRequested;

	public CalcWorkerTwo(Socket sock) throws IOException {
		dataIn = new DataInputStream(
			new BufferedThreadedInputStream(
				sock.getInputStream()));
		dataOut = new DataOutputStream(
			new BufferedOutputStream(
				sock.getOutputStream()));

		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 ) {
			try {
				System.out.println("in CalcWorker - about to " +
						"block waiting to read a double");
				double val = dataIn.readDouble();
				System.out.println(
						"in CalcWorker - read a double!");
				dataOut.writeDouble(Math.sqrt(val));
				dataOut.flush();
			} catch ( InterruptedIOException iiox ) {
				System.out.println("in CalcWorker - blocked " +
						"read was interrupted!!!");
			} catch ( IOException x ) {
				if ( noStopRequested ) {
					x.printStackTrace();
					stopRequest();
				}
			}
		}

		// In real-world code, be sure to close other streams 
		// and the socket as part of the clean-up. Omitted here 
		// for brevity.

		System.out.println("in CalcWorker - leaving runWork()");
	}

	public void stopRequest() {
		System.out.println(
				"in CalcWorker - entering stopRequest()");
		noStopRequested = false;
		internalThread.interrupt();
		System.out.println(
				"in CalcWorker - leaving stopRequest()");
	}

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

⌨️ 快捷键说明

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