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 FirstValue {
- 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 16
- /*
- Z wątku P2 wykonuję tylko B=B+C - ten startuje jako pierwszy
- Z wątku P1 wykonuję tylko A=10
- Suma 10+3+3 = 16
- Z wątku P4 wyswietlam wynik
- 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);
- System.out.println("Thread P1 is running...");
- a1.acquire();//czekam na zezwolenie z wątku P1
- A = 10;
- a4.release();//zwalniam wątek P4 (drukowanie)
- a1.acquire();//i blokuję siebie przed wykonaniem kolejnych instrukcji
- B = B + 5;
- C = C + A;
- Thread.sleep(SLEEP);
- System.out.println("Thread P1 is done...");
- } 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);
- System.out.println("Thread P2 is running...");
- B = B + C;
- a1.release();//zwalniam wątek P1
- a2.acquire();//blokuję siebie
- A = A + B;
- Thread.sleep(SLEEP);
- System.out.println("Thread P2 is done...");
- } 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 {
- Thread.sleep(SLEEP);
- System.out.println("Thread P3 is running...");
- a3.acquire();//czekam na zezwolenie
- C = B + 10;
- A = 2 * A;
- B = B + A;
- System.out.println("Thread P3 is done...");
- //Ten wątek kończy ostatni, nikogo nie musi wpuszczać
- } 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...");
- //Właściwie trzeba by puścić oczekujące wątki
- a1.release();
- 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