Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Semaphore;
- class domakin {
- private final Semaphore semafor = new Semaphore(1);
- int pocetenbroj;
- int broj_na_lifcinja;
- domakin(int br) {
- this.pocetenbroj = br;
- }
- public String toString() {
- return "" + broj_na_lifcinja;
- }
- public void staviLivcinja(){
- broj_na_lifcinja = pocetenbroj;
- System.out.println("Resetirav livcinja");
- }
- public void sakamLifce() throws InterruptedException{
- semafor.acquire();
- izvadiLivce();
- }
- public synchronized void izvadiLivce(){
- broj_na_lifcinja--;
- semafor.release();
- }
- }
- class ucesnik {
- String id;
- ucesnik(String id) {
- this.id = id;
- }
- public String toString() {
- return id;
- }
- //Edno resenie
- public void zemiLivce(domakin dom){
- dom.izvadiLivce();
- System.out.println("Ucesnik " + id + " zema livce!");
- }
- }
- public class lab8 {
- public static void main(String[] args) {
- domakin dom = new domakin(3);
- ucesnik u1 = new ucesnik("1");
- ucesnik u2 = new ucesnik("2");
- ucesnik u3 = new ucesnik("3");
- while(true){
- dom.staviLivcinja();
- cekaj();
- u1.zemiLivce(dom);
- cekaj();
- u2.zemiLivce(dom);
- cekaj();
- u3.zemiLivce(dom);
- cekaj();
- }
- }
- public static void cekaj(){
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement