JAVA dynamic threading
i'm a begginer in java, and i have this code i made, but that t thread
gets only executed once, how can i make the thread be dynamic?
//main program - creating 2 threads, unfortuantely at this point only 1
it's running
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();
}
//the timethread i'm calling in the program
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();
}
}
//the random function generator
private static int genRandomInteger(int aStart, int aEnd){
int returnValue = aStart + (int)(Math.random()
* ((aEnd - aStart) + 1));
return returnValue;
}
No comments:
Post a Comment