请问applet中怎样处理线程
1、我想实现在Applet中用Runnable建两个线程两个线程的功能都是无限循环地计数在Applet中显示那两个计数进线程当前的值我写了如下代码但是编译时无问题运行时就出错请各位大侠指点一二
源码:
import java.awt.*;
import java.applet.Applet;
/*
<APPLET
CODE=dbuffer.class
WIDTH=200
HEIGHT=200>
</APPLET>
*/
class t1 implements Runnable
{
public boolean sto1=true;
public long cou1;
Thread thread;
t1()
{
thread=new Thread(this,"t1");
thread.start();
}
public void run()
{
while(sto1)
{
cou1++;
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
}
class t2 implements Runnable
{
public boolean sto2=true;
public long cou2;
Thread thread;
t2()
{
thread=new Thread(this,"t2");
thread.start();
}
public void run()
{
while(sto2)
{
cou2++;
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
}
public class dbuffer extends Applet implements Runnable
{
t1 nt1=null;
t2 nt2=null;
Thread nt3=null;
public void init()
{
}
public void start()
{
t1 nt1=new t1();
t2 nt2=new t2();
Thread nt3=new Thread(this);
nt3.start();
}
public void stop()
{ }
public void run()
{
while(true)
{
nt1.sto1=false;
nt2.sto2=false;
repaint();
try{
Thread.sleep(100);
nt1.sto1=true;
nt2.sto2=true;
}
catch(InterruptedException e){}
}
}
public void paint (Graphics g)
{
nt1.sto1=false;
nt2.sto2=false;
g.drawString(String.valueOf(nt1.cou1),10,10);
g.drawString(String.valueOf(nt2.cou2),40,10);
}
}
问题点数:0、回复次数:2Top
1 楼wdydt163(东东)回复于 2003-08-03 15:51:44 得分 0
出什么错了Top
2 楼chongchong2001(虫虫)回复于 2003-08-03 21:14:07 得分 0
呵呵
在那个版块说了
/*
<APPLET
CODE=dbuffer.class
WIDTH=200
HEIGHT=200>
</APPLET>
*/
class t1 implements Runnable
{
public boolean sto1=true;
public long cou1;
Graphics g1 = null;
Thread thread;
t1( Graphics g )
{
thread=new Thread(this,"t1");
thread.start();
g1 = g;
g1.setXORMode( Color.WHITE );
}
public void run()
{
while(sto1)
{
cou1++;
try{
Thread.sleep(100);
if(cou1>1) g1.drawString(Long.toString(cou1-1),10,10);
g1.drawString(Long.toString(cou1),10,10);
}
catch(InterruptedException e){}
}
}
}
class t2 implements Runnable
{
public boolean sto2=true;
public long cou2;
Graphics g2 = null;
Thread thread;
t2( Graphics g )
{
thread=new Thread(this,"t2");
thread.start();
g2 = g;
g2.setXORMode( Color.WHITE );
}
public void run()
{
while(sto2)
{
cou2++;
try{
Thread.sleep(100);
if(cou2>1) g2.drawString(Long.toString(cou2-1),40,10);
g2.drawString( Long.toString(cou2),40,10);
}
catch(InterruptedException e){}
}
}
}
public class dbuffer extends Applet
{
t1 nt1=null;
t2 nt2=null;
Thread nt3=null;
public void start()
{
t1 nt1=new t1( this.getGraphics() );
t2 nt2=new t2( this.getGraphics() );
}
}Top




