1

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();
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Tom
  • 421
  • 3
  • 6
  • 12
  • The code you posted does not compile. You are missing closing curly brackets. This would be easier to see if you indented your code. – unholysampler Nov 08 '12 at 16:42
  • Why don't you put your last message at the end of your run() method ? This way the message will display and the thread will finish just 1 ms later. – Mickäel A. Nov 08 '12 at 16:43

3 Answers3

5

Till now you are on track. Now, to display another message, when this thread has finished, you can invoke Thread#join on this thread from your main thread. You would also need to handle InterruptedException, when you use t.join method.

Then your main thread will continue, when your thread t has finished. So, continue your main thread like this: -

t.start();
try {
    t.join();
} catch (InterruptedException e) {
    e.printStackTrace();
}
System.out.println("Your Message"); 

When your call t.join in a particular thread (here, main thread), then that thread will continue its further execution, only when the thread t has completed its execution.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @Tom With addition of Concurrent package this is not the only way. You can have a look at [CyclicBarrier](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CyclicBarrier.html) – Amit Deshpande Nov 08 '12 at 17:15
  • @AmitD Thank you for your addition, but this is a specific tasks we were given to do but I will definitely look at the different way of doing this thanks! – Tom Nov 08 '12 at 17:52
1

Extending the Thread class itself is generally not a good practice.

You should create an implementation of the Runnable interface as follows:

public class MyRunnable implements Runnable {

    public void run() {
        //your code here
    }
}

And pass an intance of it to the thread as follows:

MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();

Please check this answer here on SO: Implementing Runnable vs. extending Thread

Community
  • 1
  • 1
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

This is how you can do that.........

class A implements Runnable
{
    public void run()
    {
    for(int i=1;i<=10;i++)
    System.out.println(Thread.currentThread().getName()+"\t"+i+"  hi");
    }
}
class join1
{
public static void main(String args[])throws Exception
    {
    A a=new A();
    Thread t1=new Thread(a,"abhi");
    t1.start();
    t1.join();
    System.out.println("hello this is me");//the message u want to display
    }
}

see join() details on join

Abhishekkumar
  • 1,102
  • 8
  • 24
  • What change did you made? And why? How will it affect the output? Can you explain it, rather than pasting silent code? – Rohit Jain Nov 08 '12 at 16:57
  • i am only showing my code sir i am not denying about your code as i am new so i am here to learn from people like you. – Abhishekkumar Nov 08 '12 at 17:02
  • @Abhishek.. Sure you will certainly learn many things on SO. But, in general, we expect that along with your code, you also add some explanation in your answer, so that OP can understand what you want to do. I'm not telling that your code is wrong. It is perfectly fine. But an explanation will certainly help OP. This will be helpful for you in future also. – Rohit Jain Nov 08 '12 at 17:06