(冶金行业)J程序设计教程冶金工业出版社第章

上传人:管****问 文档编号:127815233 上传时间:2020-04-06 格式:DOC 页数:9 大小:76.07KB
返回 下载 相关 举报
(冶金行业)J程序设计教程冶金工业出版社第章_第1页
第1页 / 共9页
(冶金行业)J程序设计教程冶金工业出版社第章_第2页
第2页 / 共9页
(冶金行业)J程序设计教程冶金工业出版社第章_第3页
第3页 / 共9页
(冶金行业)J程序设计教程冶金工业出版社第章_第4页
第4页 / 共9页
(冶金行业)J程序设计教程冶金工业出版社第章_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《(冶金行业)J程序设计教程冶金工业出版社第章》由会员分享,可在线阅读,更多相关《(冶金行业)J程序设计教程冶金工业出版社第章(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 implements Runnable

2、 privatedouble latestPiEstimate public void run try System out print Math PI Math PI t calPi 0 00001 System out println the latest PI this latestPiEstimate catch InterruptedExceptione System out println Thecaculator is Interrupted 用于计算圆周率的方法 accuracy为计算精度 privatevoid calPi double accuracy throws Int

3、erruptedException 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 cl

4、ass 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

5、 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 java public class DeadLockDemo public static void main String args DemoObject a new DemoObj

6、ect 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 cat

7、ch 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 ThreeThreadDemo public static void main String args 创建新线程 CustomThread ct1 new CustomThread 0 CustomThread ct

8、2 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 Thread int id public CustomThread int customThreadID this id customThreadID 重定义子线程的run 方法 public void run 输出自定义线程的信息 for int

9、 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 Dig

10、italClock 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 ActionLi

11、stener 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 s

12、tart buttomPane add stop JPanel contentPane new JPanel contentPane setLayout new BorderLayout contentPane add cp BorderLayout CENTER contentPane add buttomPane BorderLayout SOUTH this setContentPane contentPane this setResizable false 例程 9 6 ClockPane java 数字时钟面板的实现类 import javax swing JPanel import

13、 java util import java awt import java awt font FontRenderContext import java awt geom import java text public class ClockPane extends JPanel implements Runnable 线程是否中止的标志 boolean running false 用于显示当前时间的字符串 String time Clock Font font new Font SanSerif Font BOLD 40 启动报时器 public void startClock this

14、running true Thread t new Thread this t start 终止报时器 public void stopClock this running false 实现Runnable接口的run 方法 public void run while this running 获取当前时间并转换成字符串 this time DateFormat getTimeInstance format new Date this repaint 让当前线程休眠1秒钟 try Thread sleep 1000 catch InterruptedException e e printSta

15、ckTrace 输出当前时间 public void paintComponent Graphics g super paintComponent g Graphics2D g2 Graphics2D g g2 setFont this font FontRenderContext contex g2 getFontRenderContext Rectangle2D bound this font getStringBounds this time contex int strX int this getWidth bound getWidth 2 int strY int this getH

16、eight bound getHeight 2 40 g2 drawString this time strX strY 例程 9 7 IOPipeDemo java 演示采用管道机制的线程间通信 import java io public class IOPipeDemo public static void main String args try 创建并连接管道 final PipedOutputStream pout new PipedOutputStream final PipedInputStream pin new PipedInputStream pout 创建并启动输出线程 Thread outputThread new Thread new Runnable public void run writeBytes pout outputThread start 创建并启动输入线程 Thread inputThread new Thread new Runnable public void run readBytes pin inputThread start catc

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 经营企划

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