Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package values;
- import java.util.concurrent.Semaphore;
- /**
- *
- * @author Rau7
- */
- public class SecondValue {
- private static int A = 0;
- private static int B = 0;
- private static int C = 3;
- private static final int SLEEP = 1;
- //declare all necessary semaphores
- // private static final Semaphore NONAME = new Semaphore(1, true);
- private static final Semaphore a1 = new Semaphore(0, true);
- private static final Semaphore a2 = new Semaphore(1, true);
- private static final Semaphore a3 = new Semaphore(0, true);
- private static final Semaphore a4 = new Semaphore(0, true);
- public static void main(String[] args) {
- new P2().start();
- new P1().start();
- new P3().start();
- new P4().start();
- //Tutaj suma ma być równa 46
- /*
- Uruchamiam kolejno:
- wątek P2 - startuje jako pierwszy
- wątek P1, P1 - DWUKROTNIE!
- wątek P4 - drukowanie wyniku
- zwalniam pozostałe aktywne wątki, żeby nie "wisiały"
- */
- }
- private static final class P1 extends Thread {
- @Override
- public void run() {
- try {
- Thread.sleep(SLEEP);
- for (int i = 0; i < 2; i++) {
- System.out.println("Thread P1 is running...");
- a1.acquire();
- A = 10;
- B = B + 5;
- C = C + A;
- Thread.sleep(SLEEP);
- System.out.println("Thread P1 is done...");
- //Pozwalam drugi raz wykonać wątek P1
- a1.release();
- }
- //Wpuszczam wątek drukowania wyniku
- a4.release();
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- }
- }
- private static final class P2 extends Thread {
- @Override
- public void run() {
- try {
- Thread.sleep(SLEEP);
- a2.acquire();
- System.out.println("Thread P2 is running...");
- B = B + C;
- A = A + B;
- Thread.sleep(SLEEP);
- System.out.println("Thread P2 is done...");
- a1.release(); //Zwalniam wątek P1
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- }
- }
- private static final class P3 extends Thread {
- @Override
- public void run() {
- try {
- a3.acquire();
- Thread.sleep(SLEEP);
- System.out.println("Thread P3 is running...");
- C = B + 10;
- A = 2 * A;
- B = B + A;
- Thread.sleep(SLEEP);
- System.out.println("Thread P3 is done...");
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- }
- }
- private static final class P4 extends Thread {
- @Override
- public void run() {
- try {
- a4.acquire();
- System.out.println("Thread 4 is running");
- Thread.sleep(SLEEP);
- System.out.println("Sum result: " + A + " + " + B + " + " + C + " = " + (A + B + C));
- Thread.sleep(SLEEP);
- System.out.println("Thread P4 is done...");
- //Zwalniam "wiszący" wątek P3
- a3.release();
- } catch (InterruptedException ex) {
- System.out.println("Ooops...");
- Thread.currentThread().interrupt();
- throw new RuntimeException(ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement