Skip to content

Instantly share code, notes, and snippets.

@harunyasar
Last active May 12, 2017 18:17
Show Gist options
  • Save harunyasar/2ff967c63efe0bbc5b7214aa9c46ac2d to your computer and use it in GitHub Desktop.
Save harunyasar/2ff967c63efe0bbc5b7214aa9c46ac2d to your computer and use it in GitHub Desktop.
LAB4
public class Garson implements Runnable {
public Kantin k;
public int id, bekleme;
Garson(int id, int bekleme, Kantin k) {
this.id = id;
this.k = k;
this.bekleme = bekleme;
}
@Override
public void run() {
try {
for (int i = 0; i < this.k.getKapasite(); i++) {
if (this.k.getYemekKuyrukUz() > 0) {
int musteri = this.k.yemekkuyruk.pop();
System.out.println("Kisi "+musteri+" yemek sirasindan cikti. (Garson "+this.id+" tarafindan cikarildi)");
Thread.sleep(this.bekleme);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.LinkedList;
public class Kantin {
public LinkedList<Integer> fiskuyruk, yemekkuyruk;
private int kapasite;
Kantin(int kapasite) {
this.kapasite = kapasite;
this.fiskuyruk = new LinkedList<Integer>();
this.yemekkuyruk = new LinkedList<Integer>();
}
int getKapasite() {
return kapasite;
}
int getFisKuyrukUz() {
return this.fiskuyruk.size();
}
int getYemekKuyrukUz() {
return this.yemekkuyruk.size();
}
}
public class Kasiyer implements Runnable {
public Kantin k;
public int id, bekleme;
Kasiyer(int id, int bekleme, Kantin k) {
this.k = k;
this.id = id;
this.bekleme = bekleme;
}
@Override
public void run() {
try {
for (int i = 0; i < this.k.getKapasite(); i++) {
if (this.k.getFisKuyrukUz() > 0) {
int musteri = this.k.fiskuyruk.pop();
this.k.yemekkuyruk.add(musteri);
System.out.println("Kisi "+musteri+" fis sirasindan cikip yemek sirasina girdi.( Kasiyer "+this.id+" tarafindan cikarildi)");
Thread.sleep(this.bekleme);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String args[]) {
Kantin kantin = new Kantin(1000);
Runnable musteri = new MusteriUret(50, kantin);
Runnable kasiyer1 = new Kasiyer(1, 60, kantin);
Runnable garson1 = new Garson(1, 100, kantin);
Runnable garson2 = new Garson(2, 150, kantin);
Thread musteriThread = new Thread(musteri);
Thread kasiyer1Thread = new Thread(kasiyer1);
Thread garson1Thread = new Thread(garson1);
Thread garson2Thread = new Thread(garson2);
musteriThread.start();
kasiyer1Thread.start();
garson1Thread.start();
garson2Thread.start();
}
}
public class MusteriUret implements Runnable {
public Kantin k;
public int bekleme;
MusteriUret(int bekleme, Kantin k) {
this.k = k;
this.bekleme = bekleme;
}
@Override
public void run() {
try {
for (int i=0; i < 200; i++) {
this.k.fiskuyruk.add(i);
System.out.println("Kisi "+i+" fis sirasina girdi.");
Thread.sleep(this.bekleme);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment