Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package grandCanyon;
- import java.util.ArrayList;
- public class Skywalk {
- private ArrayList<Besucher> aktuelleBesucher;
- public static int anzahlBesucher;
- public Skywalk(){
- this.aktuelleBesucher = new ArrayList<Besucher>(120);
- }
- public void betreten(Besucher b){
- if(this.aktuelleBesucher.size() <= 120){
- this.aktuelleBesucher.add(b);
- this.anzahlBesucher++;
- b.start();
- }else{
- System.out.println("Der Skywalk ist leider bereits voll! Kommen Sie bitte später wieder.");
- }
- }
- public void verlassen(Besucher b){
- this.aktuelleBesucher.remove(b);
- this.anzahlBesucher--;
- }
- public static void main(String[] args) throws InterruptedException{
- Skywalk skywalk = new Skywalk();
- for(int i = 0; i<=3000; i++){
- Besucher b = new Besucher(i, (long) Math.random() * 100000);
- skywalk.betreten(b);
- skywalk.verlassen(b);
- }
- }
- }
- package grandCanyon;
- public class Besucher extends Thread {
- private int nr;
- private long verweildauer;
- public void run(){
- System.out.println("Thread " + this.nr + "Betrete den Skywalk!");
- try {
- Thread.sleep(this.verweildauer/2);
- System.out.println("Thread "+ this.nr + ": Schaue herum");
- Thread.sleep(this.verweildauer/2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Aktuell befinden sich " + Skywalk.anzahlBesucher + "/120 Besucher auf dem Skywalk!");
- System.out.println("Thread "+ this.nr + ": Verlasse den Skywalk");
- }
- public Besucher(int nr, long verweildauer){
- this.nr = nr;
- this.verweildauer = verweildauer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement