Skip to content

Instantly share code, notes, and snippets.

@draganczukp
Created May 17, 2019 10:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draganczukp/dc2b87634615f671c15613d351c09155 to your computer and use it in GitHub Desktop.
Save draganczukp/dc2b87634615f671c15613d351c09155 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.concurrent.Semaphore;
public class Main {
static class W1 extends Thread{
public static int a;
private Semaphore s;
private Semaphore s0;
public W1(Semaphore s0, Semaphore s){
this.s0 = s0;
this.s = s;
}
@Override
public void run() {
Scanner in = new Scanner(System.in);
System.out.print("Podaj A: ");
a = in.nextInt();
this.s.release();
this.s0.release();
}
}
static class W2 extends Thread{
public static int b;
private Semaphore s;
private Semaphore s0;
public W2(Semaphore s0, Semaphore s){
this.s0 = s0;
this.s = s;
}
@Override
public void run() {
try {
this.s0.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner in = new Scanner(System.in);
System.out.print("Podaj B: ");
b = in.nextInt();
this.s.release();
}
}
static class W3 extends Thread{
Semaphore s1, s2;
public W3(Semaphore s1, Semaphore s2){
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run() {
try {
s1.acquire();
s2.acquire();
System.out.println("Suma to: " + (W1.a + W2.b));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Semaphore s0 = new Semaphore(0);
Semaphore s1 = new Semaphore(0);
Semaphore s2 = new Semaphore(0);
new W1(s0,s1).start();
new W2(s0,s2).start();
new W3(s1, s2).start();
}
}
@Kamol3k
Copy link

Kamol3k commented Jun 6, 2019

dzięki działa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment