jeudi 25 décembre 2014

Java Thread Random

Ok, so I want to generate a random number for printing in a thread. One and two work fine, but three just prints out the same random number. So if a generates 1928, it sleeps for 1928 over and over again. How do I make dynamically random numbers? Three is the only one where I want to interrupt if another random number(num) is smaller than 1000.



package thread;

import java.util.Random;

public class Threads {

public static Thread one;
public static Thread two;
public static Thread three;

public static int numbers[] = { 1, 2, 3, 4, 5 };
public static String letters[] = { "a", "b", "c", "d", "e" };
public static float negatives[] = { -1, -2, -3, -4, -5 };
public static Random rand = new Random();

public static void main(String args[]) {
startSequences();
one.setName("one");
two.setName("two");
three.setName("three");
one.start();
two.start();
three.start();
}

public static void startSequences() {
one = new Thread() {
public void run() {
try {
System.out
.println("Numbers\n-----------------------------------");
for (int i = 0; i < numbers.length; i++) {
int a = rand.nextInt(3999);
System.out.printf(
"%s is sleeping for %d milliseconds. \n",
Thread.currentThread().getName(), a);
Thread.sleep(a);
System.out.println(Thread.currentThread().getName()
+ " is done sleeping.");
System.out.printf("current number is %s\n", numbers[i]);
}

} catch (InterruptedException e) {
System.out.printf("%s has been interrupted. How rude!",
Thread.currentThread().getName());
} finally {
System.out.printf("%s is finally done!\n", Thread
.currentThread().getName());
}
}
};

two = new Thread() {
public void run() {
try {
one.join();
System.out
.println("\nLetters\n-----------------------------------");
for (int i = 0; i < letters.length; i++) {
int a = rand.nextInt(3999);
System.out.printf(
"%s is sleeping for %d milliseconds.\n", Thread
.currentThread().getName(), a);
Thread.sleep(a);
System.out.println(Thread.currentThread().getName()
+ " is done sleeping.");
System.out.printf("current letter is %s\n", letters[i]);
}
} catch (InterruptedException e) {
System.out.printf("%s has been interrupted. How rude!",
Thread.currentThread().getName());
} finally {
System.out.printf("%s is now done. Finally!", Thread
.currentThread().getName());
}
}
};

three = new Thread() {
public void run() {
try {
int num = rand.nextInt(3999);
two.join();
if (num < 1000) {
System.out
.printf("\n%s is being interrupted because the random was %d and lower than 1000.",
Thread.currentThread().getName(), num);
Thread.sleep(2000);
Thread.currentThread().interrupt();
} else {
int a = rand.nextInt(3999);
System.out
.println("\nNegatives-----------------------------------\n");
System.out
.printf("the number was %s, Therefore, it will not be interrupted.",
num);
for (int i = 0; i < negatives.length; i++) {
System.out.printf(
"\n%s is sleeping for %d milliseconds.",
Thread.currentThread().getName(), a);
Thread.sleep(a);
System.out.printf("\n%s has finished sleeping.",
Thread.currentThread().getName());
System.out.printf(
"\ncurrent negative number is %s",
negatives[i]);
}
}
} catch (InterruptedException e) {
System.out.printf("\n%s has been interrupted. How rude!",
Thread.currentThread().getName());
} finally {
System.out.printf("\n%s is now done. Finally!", Thread
.currentThread().getName());
}
}
};
}

}




Aucun commentaire:

Enregistrer un commentaire