Drop Down

Tuesday, January 22, 2019

MultiThreading - Child Thread Wait until Main Thread completes

package multithreading;
//Child Thread Wait until Main Thread completes
public class MyThread6_Join
{

public static void main(String[] args) throws InterruptedException
{
ThreadJoin.mt = Thread.currentThread();
ThreadJoin tj = new ThreadJoin();
tj.start();

  char[] ch = {'e','t','y','r','w','d','g','h','t','s','e','b','n','l','k'};
  for(char c : ch)
  {
  System.out.println(c);
  try
  {
Thread.sleep(200);
  }
  catch(Exception e)
  {
  System.out.println(e);
  }
  }

}

}
//************************************CHILD*********************************
class ThreadJoin extends Thread
{
static Thread mt;
public void run()
{

try
{
mt.join();
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}

int[] arr = {5,7,8,9,2,3,4,11,33,55,77,55,33,99,100};
for(int a : arr)
{
System.out.println(a);
try
{
Thread.sleep(200);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}

No comments:

Post a Comment

Java 8 Notes Pics