logo

Thread.sleep() in Java mit Beispielen

Die Java-Thread-Klasse stellt die beiden Varianten der Sleep()-Methode bereit. Die erste Variante akzeptiert nur ein Argument, während die andere Variante zwei Argumente akzeptiert. Die Methode sleep() wird verwendet, um die Arbeit eines Threads für eine bestimmte Zeitspanne anzuhalten. Die Zeit, bis zu der der Thread im Ruhezustand bleibt, wird als Ruhezeit des Threads bezeichnet. Nach Ablauf der Ruhezeit beginnt der Thread mit der Ausführung an der Stelle, an der er aufgehört hat.

Die Syntax der Methode „sleep()“:

Im Folgenden finden Sie die Syntax der Methode „sleep()“.

 public static void sleep(long mls) throws InterruptedException public static void sleep(long mls, int n) throws InterruptedException 

Die Methode sleep() mit dem einen Parameter ist die native Methode, und die Implementierung der nativen Methode erfolgt in einer anderen Programmiersprache. Die anderen Methoden mit den beiden Parametern sind nicht die native Methode. Das heißt, die Implementierung erfolgt in Java. Wir können mit Hilfe der Thread-Klasse auf die Sleep()-Methoden zugreifen, da die Signatur der Sleep()-Methoden das Schlüsselwort static enthält. Sowohl die native als auch die nicht-native Methode lösen eine aktivierte Ausnahme aus. Daher kann hier entweder der Try-Catch-Block oder das Schlüsselwort throws funktionieren.

So blenden Sie die Anwendung in Android ein

Die Thread.sleep()-Methode kann mit jedem Thread verwendet werden. Das bedeutet, dass jeder andere Thread oder der Hauptthread die Methode sleep() aufrufen kann.

Parameter:

Im Folgenden sind die Parameter aufgeführt, die in der Methode „sleep()“ verwendet werden.

mls: Die Zeit in Millisekunden wird durch den Parameter mls dargestellt. Die Dauer, für die der Thread schläft, wird durch die Methode sleep() angegeben.

N: Es zeigt die zusätzliche Zeit an, bis zu der der Programmierer oder Entwickler möchte, dass sich der Thread im Ruhezustand befindet. Der Bereich von n liegt zwischen 0 und 999999.

bash schlafen

Die Methode gibt nichts zurück.

Wichtige Punkte, die Sie bei der Sleep()-Methode beachten sollten

Immer wenn die Thread.sleep()-Methoden ausgeführt werden, wird die Ausführung des aktuellen Threads immer angehalten.

Immer wenn ein anderer Thread eine Unterbrechung durchführt, während sich der aktuelle Thread bereits im Ruhemodus befindet, wird die InterruptedException ausgelöst.

Wenn das System, das die Threads ausführt, ausgelastet ist, ist die tatsächliche Ruhezeit des Threads im Allgemeinen länger als die in Argumenten übergebene Zeit. Wenn jedoch das System, das die Methode „sleep()“ ausführt, weniger ausgelastet ist, entspricht die tatsächliche Ruhezeit des Threads fast der im Argument übergebenen Zeit.

Beispiel für die Methode „sleep()“ in Java: im benutzerdefinierten Thread

Das folgende Beispiel zeigt, wie man die Methode „sleep()“ im benutzerdefinierten Thread verwenden kann.

Dateiname: TestSleepMethod1.java

 class TestSleepMethod1 extends Thread{ public void run(){ for(int i=1;i<5;i++){ 500 the thread will sleep for milli seconds try{thread.sleep(500);}catch(interruptedexception e){system.out.println(e);} system.out.println(i); } public static void main(string args[]){ testsleepmethod1 t1="new" testsleepmethod1(); t2="new" t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 2 2 3 3 4 4 </pre> <p>As you know well that at a time only one thread is executed. If you sleep a thread for the specified time, the thread scheduler picks up another thread and so on.</p> <h3>Example of the sleep() Method in Java : on the main thread</h3> <p> <strong>FileName:</strong> TestSleepMethod2.java</p> <pre> // important import statements import java.lang.Thread; import java.io.*; public class TestSleepMethod2 { // main method public static void main(String argvs[]) { try { for (int j = 0; j <5; 1 1000 j++) { the main thread sleeps for milliseconds, which is sec whenever loop runs thread.sleep(1000); displaying value of variable system.out.println(j); } catch (exception expn) catching exception system.out.println(expn); < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 </pre> <h3>Example of the sleep() Method in Java: When the sleeping time is -ive</h3> <p>The following example throws the exception IllegalArguementException when the time for sleeping is negative.</p> <p> <strong>FileName:</strong> TestSleepMethod3.java</p> <pre> // important import statements import java.lang.Thread; import java.io.*; public class TestSleepMethod3 { // main method public static void main(String argvs[]) { // we can also use throws keyword followed by // exception name for throwing the exception try { for (int j = 0; j <5; j++) { it throws the exception illegalargumentexception as time is -ive which -100 thread.sleep(-100); displaying variable's value system.out.println(j); } catch (exception expn) iscaught here system.out.println(expn); < pre> <p> <strong>Output:</strong> </p> <pre> java.lang.IllegalArgumentException: timeout value is negative </pre> <hr></5;></pre></5;></pre></5;i++){>

Wie Sie wissen, wird jeweils nur ein Thread ausgeführt. Wenn Sie einen Thread für die angegebene Zeit in den Ruhezustand versetzen, nimmt der Thread-Scheduler einen anderen Thread auf und so weiter.

dynamisches Array in Java

Beispiel für die Methode „sleep()“ in Java: im Hauptthread

Dateiname: TestSleepMethod2.java

 // important import statements import java.lang.Thread; import java.io.*; public class TestSleepMethod2 { // main method public static void main(String argvs[]) { try { for (int j = 0; j <5; 1 1000 j++) { the main thread sleeps for milliseconds, which is sec whenever loop runs thread.sleep(1000); displaying value of variable system.out.println(j); } catch (exception expn) catching exception system.out.println(expn); < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 </pre> <h3>Example of the sleep() Method in Java: When the sleeping time is -ive</h3> <p>The following example throws the exception IllegalArguementException when the time for sleeping is negative.</p> <p> <strong>FileName:</strong> TestSleepMethod3.java</p> <pre> // important import statements import java.lang.Thread; import java.io.*; public class TestSleepMethod3 { // main method public static void main(String argvs[]) { // we can also use throws keyword followed by // exception name for throwing the exception try { for (int j = 0; j <5; j++) { it throws the exception illegalargumentexception as time is -ive which -100 thread.sleep(-100); displaying variable\'s value system.out.println(j); } catch (exception expn) iscaught here system.out.println(expn); < pre> <p> <strong>Output:</strong> </p> <pre> java.lang.IllegalArgumentException: timeout value is negative </pre> <hr></5;></pre></5;>

Beispiel für die Methode „sleep()“ in Java: Wenn die Schlafzeit -ive ist

Das folgende Beispiel löst die Ausnahme IllegalArguementException aus, wenn die Schlafzeit negativ ist.

Dateiname: TestSleepMethod3.java

 // important import statements import java.lang.Thread; import java.io.*; public class TestSleepMethod3 { // main method public static void main(String argvs[]) { // we can also use throws keyword followed by // exception name for throwing the exception try { for (int j = 0; j <5; j++) { it throws the exception illegalargumentexception as time is -ive which -100 thread.sleep(-100); displaying variable\'s value system.out.println(j); } catch (exception expn) iscaught here system.out.println(expn); < pre> <p> <strong>Output:</strong> </p> <pre> java.lang.IllegalArgumentException: timeout value is negative </pre> <hr></5;>