资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
第9章 多线程与Applet/例程9-1:Pi.java/*演示采用多线程技术计算圆周率*/public class Pi public static void main(String args) PiCaculatorpc = new PiCaculator(); Thread t = new Thread(pc); t.start(); try Thread.sleep (10000);/休眠,等待可能出现的异常情况 t.interrupt(); catch(InterruptedExceptione) e.printStackTrace(); class PiCaculator implements Runnable privatedouble latestPiEstimate; public void run() try System.out.print (Math.PI = +Math.PI+ t ); calPi(0.00001); System.out.println (the latestPI = +this.latestPiEstimate ); catch(InterruptedExceptione) System.out.println(Thecaculator is Interrupted.); /*用于计算圆周率的方法,accuracy为计算精度*/ privatevoid calPi(double accuracy) throws InterruptedException this.latestPiEstimate =0.0; long iteration= 0; intsign = -1; /按给定精度计算圆周率 while(Math.abs (Math.PI-this.latestPiEstimate)accuracy) if(Thread.interrupted () throw new InterruptedException(); iteration+; sign = -sign; this.latestPiEstimate += (sign*4.0/(2*iteration-1); /例程9-2:SynDemo.java/*演示没有进行线程同步所带来的问题*/public class SynDemo public static void main(String args) Demostrator shareDemostrator = new Demostrator(); Thread t1 = new Thread(shareDemostrator,t1); Thread t2 = new Thread(shareDemostrator,t2); t1.start(); t2.start(); class Demostrator implements Runnable private int shareData = 0; public void run() Thread t = Thread.currentThread (); for(int i = 1; i = 5; i+) int copy = shareData; try Thread.sleep (int)(Math.random ()*1000); catch(Exception e) e.printStackTrace(); System.out.println (Thread +t.getName ()+: copy=+copy+tshareData=+shareData); shareData+; /例程9-3:DeadLockDemo.javapublic class DeadLockDemo public static void main(String args) DemoObject a = new DemoObject(); DemoObject b = new DemoObject(); a.another = b; b.another = a; Thread t1 = new Thread(a,t1); Thread t2 = new Thread(b,t2); t1.start (); t2.start (); class DemoObject implements Runnable public DemoObject another = null; public void run() this.method (); public synchronized void method() if(this.another != null) try Thread.sleep (1000); catch(Exception e) e.printStackTrace(); another.method (); /下面的代码段实际上是执行不到的 System.out.println (If you can see this line,no deadlock happened); /例程9-4: ThreeThreadDemo.java/*ThreeThreadDemo.java*/public class ThreeThreadDemopublic static void main(String args)/创建新线程CustomThread ct1 = new CustomThread(0);CustomThread ct2 = new CustomThread(1);/启动新线程ct1.start ();ct2.start ();/输出main线程信息for(int i = 0; i 5; i+)System.out.println(main thread: +i);System.out.println (main thread has done!);class CustomThread extends Threadint id;public CustomThread(int customThreadID)this.id = customThreadID;/重定义子线程的run()方法public void run()/输出自定义线程的信息for(int i = 0; i 5; i+)System.out.println (CustomThread #+ this.id +: +i);System.out.println (CustomThread #+this.id+ has done!);/例程9-5:DigitalClock.java/*采用多线程技术演示一个简单的数字时钟*/import java.awt.event.*;import java.awt.*;import javax.swing.*;public class DigitalClock extends JFrame public static void main(String args) JFrame frame = new DigitalClock(); frame.show(); public DigitalClock() this.setSize(200,150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /数字时钟面板 final ClockPane cp = new ClockPane(); /设置按钮状态并注册事件监听者 final JButton start = new JButton(start); final JButton stop = new JButton(stop); stop.setEnabled(false); start.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) cp.startClock(); start.setEnabled(false); stop.setEnabled(true); ); stop.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) cp.stopClock (); start.setEnabled(true); stop.setEnabled(false); ); /设置面板布局 JPanel buttomPane = new JPanel(); buttomPane.add(start); buttomPane.add(stop); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(); contentPane.add(cp,Borde
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号