Thread Class és una classe que és bàsicament un fil d'execució dels programes. Està present al paquet Java.lang. La classe de fil conté el Dormir () mètode. Hi ha dos mètodes sobrecarregats del mètode Sleep() presents a Thread Class, un amb un argument i un altre amb dos arguments. El mètode sleep() s'utilitza per aturar l'execució del fil actual (el que s'està executant al sistema) durant una durada específica del temps i després d'aquesta durada, el fil que s'està executant abans comença a executar-se de nou.
Punt important sobre el mètode Thread.sleep():
comanda d'instal·lació npm
- Mètode Sempre que Thread.sleep() s'executa, sempre posa en pausa l'execució del fil actual.
- Si qualsevol altre fil s'interromp quan el fil està dormint, es llançarà InterruptedException.
- Si el sistema està ocupat, aleshores el temps real que el fil s'adormirà serà més que el que s'ha passat mentre es crida al mètode de repòs i si el sistema té menys càrrega, el temps real de repòs del fil s'aproximarà al passat mentre cridant al mètode sleep().
Sintaxi del mètode Sleep().
Hi ha 4 variacions del mètode sleep() a Java Thread. Aquests són:
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>
Paràmetres passats al mètode Thread.Sleep().
- millis: Durada del temps en mil·lisegons durant el qual el fil dormirà nanos: Aquest és el temps addicional en nanosegons durant el qual volem que el fil s'adormi. Oscil·la entre 0 i 999999.
Mètode de retorn del tipus de son(): No retorna cap valor, és a dir, el tipus de retorn de la funció de repòs és nul.
El mètode sleep amb un paràmetre és un mètode natiu, és a dir, la implementació d'aquest mètode es fa en un altre llenguatge de programació i l'altre mètode amb dos paràmetres no és un mètode natiu, és a dir, la seva implementació es fa en Java. Els dos mètodes de son són estàtics, és a dir, podem accedir-hi mitjançant la classe Thread. Ambdós mètodes llancen una excepció marcada, és a dir, podem gestionar l'excepció utilitzant la paraula clau throws o utilitzant el bloc try and catch.
Podem utilitzar el mètode Thread.Sleep() per a qualsevol fil, és a dir, ho podem fer amb el fil principal o qualsevol altre fil que fem amb programació.
1. Utilitzant el mètode Thread.Sleep() per al fil principal
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);> >}> >}> }> |
>
>Sortida
0 1 2 3 4>
2. Utilitzant el mètode Thread.Sleep() per a un fil personalitzat
Java
bucle per al c
// 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();> >}> }> |
>
>Sortida
0 1 2 3 4>
3. IllegalArguementException quan el temps de son és negatiu
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);> >}> >}> }> |
>
>Sortida
java.lang.IllegalArgumentException: timeout value is negative>