مشخصات مقاله
-
1408
-
0.0
-
4083
-
0
-
0
آموزش Java – کار با متد join() در پردازش موازی
آموزش Java – کار با متد join() در پردازش موازی
متد join() منتظر می ماند تا thread جاری عملیات خود را تکمیل کند، سپس به thread بعدی اجازه ی اجرا می دهد. به عبارت دیگر، مقدار زمان مشخصی صبر می کند تا thread ای که بر روی آن فراخوانی شده کار خود را به پایان برساند و از حافظه حذف شود سپس به thread بعدی امکان اجرا می دهد.
دستور استفاده از متد join():
public void join()throws InterruptedException public void join(long milliseconds)throws InterruptedException
مثال کاربردی از متد join():
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
خروجی:
1 2 3 4 5 1 1 2 2 3 3 4 4 5 5
همان طور که در مثال بالا مشاهده می کنید، زمانی که t1 عملیات خود را به پایان می رساند، t2 و سپس t3 شروع به اجرا می نمایند.
مثال کاربردی از متد join(long milliseconds):
class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod2 t1=new TestJoinMethod2();
TestJoinMethod2 t2=new TestJoinMethod2();
TestJoinMethod2 t3=new TestJoinMethod2();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
خروجی:
1 2 3 1 4 1 2 5 2 3 3 4 4 5 5
در مثال بالا زمانی که t1 عملیات خود را در طول (سه بار) 1500 میلی ثانیه به پایان می رساند، سپس t2 و t3 شروع به اجرا می نمایند.
مثال کاربردی از متدهای getName()، setName(String) و getId()
public String getName() public void setName(String name) public long getId()
مثال:
class TestJoinMethod3 extends Thread{
public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestJoinMethod3 t1=new TestJoinMethod3();
TestJoinMethod3 t2=new TestJoinMethod3();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
System.out.println("id of t1:"+t1.getId());
t1.start();
t2.start();
t1.setName("Sonoo Jaiswal");
System.out.println("After changing name of t1:"+t1.getName());
}
}
خروجی:
Name of t1:Thread-0 Name of t2:Thread-1 id of t1:8 running... After changling name of t1:Sonoo Jaiswal running...
مثال کاربردی از متد currentThread()
متد currentThread اشاره گری (reference) به آبجکت thread در حال اجرا را برمی گرداند.
دستور استفاده از متد:
public static Thread currentThread()
مثال کاربردی از متد currentThread():
class TestJoinMethod4 extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String args[]){
TestJoinMethod4 t1=new TestJoinMethod4();
TestJoinMethod4 t2=new TestJoinMethod4();
t1.start();
t2.start();
}
}
خروجی:
Thread-0 Thread-1