logo

Thread.sleep() Javassa esimerkkien kanssa

Java Thread -luokka tarjoaa sleep()-menetelmän kaksi muunnelmaa. Ensimmäinen hyväksyy vain argumentin, kun taas toinen variantti hyväksyy kaksi argumenttia. Menetelmää sleep() käytetään säikeen toiminnan pysäyttämiseen tietyksi ajaksi. Aika, johon asti lanka pysyy lepotilassa, tunnetaan langan nukkumisajana. Nukkumisajan päätyttyä lanka aloittaa suorituksensa siitä, mihin se on jäänyt.

Sleep()-menetelmän syntaksi:

Seuraavassa on sleep()-menetelmän syntaksi.

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

Menetelmä sleep() yhdellä parametrilla on natiivi menetelmä, ja natiivimenetelmän toteutus suoritetaan toisella ohjelmointikielellä. Muut menetelmät, joilla on kaksi parametria, eivät ole alkuperäisiä. Eli sen toteutus suoritetaan Javalla. Sleep()-menetelmiin pääsee käsiksi Thread-luokan avulla, koska sleep()-menetelmien allekirjoitus sisältää staattisen avainsanan. Alkuperäinen, samoin kuin ei-natiivi menetelmä, heittää valitun Poikkeuksen. Siksi joko try-catch block tai heitot-avainsana voivat toimia tässä.

Thread.sleep()-menetelmää voidaan käyttää minkä tahansa säikeen kanssa. Se tarkoittaa, että mikä tahansa muu säie tai pääsäie voi kutsua sleep()-menetelmän.

päivämäärä merkkijonoksi

Parametrit:

Seuraavat ovat sleep()-menetelmässä käytetyt parametrit.

ml: Aika millisekunteina esitetään parametrilla mls. Säikeen lepotilan kesto annetaan menetelmällä sleep().

n: Se näyttää lisäajan, johon asti ohjelmoija tai kehittäjä haluaa säikeen olevan lepotilassa. Alue n on 0 - 999999.

css lihavoitu teksti

Menetelmä ei palauta mitään.

Tärkeitä huomioita Sleep()-menetelmästä

Aina kun Thread.sleep() -menetelmät suoritetaan, se pysäyttää aina nykyisen säikeen suorituksen.

Aina kun toinen säie keskeyttää nykyisen säikeen ollessa jo lepotilassa, InterruptedException heitetään.

vlc lataa videoita youtubesta

Jos säikeitä suorittava järjestelmä on varattu, säikeen todellinen lepoaika on yleensä enemmän kuin argumenteissa kulunut aika. Jos kuitenkin sleep()-menetelmää suorittavalla järjestelmällä on vähemmän kuormitusta, säikeen todellinen nukkumisaika on melkein sama kuin argumentissa kulunut aika.

Esimerkki sleep()-menetelmästä Javassa: mukautetussa säikeessä

Seuraava esimerkki näyttää, kuinka voit käyttää sleep()-menetelmää mukautetussa säikeessä.

Tiedoston nimi: TestSleepMethod1.java

hiiren rulla ei rullaa kunnolla
 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++){>

Kuten tiedät hyvin, että kerrallaan suoritetaan vain yksi säie. Jos unohdat säiettä määritetyn ajan, säikeen ajoitus poimii toisen säikeen ja niin edelleen.

Esimerkki sleep()-menetelmästä Javassa: pääsäikeessä

Tiedoston nimi: 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;>

Esimerkki sleep()-menetelmästä Javassa: Kun nukkumisaika on -ive

Seuraava esimerkki heittää poikkeuksen IllegalArguementException, kun nukkumisaika on negatiivinen.

Tiedoston nimi: 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;>