2014-12-25 83 views
2

好吧,所以我想生成一個隨機數在線程中打印。一個和兩個工作正常,但三個只是打印出相同的隨機數。所以如果一個產生1928年,它一次又一次地睡在1928年。我如何製作動態隨機數字?三是我想打斷如果另一個隨機數(NUM)小於1000Java線程隨機

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()); 
      } 
     } 
    }; 
} 

} 

回答

2

如果我明白你的問題,然後爲Thread three需要隨機數生成進入循環的唯一一個。類似的,

// int a = rand.nextInt(3999); 
System.out.println("\nNegatives-----------------------------------"); 
System.out.printf("the number was %s and will not be interrupted.%n", num); 
for (int i = 0; i < negatives.length; i++) { 
    int a = rand.nextInt(3999); 
+1

非常感謝您的幫助! –

-1

沒有測試,我只是檢查了Random的javadoc。我發現:

java.util.Random的實例是線程安全的。但是,跨線程併發使用相同的java.util.Random實例可能會遇到爭用並導致性能下降。考慮在多線程設計中使用ThreadLocalRandom。

所以你可能會檢查ThreadLocalRandom,如果它解決了你的問題。