logo

Thread.sleep() في Java مع أمثلة

توفر فئة Java Thread نوعين مختلفين من طريقة السكون (). الأول يقبل وسيطة فقط، بينما يقبل المتغير الآخر وسيطتين. يتم استخدام طريقة السكون () لإيقاف عمل سلسلة الرسائل لفترة زمنية معينة. يُعرف الوقت الذي يظل فيه الخيط في حالة النوم بوقت نوم الخيط. بعد انتهاء فترة النوم، يبدأ الخيط في التنفيذ من حيث توقف.

صيغة طريقة النوم () :

فيما يلي بناء جملة طريقة النوم ().

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

طريقة النوم () مع المعلمة الواحدة هي الطريقة الأصلية، ويتم تنفيذ الطريقة الأصلية بلغة برمجة أخرى. الطرق الأخرى التي تحتوي على المعلمتين ليست هي الطريقة الأصلية. أي أن تنفيذه يتم في Java. يمكننا الوصول إلى أساليب النوم () بمساعدة فئة الموضوع، حيث أن توقيع أساليب النوم () يحتوي على الكلمة الأساسية الثابتة. تقوم الطريقة الأصلية، وكذلك الطريقة غير الأصلية، بطرح استثناء محدد. لذلك، يمكن أن تعمل هنا إما كتلة محاولة الالتقاط أو الكلمة الرئيسية للرمي.

مليون بالأرقام

يمكن استخدام طريقة Thread.sleep() مع أي مؤشر ترابط. هذا يعني أن أي مؤشر ترابط آخر أو الخيط الرئيسي يمكنه استدعاء طريقة النوم ().

حدود:

فيما يلي المعلمات المستخدمة في طريقة النوم ().

مل: يتم تمثيل الوقت بالمللي ثانية بواسطة المعلمة mls. يتم تحديد المدة التي سينام فيها الخيط من خلال طريقة النوم ().

ن: يُظهر الوقت الإضافي الذي يريد المبرمج أو المطور أن يكون الخيط في حالة السكون خلاله. نطاق n هو من 0 إلى 999999.

الطريقة لا ترجع أي شيء.

نواة ميكروليثيك

نقاط مهمة يجب تذكرها حول طريقة Sleep()

عند تنفيذ أساليب Thread.sleep()، فإنها دائمًا ما توقف تنفيذ مؤشر الترابط الحالي.

عندما يقوم مؤشر ترابط آخر بالمقاطعة بينما يكون مؤشر الترابط الحالي في وضع السكون بالفعل، فسيتم طرح InterruptedException.

إذا كان النظام الذي ينفذ سلاسل الرسائل مشغولاً، فإن وقت النوم الفعلي لمؤشر الترابط يكون بشكل عام أكبر مقارنة بالوقت الذي يمر في الوسائط. ومع ذلك، إذا كان النظام الذي ينفذ طريقة Sleep() لديه تحميل أقل، فإن وقت النوم الفعلي للخيط يساوي تقريبًا الوقت المنقضي في الوسيطة.

تحويل شار إلى جافا int

مثال على طريقة السكون () في Java: في مؤشر الترابط المخصص

يوضح المثال التالي كيف يمكن استخدام طريقة السكون () في مؤشر الترابط المخصص.

اسم الملف: 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++){>

كما تعلم جيدًا أنه في المرة الواحدة يتم تنفيذ موضوع واحد فقط. إذا قمت بنوم مؤشر ترابط للوقت المحدد، فإن برنامج جدولة مؤشر الترابط يلتقط مؤشر ترابط آخر وما إلى ذلك.

مثال على طريقة النوم () في Java: في الموضوع الرئيسي

اسم الملف: 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: عندما يكون وقت النوم هو -ive

يطرح المثال التالي الاستثناء IllegalArguementException عندما يكون وقت النوم سالبًا.

اسم الملف: 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;>