我是java的初学者,我有这个代码,但是t线程只执行一次,我怎样才能使线程成为动态的?
//主程序 - 创建 2 个线程,不幸的是此时只有 1 个正在运行
public static void main(String[] args){
timeThread ttm = new timeThread();
ttm.name = "map";
ttm.min = 1000;
ttm.max = 5000;
ttm.start();
timeThread tta = new timeThread();
tta.name = "arena";
tta.min = 6000;
tta.max = 10000;
tta.start();
}
//我在程序中调用的时间线程
static class timeThread{
static String name;
static int min;
static int max;
static int random;
static Thread t = new Thread () {
public void run () {
while (true){
random = genRandomInteger(min,max);
System.out.println("Thread named: "
+ name + " running for: "
+ random + " secconds...");
try {
Thread.sleep(random);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
};
void start(){
t.start();
}
}
//随机函数生成器
private static int genRandomInteger(int aStart, int aEnd){
int returnValue = aStart + (int)(Math.random()
* ((aEnd - aStart) + 1));
return returnValue;
}