资源描述:
《20132155邢超超13计科3班(java实验八)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、2015/2016学年第一学期《Java程序设计》实验报告八班级13计科3班学号20132155姓名邢超超实验时间9:35-11:10实验地点东实验楼2实验成绩一、实验题目1、利用继承Thread类构建线程。1,22>利用Runnable接口构建线程。33、验证线程的同步机制。4,54、验证线程死锁现象及解决办法。6,7,85、验证直接、间接抛出异常的机制。11,12,13,146、验证异常捕获与处理机制。15,16二、实验目的学习和掌握线程设计方法及异常处理机制,对java语言的安全性有初步的掌握。三、实验过程1、2、四、实验
2、心得五、源程序8.1classCountingThreadextendsThread{publicvoidrun(){System,out・printin();System,out.println(z,子线程开始〃+this+"开始〃);for(inti=0;i<8;i++){System・out・println(this・getName()+〃・i二〃+(i+l)+〃t〃);System.out・printin();System,out.printin(〃子线程"+this+〃结束〃);}}publicclassThread
3、Test{publicstaticvoidmain(String[]args){System,out.printin(z,主线程开始〃);CountingThrcadthrcadl=newCountingThrcad();thread1.start();CountingThreadthread2=newCountingThread();thread2・start();CountingThreadthread3=newCountingThread();thrcad3・start();}}8.2importjava・awt・*;pu
4、blicclassBirdextendsThread{privateintxdir=2*(l-2*(int)Math・e〃加(Math,r臼刀〃刃()));privateintydir二2*(1一2*(int)Math・.randomO、);privatebooleanrunning二false;privateCagecage=null;protectedintx,y;Imagebird二Toolkit•getDefaultToolki认)・getlmage(z,bird・jpg〃);publicBird(Cagecage,in
5、t_x,int_y){cage二_cage;x=x;y二_y;start();}publicvoidstart(){running=true;super・start();}publicvoidhalt(){running二false;}publicvoidrun(){while(running){move();try{s/eep(120);}catch(IntcrruptcdExccptionic){System・out・printin("Threadinterrupted");privatevoidmove(){x+二xdir
6、;y+二ydir;if(x>cagc.gctSizc()・width){x二cage・getSize()・width;xdir*二(-1);Iif(x<0){xdir*二(T);}if(y>cage・getSize()・height){y二cage.getSize()・height;ydir*二(T);}if(y<0){ydir*二(T);publicvoiddraw(Graphicsg){//TODOAuto-generatedmethodstubg.drawlmage(bird,x,y,30,40,cage);}}impor
7、tjava.awt・*;importjava・awt.event・*;importjavax・swing・JFrame;publicclassCageextendsJFrameimplementsActionListener{privateButtonquit=newButton("Quit");privateButtonstart二newButton("Start");privateButtonstop=newButton("Stop");privateBirdbirds[]二newBird[20];Imagebird=Too
8、lkit.getDefaultToolkitl)・getlmage(/zbird.jpg〃);publicCage(){super(z,CagewithBirds");setLayout(newFlowLayout());add(quit);quit・addAc