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

📄 piinterrupt.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
public class PiInterrupt extends Object implements Runnable {
	private double latestPiEstimate;

	public void run() {
		try {
			System.out.println("for comparison, Math.PI=" + 
								Math.PI);
			calcPi(0.000000001);
			System.out.println("within accuracy, latest pi=" + 
								latestPiEstimate);
		} catch ( InterruptedException x ) {
			System.out.println("INTERRUPTED!! latest pi=" + 
								latestPiEstimate);
		}
	}

	private void calcPi(double accuracy) 
				throws InterruptedException {

		latestPiEstimate = 0.0;
		long iteration = 0;
		int sign = -1;

		while ( Math.abs(latestPiEstimate - Math.PI) > 
				accuracy ) {

			if ( Thread.interrupted() ) {
				throw new InterruptedException();
			}

			iteration++;
			sign = -sign;
			latestPiEstimate += 
					sign * 4.0 / ( ( 2 * iteration ) - 1 );
		}
	}

	public static void main(String[] args) {
		PiInterrupt pi = new PiInterrupt();
		Thread t = new Thread(pi);
		t.start();

		try {
			Thread.sleep(10000);
			t.interrupt();
		} catch ( InterruptedException x ) {
			// ignore
		}
	}
}

⌨️ 快捷键说明

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