2个有关线程的问题, 1.run()后面加上sleep(1);2.代码贴全了看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最好不要调用destroy(),线程在完成了工作后会被系统回收,当你的线程有无限循环注意在什么时候跳出,还要为避免利己线程! 1. 代码2 肯定是定义的时候写了boolean inApplet = true; 不好意思,看花眼了,inApplet前面有定义.还有一个问题,有关超级线程的,是不是优先级最高的线程自动被设为超级线程。 还有个问题,书上是用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 racerstheThreads[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(); }elseSystem.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");}}} 求!Java读取远程文本文件数据的问题..... 关于排序 鼠标事件的响应,帮忙额。。。。。 URLConnection问题 Java中的数组和C语言中的数组有何异同? 200分,帮忙编一个小游戏 如何得到焦点focus 谁能给个FloodIt游戏的编程思路? 我用SQL Server2000还需要建数据源么? 把tomcat安装成service 谁能告诉我 下载!此代码只能下载excel文件如何修改才能下载所有文件?(在线等待,解决问题立刻给分)
还有一个问题,有关超级线程的,是不是优先级最高的线程自动被设为超级线程。
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");
}
}
}