按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
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=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