Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java: Stopuhr mit Threads. Wie anhalten/wieder starten?

Empfohlene Antworten

Veröffentlicht

Hi Ihr Experten, ich programmiere derzeit in der FH mit Threads. Habe ein Grundverständnis m.E. schon dafür entwickelt.

Derzeit habe ich eine Stoppuhr, zu programmieren, die ich anhalten und weiterlaufen lassen soll. Allerdings weiß ich nicht, wie ich das hinbekommen soll. Unsere Professorin meinte, dass dies mit wait/notify funktioniert. Allerdings setzen diese beiden Methoden ja eine synchronized Methode voraus, auf die sie angewandt wird.

M.E. ist aber synchronized nur dafür da, um den Zugriff von zwei Threads auf ein Objekt zu beschränken - sprich die eine Methode darf erst, wenn die andere dises fertig hat, sodass es zu keinen zerstückelten oder inkonsistenten Daten kommt. Daher sehe ich in meinem Fall keine Anwendung dafür.

Meine Anwendung wird derzeit ja über die Variable weiter gesteuert. Wird auf den Stopbutton gedrückt, wird diese auf false gesetzt und entsprechend die run Methode beendet. Meiner Meinung nach müsste dann ja dann noch dem beenden der run Methode der Thread komplett tot sein, was mir auch die thread1.isAlive() Methode bestätigt. Versuche ich allerdings dann erneut den Thread danach zu starten, so bekomme ich mehrere Fehlermeldungen:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

at java.lang.Thread.start(Unknown Source)

at Programmieren3.forum_stoppuhr.actionPerformed(forum_stoppuhr.java:120)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Woran liegt das?

Ich hoffe, ihr könnt mir weiterhelfen und ein paar Tips geben, besonders in Bezug auf wait(), notify().

Vielen Dank im Voruas.

Chris


import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextField;


public class forum_stoppuhr extends JFrame implements Runnable, ActionListener{

	private static final long serialVersionUID = 1L;

	private static JButton b1 = new JButton("Starte Stoppuhr"), b2 = new JButton("Stoppe Stoppuhr");

	private static JTextField t1 = new JTextField("00:00");

	private static boolean weiter = true;

	private Thread thread1 = new Thread(this);




	public forum_stoppuhr(){

		super("Stoppuhr");

		setSize(400,80);

		setLayout(new BorderLayout());

		t1.setEditable(false);

		b1.setPreferredSize(new Dimension(160,30));

		b1.addActionListener(this);

		b1.setActionCommand("start");

		b2.setActionCommand("stop");

		b2.addActionListener(this);

		b2.setPreferredSize(new Dimension(160,30));

		add(t1, BorderLayout.NORTH);

		add(b1, BorderLayout.EAST);

		add(b2, BorderLayout.WEST);

		setVisible(true);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}	




	public static void main(String[] args) {

		new forum_stoppuhr();

	}


	public void run() {

		String min = "0", sek = "0";

		while(weiter){

			if(t1.getText().equals("00:00")){

				t1.setText("00:01");

				try {

					Thread.sleep(1000);

				} catch (InterruptedException e) {

					// TODO Auto-generated catch block

					System.out.println("interrupted");

				}

			}else{

				min = String.valueOf(Integer.parseInt(t1.getText().substring(0, 2)));


				sek = String.valueOf(Integer.parseInt(t1.getText().substring(3, 5)) + 1);



				if(Integer.parseInt(sek)>=60){

					sek = String.valueOf(Integer.parseInt(sek) + (Integer.parseInt(sek)%60));

					min = String.valueOf(Integer.parseInt(min) + (Integer.parseInt(sek)/60));

					sek = "0";

				}


				/*

				System.out.println("Minuten:"+ min);

				System.out.println("Sekunden:"+ sek);

				*/

				//Ergänzung um Nullen auf zwei Stellen

				if(min.length()<2){

					min = "0" + min;

				}

				if(sek.length()<2){

					sek = "0" + sek;

				}


				t1.setText(min + ":" + sek);



				try {

					Thread.sleep(1000);

				} catch (InterruptedException e) {

					System.out.println("interrupted");

				}

			}

		}

	}


	public void actionPerformed(ActionEvent e) {

		if(e.getActionCommand().equals("start")){

			if(thread1.isAlive() == true){

				System.out.println("Ausgabe wird fortgesetzt!");

				//Was muss hier implementiert werden?


			}else{

				System.out.println("Thread wird gestartet");

				thread1.start();

				System.out.println("Lebensstatus des Threads direkt nach dem Starten 1:" + thread1.isAlive());

			}

		}else if(e.getActionCommand().equals("stop")){

			weiter=false;

			System.out.println("Ausgabe wurde gestoppt!");			

		}

	}

}







Einige praktische Beispiele, zum starten/stoppen/unterbrechen/forsetzen findest du unter [Java Thread Primitive Deprecation].

Gruß

ich würd nen timer benutzen ;)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.