I want to resume a thread after antoher thread finishes its execution.

Hi,

I have the following scenario. I want to print thecontent string of my threadclass A. But the fact is while therun method is changing thecontent string , theThreadTest returns to its execution and trying to outpuot the null string. I want theSystem.out.println(a.getContent()) line to execute after therun method finishes changing thecontent string. How can i do it?

Please Help!

publicclass ThreadTest

{

public ThreadTest()

{

A a =new A();

System.out.println(a.getContent());

}

publicstaticvoid main(String arg[])

{

new ThreadTest();

}

}

class Aextends Thread

{

String content =new String();

public A()

{

start();

}

publicvoid run()

{

for(int i = 0; i <= 100; i++){

content = content.concat("Admin"+i);

}

//System.out.println(content);

}

publicvoid setContent(String content)

{

this.content = content;

}

public String getContent()

{

return this.content;

}

}

[2351 byte] By [mr47a] at [2007-11-26 16:34:12]
# 1
Look at Thread.join()
dannyyatesa at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 2
I would recommend "wait & notify" for this task.
_Dima_a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 3
public ThreadTest() {A a = new A();a.join(); //Add join hereSystem.out.println(a.getContent());}
Caffeine0001a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 4
hey it didnt worked after i used the join method. same problem persists. it is still taking the null string. another thing i would like to add here. i have synchronized the setContent and getContent method. even now its not working. what can be the possible reason?
mr47a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 5
The start does not belong in the constructor of A.The start() belongs: new ThreadTest().start();
cooper6a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 6

hey

The ThreadTest class hasn't extend Thread class. how can it be like

new ThreadTest().start()

? The class A is the thread class that has extend the parent Thread. so i could have initiate it by this:

new A().start()

but my question was i am yet to find out a clue how to resume my main thread after another thread finishes execution.

mr47a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 7

You are correct in spotting the error the that proposed solution.

You are also correct that the proposed solution is nonsense. Whilst it's generally a bad idea to call start() within a constructor (any constructor), there is nothing about doing so which will cause the failure you're observing.

> but my question was i am yet to find out a clue how

> to resume my main thread after another thread

> finishes execution.

This question is fundamentally flawed. The point of threads is that they run simultaneously. So, once you call start() on a thread, it runs in parallel to the thread that called start().

What you actually want to do is it make the main thread pause and wait for the second thread to finish.

As already pointed out by me and another poster, Thread.join() is the way to do this. If it doesn't work for you, then there is something else about your code that is wrong, or the code you have posted is different from the code you are running.

dannyyatesa at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 8
In fact, I have tried your code with the appropriate call to join() and it works fine for me.You should be aware, however, that because it is multi-threaded code, if there are bugs in it then they will be non-deterministic and may not appear on my machine.
dannyyatesa at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 9

Thanks a lot dannyyates for the solution and thank you also for spotting my errors.

You are exactly right. The code i was running is different from the code i have given here.

so now i have just a single question left in my mind. what if i synchronized both of these to method setContent and getContent method here? would that have worked? i have tried but couldn't get them to work!

mr47a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 10

The answer to that is "it depends".

The two threads run in parallel (actually, physically in parallel if you have a multiprocessor/multicore box), so the results are non-deterministic.

Realistically, the main thread is more likely to complete first as it does nothing after starting the other thread. So no. But occassionally it would have worked - depending on how the OS/JVM schedule the two threads.

This is why multi-threaded programming is hard - you have to think about what happens when things happen in parallel like this.

dannyyatesa at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 11
> so now i have just a single question left in my mind.I have a single question left in my mind two. Why is this thing a thread at all? Why not just a method called in the same thread as the caller?
ejpa at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...
# 12
Because that thing just describes an example scenario. I have just simplify the thing. Just a construction nothing more. I haven't given the real code as it will be too big to post it here. Okay, guys we can end this discussion here.Thanks a lot.
mr47a at 2007-7-8 22:58:55 > top of Java-index,Core,Core APIs...