Advertisement
Shailrshah

Executing Threads According to their Priorities

Sep 28th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. import java.util.Scanner;
  2. class myThread implements Runnable {
  3.     int limit, tNumber;
  4.     myThread(int x, int y){
  5.         limit = x; tNumber = y;
  6.     }
  7.     public void run() {
  8.         for(int i = 1 ; i <= limit; i++)
  9.             System.out.println(i+"(Thread " +tNumber+ ")");
  10.     }
  11. }
  12. class Threading {
  13.     static int getInt(String prompt) {
  14.         System.out.print(prompt);
  15.         while(true){
  16.             try {
  17.                 return Integer.parseInt(new Scanner(System.in).next());
  18.             } catch(NumberFormatException ne) {
  19.                 System.out.print("That's not a whole number.\n"+prompt);
  20.             }
  21.         }
  22.     }
  23.     public static void main(String[] args) {
  24.         int n = getInt("Enter the number of threads:  ");
  25.         int[] priority = new int[n];
  26.         int[] limit = new int[n];
  27.         for(int i = 0; i < n; i++) {
  28.             limit[i] = getInt("Enter the limit of thead "+(i+1)+":  ");
  29.             priority[i] = getInt("Enter the priority of thread "+(i+1)+":  ");
  30.             while(priority[i] < 1 || priority[i] > 10)
  31.                 priority[i] = getInt("The priority should be between 1 and 10: ");
  32.         }
  33.         for(int i = 0; i <n; i++){
  34.             Thread t = new Thread(new myThread(limit[i],i+1));
  35.             t.setPriority(priority[i]);
  36.             t.start();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement