logo

Thread.sleep() Java nyelven példákkal

A Java Thread osztály a sleep() metódus két változatát biztosítja. Az első csak egy argumentumot fogad el, míg a másik változat két argumentumot. A sleep() metódus egy szál működésének egy adott időre történő leállítására szolgál. Azt az időt, ameddig a fonal alvó állapotban marad, a szál alvási idejeként ismerjük. Az alvásidő letelte után a szál onnan kezdi a végrehajtást, ahol abbahagyta.

Az alvás() metódus szintaxisa:

Az alábbiakban a sleep() metódus szintaxisa látható.

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

Az egy paraméterrel rendelkező sleep() metódus a natív metódus, a natív metódus megvalósítása pedig egy másik programozási nyelven valósul meg. A két paraméterrel rendelkező többi metódus nem a natív módszer. Vagyis a megvalósítása Java nyelven történik. A sleep() metódusokat a Thread osztály segítségével érhetjük el, mivel a sleep() metódusok aláírása tartalmazza a static kulcsszót. A natív, valamint a nem natív módszer bejelölt kivételt dob. Ezért vagy a try-catch blokk, vagy a throws kulcsszó működhet itt.

np hol

A Thread.sleep() metódus bármely szálhoz használható. Ez azt jelenti, hogy bármely más szál vagy a főszál meghívhatja a sleep() metódust.

Paraméterek:

Az alábbiak a sleep() metódusban használt paraméterek.

mls: A milliszekundumban megadott időt az mls paraméter jelöli. A szál alvási időtartamát a sleep() metódus adja meg.

n: Megmutatja azt a további időt, ameddig a programozó vagy a fejlesztő azt szeretné, hogy a szál alvó állapotban legyen. Az n tartománya 0 és 999999 között van.

tömb lista java

A módszer nem ad vissza semmit.

Fontos tudnivalók a Sleep() módszerrel kapcsolatban

Amikor a Thread.sleep() metódus fut, mindig leállítja az aktuális szál végrehajtását.

Amikor egy másik szál megszakad, miközben az aktuális szál már alvó módban van, akkor a Megszakított kivétel megjelenik.

Ha a szálakat végrehajtó rendszer foglalt, akkor a szál tényleges alvási ideje általában több, mint az argumentumokban eltelt idő. Ha azonban a sleep() metódust végrehajtó rendszernek kisebb a terhelése, akkor a szál tényleges alvási ideje majdnem megegyezik az argumentumban eltelt idővel.

Példa a sleep() metódusra Java-ban: az egyéni szálon

A következő példa bemutatja, hogyan használható a sleep() metódus az egyéni szálon.

Fájl név: 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++){>

Jól tudod, hogy egyszerre csak egy szál kerül végrehajtásra. Ha elalszik egy szálat a megadott ideig, a szálütemező újabb szálat vesz fel és így tovább.

szkriptek futtatása linux alatt

Példa a sleep() metódusra Java-ban: a főszálon

Fájl név: 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;>

Példa a sleep() metódusra Java-ban: Amikor az alvási idő -ive

A következő példa az IllegalArguementException kivételt adja meg, ha az alvás ideje negatív.

Fájl név: 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;>