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]

# 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 >

# 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.
# 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.
# 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 >

# 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.