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.

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

Ich hab ein Verständnisproblem bei meinem Programm:

import java.util.*;

import java.text.*;


public class Clock

extends Thread

{

	Calendar cal=new GregorianCalendar();


	public Clock(int timeHour,int timeMinute,String title)

	{

		wakeHour=timeHour;

		wakeMinute=timeMinute;

		this.title=title;


		start();

	}


	public Clock(int timeHour,String title)

	{

		this(timeHour,0,title);

	}


	public void run()

	{

		boolean checked=false;


		while(true)

		{

			try	{

				cal.setTime(new Date());


				if	(cal.get(Calendar.HOUR_OF_DAY)==wakeHour &&

					 cal.get(Calendar.MINUTE)==wakeMinute)

				{

					if(!checked)	{

						System.out.println(title);


						checked=true;

					}

				}	else	{

					System.out.println(cal.get(Calendar.HOUR_OF_DAY)+":"+

						cal.get(Calendar.MINUTE)+"   "+cal.getTime());

				}


				Thread.sleep(1000*10);	//10 Sekunden warten

			}	catch(InterruptedException e)	{

				System.out.println("interrupted");

			}

		}

	}


	private int wakeHour,wakeMinute;

	private String title;


	public static void main(String[] args)

	{

		Clock clock=new Clock(8,50,"Simpsons kommt");

	}

}

Ich zähle ja eigentlich mit Thread.sleep 10sekundenweise

-Wie kann es dann sein, daß das Programm dann bei der Ausgabe(siehe unten rot markiert) von der 7sekunden-Stelle auf die 8sekunden-Stelle umspringt?

8:46 Thu Jul 18 08:46:37 CEST 2002

8:46 Thu Jul 18 08:46:47 CEST 2002

8:46 Thu Jul 18 08:46:57 CEST 2002

8:47 Thu Jul 18 08:47:07 CEST 2002

8:47 Thu Jul 18 08:47:17 CEST 2002

8:47 Thu Jul 18 08:47:27 CEST 2002

8:47 Thu Jul 18 08:47:37 CEST 2002

8:47 Thu Jul 18 08:47:47 CEST 2002

8:47 Thu Jul 18 08:47:57 CEST 2002

8:48 Thu Jul 18 08:48:07 CEST 2002

8:48 Thu Jul 18 08:48:18 CEST 2002

8:48 Thu Jul 18 08:48:28 CEST 2002

8:48 Thu Jul 18 08:48:38 CEST 2002

Original geschrieben von Krain

Ich zähle ja eigentlich mit Thread.sleep 10sekundenweise

-Wie kann es dann sein, daß das Programm dann bei der Ausgabe(siehe unten rot markiert) von der 7sekunden-Stelle auf die 8sekunden-Stelle umspringt?

Weil der Rest des Programms auch Zeit für die Ausführung braucht. Das summiert halt sich mit der Zeit.

Hallo,

du zählst nicht 10 Sekundenweise, sondern du schläft jeweils 10 Sekunden. Dazwischen wird allerdings immer ein paar Millisekunden gearbeitet, dadurch kommt irgendwann der Sprung.

Wenn du das Programm so weiterlaufen läßt, kommt irgendwann auch der nächste Sprung.

Gruß Jaraz

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

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.