123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- * 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 model;
-
- import java.util.Observable;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
-
- /**
- *
- * @author nobody
- */
- public class Model extends Observable implements Runnable
- {
- private int zahl;
- private boolean status;
- private Thread thr;
- private boolean notify;
- private boolean schlafen;
-
- public Model()
- {
- zahl = 0;
- status = false;
- thr = null;
- }
-
- public void start()
- {
- if (status == true)
- {
- //1
- //notify = true;
- synchronized (thr)
- {
- thr.notify();
- schlafen = false;
- }
- }
- if (status == false && thr == null)
- {
- zahl = 0;
- thr = new Thread(this);
- thr.start();
- status = true;
- notify = true;
- }
- }
-
- @Override
- public void run()
- {
- while (status == true)
- {
- if (schlafen == true)
- {
- synchronized (thr)
- {
- try
- {
- thr.wait();
- }
- catch (InterruptedException ex)
- {
- JOptionPane.showMessageDialog(null, "Alles Kaputt");
- }
- }
- }
- // Zahl incrementieren
- zahl--;
- // Zahl ändern
- if (notify == true)
- {
- this.setChanged();
- this.notifyObservers();
- }
- // Zahl zurücksetzen
- if (zahl == 0)
- {
- zahl = 10;
- }
- // kurze Wartezeit
- try
- {
- Thread.sleep(1000);
- }
- catch (InterruptedException ex)
- {
- Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
-
- /**
- * @return the zahl
- */
- public int getZahl()
- {
- return zahl;
- }
-
- public void setStartZahl(int zahl)
- {
- this.zahl = zahl;
- }
-
- public void isWarten()
- {
- schlafen = true;
- }
-
- public void isWeiterlaufen(){
- schlafen = false;
- }
- }
|