JAVA CORE - Trang 177

!

%

(

$

+

%

%

)

%

count++; //dem viec click

Thread.currentThread().yield();

click = false;


}

public void mouseReleased(MouseEvent e) {

}

} //kết thúc Applet

class clickArea extends java.awt.Canvas implements Runnable{

mouseApplet myapp;

clickArea(mouseApplet mapp){

this.myapp = mapp;

setSize(40,40);

new Thread(this).start();

}

public void paint(Graphics g){

g.drawString(new Integer(myapp.count).toString(),15,20);


}

public void run(){

while(true){

synchronized (myapp) {

while(!myapp.click){

try{

myapp.wait();

}catch(InterruptedException ie){

}

}

}

repaint(250);

}


}//end run

}

Không cần các phương thức wait() và notify(), luồng bức vẽ (canvas) không thể biết

khi nào cập nhập hiển thị. Kết quả xuất ra ngoài của chương trình được đưa ra như sau: