小小問題―關於java多執行緒

wwlhp發表於2002-09-14
下面是我自學多執行緒後自己寫的第一個程式,可是在內類那裡編譯時出錯,不知為何?

// 在此輸入java程式碼
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

 
public class Applet1 extends Applet  
{
	boolean clicked = false;
	int count;
	
	public void init()
	{
		add( new ClickCanvas( this ) );
		addMouseListener( new MouseAdapter(){
				public void mousePressed( MouseEvent me )
				{
					++count;
					synchronized( MouseAdapter.this ){ 
							clicked = true;
							MouseAdapter.this.notify();	
					}
				}	});
	}
}
 

class ClickCanvas extends Canvas implements Runnable 
{
	Applet1 applet;
	
	public ClickCanvas( Applet1 applet )
	{
		this.applet = applet;
		setBackground( Color.blue );
		setSize( 100, 100 );
		new Thread( this ).start();
	}
	
	public void run()
	{
		while( true ){
			synchronized( applet ){
				while( !applet.clicked ) 
					try{	applet.wait();
					}catch( InterruptedException ie ){}
				repaint( 250 );
				applet.clicked = false;
			}
		}
	}
				 
	public void paint( Graphics g )
	{
		g.drawString( ""+ applet.count, 10, 20 ); 
	}
}
<p class="indent">


希望您能指點錯誤之處,非常感謝!!!

相關文章