دوره‌ای که می‌تونه مسیر شغلیت رو عوض کنه! دوره‌ای که می‌تونه مسیر شغلیت رو عوض کنه!
🎯 ثبت نام

آموزش Java – کار با متد join() در پردازش موازی

آموزش Java – کار با متد join() در پردازش موازی

متد join() منتظر می ماند تا thread جاری عملیات خود را تکمیل کند، سپس به thread بعدی اجازه ی اجرا می دهد. به عبارت دیگر، مقدار زمان مشخصی صبر می کند تا thread ای که بر روی آن فراخوانی شده کار خود را به پایان برساند و از حافظه حذف شود سپس به thread بعدی امکان اجرا می دهد.

دستور استفاده از متد join():

1
2
3
public void join()throws InterruptedException
public void join(long milliseconds)throws InterruptedException 
<button></button>

مثال کاربردی از متد join():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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(); 
 
<button></button>

خروجی:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
1
1
2
2
3
3
4
4
5
5
<button></button>

همان طور که در مثال بالا مشاهده می کنید، زمانی که t1 عملیات خود را به پایان می رساند، t2 و سپس t3 شروع به اجرا می نمایند.

مثال کاربردی از متد join(long milliseconds):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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(); 
 
<button></button>

خروجی:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
1
4
1
2
5
2
3
3
4
4
5
5
<button></button>

در مثال بالا زمانی که t1 عملیات خود را در طول (سه بار) 1500 میلی ثانیه به پایان می رساند، سپس t2 و t3 شروع به اجرا می نمایند.

مثال کاربردی از متدهای getName()، setName(String) و getId()

1
2
3
4
public String getName()
public void setName(String name)
public long getId()
<button></button>

مثال:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()); 
 
<button></button>

خروجی:

1
2
3
4
5
6
7
Name of t1:Thread-0
Name of t2:Thread-1
id of t1:8
running...
After changling name of t1:Sonoo Jaiswal
running...
<button></button>

مثال کاربردی از متد currentThread()

متد currentThread اشاره گری (reference) به آبجکت thread در حال اجرا را برمی گرداند.

دستور استفاده از متد:

1
public static Thread currentThread()<button></button>

مثال کاربردی از متد currentThread():

1
2
3
4
5
6
7
8
9
10
11
12
13
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(); 
 
<button></button>

خروجی:

1
2
3
Thread-0
Thread-1
<button></button>
1396/08/05 3703 1331
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

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