第 9 章 多线程与 Applet//例程 9-1:Pi
java/*演示采用多线程技术计算圆周率*/public class Pi{ public static void main(String[] args){ PiCaculator pc = new PiCaculator(); Thread t = new Thread(pc); t
start(); try{ Thread
sleep (10000); //休眠,等待可能出现的异常情况 t
interrupt(); }catch(InterruptedException e){ e
printStackTrace(); } }}class PiCaculator implements Runnable{ private double latestPiEstimate; public void run(){ try{ System
print ("Math
PI = "+Math
PI+ "\t" ); calPi(0
00001); System
println ("the latestPI = "+this
latestPiEstimate ); }catch(InterruptedException e){ System
println("Thecaculator is Interrupted
"); } } /**用于计算圆周率的方法,accuracy 为计算精度*/ private void calPi(double accuracy) throws InterruptedException { this
latestPiEstimate =0
0; long iteration= 0; intsign = -1; //按给定精度计算圆周率 while(Math
abs (Math