Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static volatile int counter = 100000;
- public static int tc1 = 0;
- public static int tc2 = 0;
- public static int tc3 = 0;
- public static int tc4 = 0;
- public static void main(String[] args) throws InterruptedException {
- Thread w1 = new FirstThread();
- Thread w2 = new SecondThread();
- Thread w3 = new ThirdThread();
- Thread w4 = new FourthThread();
- while(counter > 0) {
- if(!w1.isAlive()) {
- w1 = new FirstThread();
- w1.start();
- w1.join();
- }
- if(!w2.isAlive()) {
- w2 = new SecondThread();
- w2.start();
- w2.join();
- }
- if(!w3.isAlive()) {
- w3 = new ThirdThread();
- w3.start();
- w3.join();
- }
- if(!w4.isAlive()) {
- w4 = new FourthThread();
- w4.start();
- w4.join();
- }
- System.out.println("Counter: " + counter);
- }
- Counters();
- Beep();
- }
- static void Counters(){
- System.out.println("Wątek pierwszy wykonał: " + tc1 + " operacji");
- System.out.println("Wątek drugi wykonał: " + tc2 + " operacji");
- System.out.println("Wątek trzeci wykonał: " + tc3 + " operacji");
- System.out.println("Wątek czwarty wykonał: " + tc4 + " operacji");
- }
- static void Beep(){
- if (counter == 0) {
- Toolkit.getDefaultToolkit().beep();
- }
- }
- private static class FirstThread extends Thread {
- public void run() {
- if(counter > 0) counter --;
- tc1++;
- }
- }
- private static class SecondThread extends Thread {
- public void run(){
- if(counter > 0) counter --;
- tc2++;
- }
- }
- private static class ThirdThread extends Thread {
- public void run(){
- if(counter > 0) counter --;
- tc3++;
- }
- }
- private static class FourthThread extends Thread {
- public void run(){
- if(counter > 0) counter --;
- tc4++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement