求Java源程序代码,一整套的就可以,不限制类型
发布网友
发布时间:2022-04-26 23:32
我来回答
共3个回答
热心网友
时间:2022-06-20 04:45
//Threader.java
import java.awt.Graphics;
import java.awt.Color;
public class Threader extends java.awt.Canvas implements Runnable {
int myPosition =0;
String myName;
int numberofSteps=600;
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("Exception on sleep");}
}
System.out.println("Threader:"+myName+" has finished the race");
}
}//end class Threader.
//GreatRace.java
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;
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]);
}
}
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<theRacers.length;x++){
theRacers[x].stop();
}
}
public void run(){
//循环直到结束
while(Thread.activeCount()>numberofThreadsAtStart+2){
try{
thisThread.sleep(100);
} catch (InterruptedException e){
System.out.println("thisThread was interrupted");
}
}
//停止竞赛
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();
}
}//end class GreatRace.
热心网友
时间:2022-06-20 04:45
没搞懂你的需求
热心网友
时间:2022-06-20 04:46
邮箱。。。。