考试首页 | 考试用书 | 培训课程 | 模拟考场 | 考试论坛  
全国  |             |          |          |          |          |         
  当前位置:计算机等级 > 二级考试 > Java语言程序设计 > 考试辅导 > 文章内容
  

计算机二级JAVA考点解析:多线程

中华IT学院   【 】  [ 2017年9月5日 ]

  7.2.7动画例子

  下面是一个包含动画线程的applet例子:

  importjava.awt.*;importjava.awt.image.ImageProducer;import

  java.applet.Applet;

  publicclassatest3extendsAppletimplementsRunnable{Imageimages[];

  MediaTrackertracker;intindex=0;Threadanimator;

  intmaxWidth,maxHeight;//Ouroff-screencomponentsfordoublebuffering.

  ImageoffScrImage;GraphicsoffScrGC;

  //Canwepaintyes?booleanloaded=false;

  //Initializetheapplet.Setoursizeandloadtheimagespublicvoidinit()

  [//Setupourimagemonitortracker=newMediaTracker(this);

  //SetthesizeandwidthofourappletmaxWidth=100;maxHeight=100;

  images=newImage[10];//Setupthedouble-bufferandresizeourapplet

  try{offScrImage=createImage(maxWidth,maxHeight);offScrGC=

  offScrImage.getGraphics();offScrGC.setColor(Color.lightGray);

  offScrGC.fillRect(0,0,maxWidth,maxHeight);

  resize(maxWidth,maxHeight);}catch(Exceptione)

  {e.printStackTrace();}

  //loadtheanimationimagesintoanarrayfor(inti=0;i

  imageFile=newString(\"images/Duke/T\"+String.valueOf(i+1)+\".gif\");

  images[i]=getImage(getDocumentBase(),imageFile)://Registerthis

  imagewiththetrackertracker.addImage(images[i],i);}try{//Use

  trackertomakesurealltheimagesareloadedtracker.waitForAll();}

  catch(InterruptedExceptione){}loaded=true;}

  //Paintthecurrentframe.publicvoidpaint(Graphicsg){if(loaded)

  {g.drawImage(offScrImage,0,0,this);}}

  //Start,setupourfirstimagepublicvoidstart(){if(tracker.checkID

  (index)){offScrGC.drawImage(images[index],0,0,this);}animator=new

  Thread(this);animator.start();}

  //Run,dotheanimationworkhere.//Grabanimage,pause,grabthenext...

  publicvoidrun(){//GettheidofthecurrentthreadThreadme=

  Thread.currentThread();

  //Ifouranimatorthreadexist,andisthecurrentthread...while

  ((animatr!=null)&&(animator==me)){if(tracker.checkID(index))

  {//Clearthebackgroundandgetthenextimage

  offScrGC.fillRect(0,0,100,100);

  offScrGCdrawImage(images[index],0,0,this);index++;//Loopbacktothe

  beginningandkeepgoingif(index>=images.length){index=0;}}

  //Delayheresoanimationlooksnormaltry{animator.sleep(200);}catch

  (InterruptedExceptione){}//Drawthenextframerepaint();}}}

  7.3多线程间的通讯

  7.3.1生产者和消费者

  多线程的一个重要特点是它们?reg;间可以互相通讯。你可以设计线程使用公用对象,每个线程都可以独立操作公用对象。典型的线程间通讯建立在生产者和消费者模型上:一个线程产生输出;另一个线程使用输入buffer

  让我们创建一个简单的\"AlphabetSoup\"生产者和相应的消费者.

  7.3.2生产者

  生产者将从thread类里派生:classProducerextendsThread

  {privateSoupsoup;privateStringalphabet=\"

  ABCDEFGHIJKLMNOPQRSTUVWXYZ\";

  publicProducer(Soups){//Keepourowncopyofthesharedobjectsoup

  =s;}

  publicvoidrun(){charc;//Throw10lettersintothesoupfor(int

  i=0;i

  soup.add(c);//printarecordofosraddition

  System.out.println(\"Added\"+c+\"tothesoup.\");//waitabitbeforewe

  addthenextlettertry{sleep((int)(Math.random()*1000));}catch

  (InterruptedExceptione){}}}}

  注意我们创建了Soup类的一个实例。生产者用soup.add()函数来建立字符池。

分享到:
本文纠错】【告诉好友】【打印此文】【返回顶部
将考试网添加到收藏夹 | 每次上网自动访问考试网 | 复制本页地址,传给QQ/MSN上的好友 | 申请链接 | 意见留言 TOP
关于本站  网站声明  广告服务  联系方式  站内导航  考试论坛
Copyright © 2006-2017 中华考试网(Examw.com) All Rights Reserved  营业执照