




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
例 7-1 ThreadDemo.javapublic class ThreadDemopublic static void main(String args)Read read;Write write;read=new Read();/ 创建线程。write=new Write();read.start(); / 线程开始运行后,Read类中的run方法将被自动执行。write.start();class Read extends Threadpublic void run()for(int i=1;i=5;i+)System.out.print(R);trysleep(500);catch(InterruptedException e)class Write extends Threadpublic void run()for(int i=1;i=5;i+)System.out.print(W);trysleep(300);catch(InterruptedException e)例 7-2 RunnableDemo.javapublic class RunnableDemopublic static void main(String args)Read read;Write write;read=new Read();/ 创建Runnable对象write=new Write();new Thread(read).start(); / 线程开始运行后,Read类中的run方法被自动执行new Thread(write).start();class Read implements Runnablepublic void run()for(int i=1;i=5;i+)System.out.print(R);tryThread.sleep(500);catch(InterruptedException e)class Write implements Runnablepublic void run()for(int i=1;i=5;i+)System.out.print(W);tryThread.sleep(300);catch(InterruptedException e)例 7-3 StopBoolean.javapublic class StopBoolean extends Threadprotected boolean done=false;public void run()while(!done)System.out.println(StopBoolean running);trysleep(720);catch(InterruptedException ex)/ nothing to doSystem.out.println(StopBoolean finished.);public static void main(String args)StopBoolean st=new StopBoolean();st.start();tryThread.sleep(5000);catch(InterruptedException e)e.printStackTrace();st.done=true;例 7-4 HiLoPri.javaclass clicker implements Runnablelong click=0;Thread t;private volatile boolean running=true;public clicker(int p)t=new Thread(this);t.setPriority(p);public void run()while(running)click+;public void stop()running=false;public void start()t.start();public class HiLoPripublic static void main(String args)Thread.currentThread().setPriority(Thread.MAX_PRIORITY);clicker hi=new clicker(Thread.NORM_PRIORITY+2);clicker lo=new clicker(Thread.NORM_PRIORITY-2);lo.start();hi.start();tryThread.sleep(10000);catch(InterruptedException e)System.out.println(Main thread interrupted.);lo.stop();hi.stop();tryhi.t.join();lo.t.join();catch(InterruptedException e)System.out.println(InterruptedException caught);System.out.println(Low-priority thread: +lo.click);System.out.println(High-priority thread: +hi.click);例 7-5 CountNoSyn.javapublic class CountNoSyn implements Runnableint i=0;public void count()i+;System.out.println(Thread.currentThread().getName()+ +i);tryThread.sleep(1000);i+;catch(Exception e)System.out.println(Thread.currentThread().getName()+ +i);public void run()count();public static void main(String args)CountNoSyn c=new CountNoSyn();new Thread(c).start();new Thread(c).start();例 7-6 Count.javapublic class Count implements Runnableint i=0;synchronized public void count()i+;System.out.println(Thread.currentThread().getName()+ +i);tryThread.sleep(1000);i+;catch(Exception e)System.out.println(Thread.currentThread().getName()+ +i);public void run()count();public static void main(String args)Count c=new Count();new Thread(c).start();new Thread(c).start();例 7-7 Count.javapublic class Count implements Runnableint i=0;public void count()synchronized(this)i+;System.out.println(Thread.currentThread().getName()+ +i);tryThread.sleep(1000);i+;catch(Exception e)System.out.println(Thread.currentThread().getName()+ +i);public void run()count();public static void main(String args)Count c=new Count();new Thread(c).start();new Thread(c).start();例 7-8 Demo.javaclass Sellint fiveLeft=2,tenLeft=0,twentyLeft=0;String s=null;public synchronized void sellTickets(int money)if(money=5) / 如果使用该方法的线程传递的参数是5,就不用等待fiveLeft=fiveLeft+1;s=给您入场卷您的钱正好;System.out.println(s);else if(money=20)while(fiveLeft20)t=0;redPen.clearRect(0,0,38,300);redPen.fillOval(20,(int)(1.0/2*t*t*3.8),16,16);try红色球.sleep(50);catch(InterruptedException e)else if(Thread.currentThread()=兰色球)bluePen.clearRect(38,0,500,300);bluePen.fillOval(38+(int)(16*t),(int)(1.0/2*t*t*3.8),16,16);try兰色球.sleep(50);catch(InterruptedException e)例题 7-2 旋转彩虹import java.applet.*;import java.awt.*;import java.awt.event.*;public class Animation extends Appletint i=0;public void paint(Graphics g)i=(i+2)%360;Color c=new Color(3*i)%255,(7*i)%255,(11*i)%255);g.setColor(c);g.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 耐火材料快速冷却技术企业制定与实施新质生产力项目商业计划书
- 素土、灰土地基施工要点
- 纳米碳酸钙填充剂行业跨境出海项目商业计划书
- 智能粮油仓储温湿度监控系统创新创业项目商业计划书
- 2025年垃圾分类知识竞赛题库和答案大全
- 管廊(管沟)开挖与支护降水施工质量通病、原因分析及应对措施
- 2025年辽宁省抚顺市事业单位工勤技能考试考试题库及参考答案
- 2025年中级消防设施操作员理论考试笔试试题含答案
- 2025年门诊部护理理论试题(附答案)
- 2025年山西省吕梁市公务员省考行政职业能力测验模拟题(附答案)
- 统编版历史《三国两晋南北朝的政权更迭与民族交融》课件
- 音乐小动物回家课件20
- 中国桥梁发展史简介
- 日立冷水机组操作维护课件
- 审计案例第6章筹资与投资循环审计案例
- 神经介入治疗(DSA)及围手术期概述精品PPT课件
- 丙烯酸树脂安全技术说明书
- 50MW光伏项目工程清单报价
- 儿童能力评估量表PEDI拍迪
- 柴油发电机组机房设计手册_图文
- 雨、污水管道工程施工方案
评论
0/150
提交评论