logo

Thread.sleep() metode Java ar piemēriem

Pavedienu klase ir klase, kas būtībā ir programmu izpildes pavediens. Tas atrodas Java.lang pakotnē. Pavedienu klase satur Gulēt() metodi. Pavedienu klasē ir divas pārslogotas Sleep() metodes metodes, viena ir ar vienu argumentu, bet otra ir ar diviem argumentiem. Sleep() metode tiek izmantota, lai apturētu pašreizējā pavediena izpildi (atkarībā no tā, kurš varētu tikt izpildīts sistēmā) uz noteiktu laiku, un pēc šī laika beigām pavediens, kas tiek izpildīts agrāk, sāk izpildīt no jauna.

Svarīgs punkts attiecībā uz Thread.sleep() metodi:



  • Metode Ikreiz, kad Thread.sleep() tiek izpildītas funkcijas, tā vienmēr aptur pašreizējā pavediena izpildi.
  • Ja kāds cits pavediens pārtrauc, kad pavediens guļ, tiks izmests InterruptedException.
  • Ja sistēma ir aizņemta, faktiskais pavediena miega laiks būs ilgāks, salīdzinot ar laiku, kas pagājis miega metodes izsaukšanas laikā, un, ja sistēmai ir mazāka slodze, tad pavediena faktiskais miega laiks būs tuvu tam, kas pagājis. miega() metodes izsaukšana.

Sleep() metodes sintakse

Java Thread ir 4 miega() metodes varianti. Šie ir:

 1.  public static void sleep(long millis)throws InterruptedException 2.  public static void sleep(long millis)throws IllegalArguementException 3.  public static void sleep(long millis, int nanos)throws InterruptedException 4.  public static void sleep(long millis, int nanos)throws IllegalArguementException>

Parametri, kas nodoti Thread.Sleep() Method



    millis: Laika ilgums milisekundēs, kurā pavediens snaudīsies nanos: Šis ir papildu laiks nanosekundēs, kuru mēs vēlamies, lai pavediens gulētu. Tas svārstās no 0 līdz 999999.

Atgriešanas miega veids() metode: Tas neatgriež nekādu vērtību, t.i., miega funkcijas atgriešanas veids ir nederīgs.

Miega metode ar vienu parametru ir native metode, proti, šīs metodes ieviešana tiek veikta citā programmēšanas valodā un otra metode ar diviem parametriem nav dzimtā metode, proti, tās ieviešana tiek veikta Java. Abas miega metodes ir statiskas, ti, mēs varam tām piekļūt, izmantojot Thread klasi. Abas metodes rada pārbaudītu izņēmumu, ti, mēs varam rīkoties ar izņēmumu, izmantojot atslēgvārdu throws vai izmantojot try and catch bloku.

Mēs varam izmantot metodi Thread.Sleep() jebkuram pavedienam, t.i., mēs varam to izdarīt ar galveno pavedienu vai jebkuru citu pavedienu, ko izveidojam programmatiski.



1. Metodes Thread.Sleep() izmantošana galvenajam pavedienam

Java




// Java Program for sleeping the main thread.> import> java.io.*;> import> java.lang.Thread;> class> GFG {> >public> static> void> main(String[] args)> >{> >// we can also use throws keyword followed by> >// exception name for throwing the exception> > >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// it will sleep the main thread for 1 sec> >// ,each time the for loop runs> >Thread.sleep(>1000>);> > >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> }>

>

bezmaksas ipconfig
>

Izvade

0 1 2 3 4>

2. Metodes Thread.Sleep() izmantošana pielāgotam pavedienam

Java




// Java Program for sleeping the custom thread.> import> java.io.*;> import> java.lang.Thread;> class> GFG>extends> Thread {> >public> void> run()> >{> >// thread 0> >// we can also use throws keyword followed by> >// exception name for throwing the exception> >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// it will sleep the main thread for 1 sec> >// ,each time the for loop runs> >Thread.sleep(>1000>);> > >// This Thread.sleep() method will sleep the> >// thread 0.> >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> >public> static> void> main(String[] args)> >{> >// main thread> >GFG obj =>new> GFG();> >obj.start();> >}> }>

>

mamma kulkarni
>

Izvade

0 1 2 3 4>

3. IllegalArguementException, ja miega laiks ir negatīvs

Java




// Java Program for showing how exception can occur if we> // pass the negative timeout value.> import> java.io.*;> import> java.lang.Thread;> class> GFG {> >public> static> void> main(String[] args)> >{> >// we can also use throws keyword followed by> >// exception name for throwing the exception> > >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// this will throw the> >// IllegalArgumentException> >Thread.sleep(->100>);> > >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> }>

>

>

Izvade

java.lang.IllegalArgumentException: timeout value is negative>