Advertisement
Guest User

ThreadsCounter

a guest
May 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static volatile int counter = 100000;
  4.  
  5.     public static int tc1 = 0;
  6.     public static int tc2 = 0;
  7.     public static int tc3 = 0;
  8.     public static int tc4 = 0;
  9.  
  10.     public static void main(String[] args) throws InterruptedException {
  11.  
  12.         Thread w1 = new FirstThread();
  13.         Thread w2 = new SecondThread();
  14.         Thread w3 = new ThirdThread();
  15.         Thread w4 = new FourthThread();
  16.  
  17.  
  18.         while(counter > 0) {
  19.  
  20.             if(!w1.isAlive()) {
  21.  
  22.                 w1 = new FirstThread();
  23.                 w1.start();
  24.                 w1.join();
  25.             }
  26.  
  27.             if(!w2.isAlive()) {
  28.                 w2 = new SecondThread();
  29.                 w2.start();
  30.                 w2.join();
  31.             }
  32.  
  33.             if(!w3.isAlive()) {
  34.                 w3 = new ThirdThread();
  35.                 w3.start();
  36.                 w3.join();
  37.             }
  38.  
  39.             if(!w4.isAlive()) {
  40.                 w4 = new FourthThread();
  41.                 w4.start();
  42.                 w4.join();
  43.             }
  44.  
  45.  
  46.             System.out.println("Counter: " + counter);
  47.         }
  48.  
  49.         Counters();
  50.         Beep();
  51.  
  52.     }
  53.  
  54.  
  55.  
  56.     static void Counters(){
  57.         System.out.println("Wątek pierwszy wykonał: " + tc1 + " operacji");
  58.         System.out.println("Wątek drugi wykonał: " + tc2 + " operacji");
  59.         System.out.println("Wątek trzeci wykonał: " + tc3 + " operacji");
  60.         System.out.println("Wątek czwarty wykonał: " + tc4 + " operacji");
  61.     }
  62.  
  63.     static void Beep(){
  64.         if (counter == 0) {
  65.  
  66.             Toolkit.getDefaultToolkit().beep();
  67.  
  68.         }
  69.     }
  70.  
  71.  
  72.     private static class FirstThread extends Thread {
  73.         public void run() {
  74.  
  75.             if(counter > 0) counter --;
  76.             tc1++;
  77.  
  78.         }
  79.     }
  80.  
  81.     private static class SecondThread extends Thread {
  82.         public void run(){
  83.  
  84.             if(counter > 0) counter --;
  85.             tc2++;
  86.  
  87.         }
  88.  
  89.     }
  90.  
  91.     private static class ThirdThread extends Thread {
  92.         public void run(){
  93.  
  94.             if(counter > 0) counter --;
  95.             tc3++;
  96.  
  97.         }
  98.     }
  99.  
  100.     private static class FourthThread extends Thread {
  101.         public void run(){
  102.  
  103.             if(counter > 0) counter --;
  104.             tc4++;
  105.  
  106.         }
  107.  
  108.  
  109.     }
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement