Advertisement
Guest User

Untitled

a guest
May 5th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.22 KB | None | 0 0
  1. package Ćwiczenie4;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static boolean endWork = false;
  7.     public static Thread T1;
  8.     private static Object threadArray[];
  9.     private static String password;
  10.     private static Integer threadsNumber;
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.         System.out.println("Podaj hasło: ");
  15.         password = new StringBuilder().append(scanner.nextLine()).toString();
  16.         System.out.println("Podaj ilość konsumentów: ");
  17.         String M = scanner.nextLine();
  18.         scanner.nextLine();
  19.         threadsNumber = Integer.parseInt(M);
  20.         T1 = new Thread(new Producent());
  21.         T1.start();
  22.  
  23.         threadArray = new Object[threadsNumber];
  24.  
  25.         for (int i = 0; i < threadsNumber; i++) {
  26.             Konsument k = new Konsument();
  27.             Thread t = new Thread(k);
  28.             threadArray[i] = t;
  29.             t.start();
  30.             k.threadID = (int) t.getId();
  31.         }
  32.  
  33.         scanner.close();
  34.     }
  35.  
  36.     public static void stopThreads() {
  37.         endWork = true;
  38.     }
  39.  
  40.     public static synchronized String getPassword() {
  41.         return Main.password;
  42.     }
  43.  
  44.     public static synchronized void closeAllThreads() {
  45.         for (int i = 0; i < threadsNumber; i++) {
  46.             ((Thread) threadArray[i]).interrupt();
  47.         }
  48.  
  49.         System.exit(0);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement