(冶金行业)Java程序设计讲义冶金工业出版社第9章

上传人:精****库 文档编号:137648940 上传时间:2020-07-10 格式:DOCX 页数:9 大小:14.35KB
返回 下载 相关 举报
(冶金行业)Java程序设计讲义冶金工业出版社第9章_第1页
第1页 / 共9页
(冶金行业)Java程序设计讲义冶金工业出版社第9章_第2页
第2页 / 共9页
(冶金行业)Java程序设计讲义冶金工业出版社第9章_第3页
第3页 / 共9页
(冶金行业)Java程序设计讲义冶金工业出版社第9章_第4页
第4页 / 共9页
(冶金行业)Java程序设计讲义冶金工业出版社第9章_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《(冶金行业)Java程序设计讲义冶金工业出版社第9章》由会员分享,可在线阅读,更多相关《(冶金行业)Java程序设计讲义冶金工业出版社第9章(9页珍藏版)》请在金锄头文库上搜索。

1、第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 impl

2、ements 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为计算精度*/ pr

3、ivatevoid 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 +=

4、(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 Demost

5、rator 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+tshare

6、Data=+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 (); c

7、lass 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 t

8、his 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+)Sy

9、stem.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);Sy

10、stem.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 Digi

11、talClock() 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()

12、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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 企业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号