1.run()后面加上sleep(1);
2.代码贴全了看看

解决方案 »

  1.   

    最好不要调用destroy(),线程在完成了工作后会被系统回收,当你的线程有无限循环注意在什么时候跳出,还要为避免利己线程!
      

  2.   

    1. 代码2 肯定是定义的时候写了boolean inApplet = true;
      

  3.   

    不好意思,看花眼了,inApplet前面有定义.
    还有一个问题,有关超级线程的,是不是优先级最高的线程自动被设为超级线程。
      

  4.   

    还有个问题,书上是用Thread.MIN_PRIORITY这个变量来设线程优先权级,我用Thread.MAX_PRIORITY来设的话,程序就不能运行,为什么?原代码:
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Frame;
    import Threader;
    public class GreatRace extends java.applet.Applet implements Runnable{Threader theRacers[];                         
    static int racerCount=3;                           //竞赛者人数
    Thread theThreads[];
    Thread thisThread;
    static boolean inApplet=true;
    int numberofThreadsAtStart;
    public void init(){
    numberofThreadsAtStart=Thread.activeCount();
    setLayout(new GridLayout(racerCount,1));
    theRacers=new Threader[racerCount];
    theThreads=new Thread[racerCount];
    for(int x=0;x<racerCount;x++){
    theRacers[x]=new Threader("Racer#"+x);
    theRacers[x].setSize(getSize().width,getSize().height/racerCount);
    add(theRacers[x]);
    theThreads[x]=new Thread(theRacers[x]);  //new thread for each racers
    theThreads[x].setPriority(Thread.MIN_PRIORITY+x);   //设线程优先权级。
    }
    }
    public void start(){
    for (int x=0;x<racerCount;x++)
    theThreads[x].start();
    thisThread=new Thread(this);
    thisThread.start();
    }public void stop(){
    for(int x=0;x<racerCount;x++){
    theRacers[x].stop();
    }
    }public void run(){
    while(Thread.activeCount()>numberofThreadsAtStart+2){
    try{
    thisThread.sleep(100);
    }
    catch(InterruptedException e){
    System.out.println("this Thread is interrupt");
    }
    }if(inApplet){
    stop();                              
    destroy();                          
    }
    else
    System.exit(0);
    }public static void main(String argv[]){
    inApplet=false;
    if(argv.length>0)
    racerCount=Integer.parseInt(argv[0]);
    Frame theFrame=new Frame("the great thread race");
    GreatRace theRace=new GreatRace();
    theFrame.setSize(400,200);
    theFrame.add("Center",theRace);
    theFrame.show();
    theRace.init();
    theFrame.pack();
    theRace.start();
    }
    }/*Threader类*/
    import java.awt.Graphics;
    import java.awt.Color;
    public class Threader extends java.awt.Canvas implements Runnable{
    int myposition=0;
    String myname;
    int numberofsteps=1600;
    boolean keepRunning=true;
    public Threader(String inname){
    myname=new String(inname);
    }public synchronized void paint(Graphics g){
    g.setColor(Color.black);
    g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2);
    g.setColor(Color.yellow);
    g.fillOval((myposition*getSize().width/numberofsteps),0,15,getSize().height);
    }public void stop(){
    keepRunning=false;                        
    }public void run(){
    while((myposition<numberofsteps)&& keepRunning){
    myposition++;
    repaint();
    try{
    Thread.currentThread().sleep(10);
    }
    catch(Exception e){
    System.out.println("sleep");
    }
    System.out.println("Thrader"+myname+"finished the race");
    }
    }
    }