Not sure sure if I am doing this right. I need to make a new thread to write out message certain number of times. I think this works so far but not sure if its the best way of doing it. Then i need to display another message after thread has finished running. How do I do that ? Using isAlive() ? How do i implement that ?
public class MyThread extends Thread {
public void run() {
int i = 0;
while (i < 10) {
System.out.println("hi");
i++;
}
}
public static void main(String[] args) {
String n = Thread.currentThread().getName();
System.out.println(n);
Thread t = new MyThread();
t.start();
}
}
Till now you are on track. Now, to display another message, when this thread has finished, you can invoke
Thread#joinon this thread from your main thread. You would also need to handleInterruptedException, when you uset.joinmethod.Then your main thread will continue, when your thread
thas finished. So, continue your main thread like this: –When your call
t.joinin a particular thread (here, main thread), then that thread will continue its further execution, only when the threadthas completed its execution.