logo

Örneklerle Java'da Thread.sleep()

Java Thread sınıfı, uyku() yönteminin iki çeşidini sağlar. Birincisi yalnızca bir argümanı kabul ederken, diğer değişken iki argümanı kabul eder. Sleep() yöntemi belirli bir süre boyunca bir iş parçacığının çalışmasını durdurmak için kullanılıyor. İpliğin uyku durumunda kaldığı süre, ipliğin uyku süresi olarak bilinir. Uyku süresi bittikten sonra thread kaldığı yerden çalışmaya başlar.

Sleep() Yöntem Sözdizimi:

Sleep() yönteminin sözdizimi aşağıdadır.

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

Tek parametreli uyku() yöntemi yerel yöntemdir ve yerel yöntemin uygulanması başka bir programlama dilinde gerçekleştirilir. İki parametreye sahip diğer yöntemler yerel yöntem değildir. Yani, uygulanması Java'da gerçekleştirilir. Sleep() yöntemlerinin imzası statik anahtar kelimeyi içerdiğinden, uyku() yöntemlerine Thread sınıfının yardımıyla erişebiliriz. Yerel ve yerel olmayan yöntem, işaretli bir İstisna oluşturur. Bu nedenle, try-catch bloğu veya throws anahtar sözcüğü burada çalışabilir.

Thread.sleep() yöntemi herhangi bir iş parçacığıyla kullanılabilir. Bu, başka herhangi bir iş parçacığının veya ana iş parçacığının uyku() yöntemini çağırabileceği anlamına gelir.

Parametreler:

Sleep() yönteminde kullanılan parametreler aşağıdadır.

ml: Milisaniye cinsinden süre mls parametresi ile temsil edilir. İş parçacığının uyuyacağı süre, uyku() yöntemiyle verilir.

N: Programcının veya geliştiricinin iş parçacığının uyku durumunda olmasını istediği ek süreyi gösterir. N aralığı 0 ila 999999 arasındadır.

Yöntem hiçbir şey döndürmez.

Sleep() Yöntemi Hakkında Hatırlanması Gereken Önemli Noktalar

Thread.sleep() yöntemleri çalıştırıldığında, geçerli iş parçacığının yürütülmesi her zaman durdurulur.

Geçerli iş parçacığı zaten uyku modundayken başka bir iş parçacığı kesintiye uğradığında, InterruptedException oluşturulur.

İş parçacıklarını yürüten sistem meşgulse iş parçacığının gerçek uyku süresi genellikle argümanlarda geçen süreye kıyasla daha fazladır. Bununla birlikte, eğer uyku() yöntemini çalıştıran sistem daha az yüke sahipse, iş parçacığının gerçek uyku süresi neredeyse argümanda geçen süreye eşittir.

Java'daki uyku() yöntemi örneği: özel iş parçacığında

Aşağıdaki örnek, özel iş parçacığında uyku() yönteminin nasıl kullanılabileceğini gösterir.

Dosya adı: 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++){>

Bildiğiniz gibi bir seferde yalnızca bir iş parçacığı yürütülür. Bir iş parçacığını belirtilen süre boyunca uyutursanız iş parçacığı zamanlayıcı başka bir iş parçacığını alır ve bu şekilde devam eder.

Java'daki uyku() Yöntemi örneği: ana iş parçacığında

Dosya adı: 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;>

Java'daki uyku() Yöntemi örneği: Uyku süresi -ive olduğunda

Aşağıdaki örnek, uyku zamanı negatif olduğunda IllegalArguementException istisnasını atar.

Dosya adı: 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;>