کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش Java – متد interrupt() در multithreading و synchronization در Java

آموزش Java – متد interrupt() در multithreading و synchronization در Java

فراخوانی متد interrupt() بر روی نمونه ای از جنس کلاس Thread سبب می شود یک thread ای که در حال اجرای عملیات می باشد، آن را متوقف کند.

زمانی که یک thread در وضعیت sleep یا waiting قرار داشته باشد، بدین معنی که متد sleep() یا wait() فراخوانی شده باشد، فراخوانی متد interrupt() بر روی آبجکتی از جنس thread سبب مختل شدن وضعیت sleep/waiting آن شده و در نتیجه خطای InterruptedException صادر می شود. اگر thread در وضعیت sleep یا waiting قرار نداشته باشد، آنگاه فراخوانی متد interrupt() اجرای thread را مختل نمی کند و صرفا مقدار بولی interrupt flag را بر روی true تنظیم می کند.

کلاس Thread سه متد برای متوقف کردن اجرای عملیات thread در اختیار توسعه دهنده قرار می دهد که به شرح زیر می باشد:

  • public void interrupt()
  • public static boolean interrupted()
  • public boolean isInterrupted()

مثال کاربردی از متوقف کردن اجرای عملیات thread با متد interrupt()

در مثال جاری، پس از متوقف کردن (interrupt) اجرای عملیات thread، آن را داخل call stack قرار داده و از متد بالای پشته به پایین پشته جهت متوقف کردن اجرای آن پاس می دهیم (exception propagation). اگر نیازی برای متوقف کردن thread وجود نداشته باشد، می توان آن را به هنگام فراخوانی متد sleep() و wait() مدیریت کرد. در زیر ابتدا به مثالی می پردازیم که در آن exception propagation اتفاق می افتد.

class TestInterruptingThread1 extends Thread{  
public void run(){  
try{  
Thread.sleep(1000);  
System.out.println("task");  
}catch(InterruptedException e){  
throw new RuntimeException("Thread interrupted..."+e);  
}  
}  
public static void main(String args[]){  
TestInterruptingThread1 t1=new TestInterruptingThread1();  
t1.start();  
try{  
t1.interrupt();  
}catch(Exception e){System.out.println("Exception handled "+e);}  
}  
}  

خروجی:

Exception in thread-0  
java.lang.RuntimeException: Thread interrupted...
java.lang.InterruptedException: sleep interrupted
at A.run(A.java:7)

مثال کاربردی از interrupt کردن یک thread که به دنبال فراخوانی متد interrupt()، عملیات خود را متوقف نمی کند

در این مثال پس از فراخوانی متد interrupt بر روی thread، خطا مدیریت می شود به همین دلیل وضعیت sleep مختل می شود اما thread کار خود را متوقف نمی کند.

class TestInterruptingThread2 extends Thread{  
public void run(){  
try{  
Thread.sleep(1000);  
System.out.println("task");  
}catch(InterruptedException e){  
System.out.println("Exception handled "+e);  
}  
System.out.println("thread is running...");  
}  
public static void main(String args[]){  
TestInterruptingThread2 t1=new TestInterruptingThread2();  
t1.start();  
t1.interrupt();  
}  
}  

خروجی:

Exception handled  
java.lang.InterruptedException: sleep interrupted
thread is running...

مثال کاربردی از interrupt کردن یک thread که طبق انتظار عمل می کند

اگر thread در حالت sleep یا waiting قرار نداشته باشد، فراخوانی متد interrupt() مقدار بولی interrupted flag را بر روی true تنظیم می کند و به همین خاطر توسعه دهنده می تواند به راحتی در زمان لازم اجرای thread را متوقف کند.

class TestInterruptingThread3 extends Thread{  
public void run(){  
for(int i=1;i<=5;i++)  
System.out.println(i);  
}  
public static void main(String args[]){  
TestInterruptingThread3 t1=new TestInterruptingThread3();  
t1.start();  
t1.interrupt();  
}  
}  

خروجی:

1
2
3
4 
5

کاربرد متدهای isInterrupted() و interrupted()

متد interrupted() بررسی می کند آّیا thread جاری متوقف شده است یا خیر. فراخوانی این متد سبب می شود interrupted status مربوط به thread مقدار بولی false را برمی گرداند. به عبارت دیگر اگر متد نام برده دوبار پشت سرهم فراخوانی شود، فراخوانی دوم مقدار false را برمی گرداند (مگر اینکه thread جاری ، پس از اینکه فراخوانی اول وضعیت interrupted آن را پاک کرده و قبل از اینکه فراخوانی دوم وضعیت آن را بررسی کند، بار دیگر متوقف شود).

اگر توقف thread به دلیل فعال نبودن آن در زمان فراخوانی متدinterrupt ، انجام نشود، این امر با مقدار false که خروجی این متد می باشد نشان داده می شود.

متد isInterrupted() بررسی می کند آیا thread جاری متوقف شده است یا خیر. لازم به ذکر است وضعیت interrupted متد نام برده با فراخوانی این متد تحت تاثیر قرار نمی گیرد. در صورتی که thread مورد نظر متوقف شده باشد، مقدار بولی true و در غیر این صورت false را برمی گرداند.

public class TestInterruptingThread4 extends Thread{  
public void run(){  
for(int i=1;i<=2;i++){  
if(Thread.interrupted()){  
System.out.println("code for interrupted thread");  
}  
else{  
System.out.println("code for normal thread");  
}  
}//end of for loop  
}  
public static void main(String args[]){  
TestInterruptingThread4 t1=new TestInterruptingThread4();  
TestInterruptingThread4 t2=new TestInterruptingThread4();  
t1.start();  
t1.interrupt();  
t2.start();  
}  
}  

خروجی:

Code for interrupted thread
code for normal thread
code for normal thread
code for normal thread
1396/08/21 3203 1293
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...