This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Surely you should at least start threadOne after threadTwo if you want threadTwo to complete first and the issue with the join. Placing the join on the second thread will then do what you request (i.e. start thread two , pause main thread, start thread one on thread two completion), however reading between the lines I suspect you don't want the main thread to wait i.e. start thread two, pause thread one, continue with main thread , wake up thread one on thread two completion in which case if you want to use a join you need the bit that starts the thread(s) in its own thread or do some thread communication between one and two e.g. wait / notify mechanism, i.e. no join. Can't see why pausing the main thread is a bad thing in this example.
A little more information on what you intend the code to do in terms of the three threads you have would be helpful e.g. at this point I would like thread one to block and main thread and threa two to continue etc and exactly which thread you placed the 'failed' join on .. i.e. comment out the 'broken' code.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Originally posted by dinesh Venkatesan: Hi All, Now i want to make sure that threadOne is not starting its execution untill threadTwo is completed its execution. calling the method join() only makes the main thread to wait.
Thanks in Advance!!! dinesh.
I think Chris's motto, which I find extremely funny, would be a good analogy - you could have a weasel thread which runs under everything, and I prefer this approach but what you get is a lot of processor time devoted to thread management. It is better, unless there is a clear, compelling reason, to use the approach Chris suggests.
There are some little buttons underneath where you write the post. One of them is called "CODE" - activate that button and write your code between the tags with the square brackets that say code.
Also, of great help would be to know if this is a learning problem, or do you have a real-world matter to approach ?
Also, I do not like to do anything more than absolutely necessary in constructors. Calling a method after construction seems to me to be a more object oriented approach. It separates the operation of the class from the construction of the class. Stay with whichever approach makes the most sense to you.
[ January 04, 2007: Message edited by: Nicholas Jordan ]
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
The simple answer is that if you want thread 1 to wait for thread 2, then write code in it to do so. Because the two threads are intended to do different things, instead of using a single thread class, you need two, one for each type of the thread. Your Thread1 class could accept an instance of Thread2 as a constructor argument, store it in a member variable, and then in Thread1's run() method, call join() on that member.
If you want those two things to happen sequentially, why are you using two threads in the first place?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Sep 17, 2006
Originally posted by Ilja Preuss: If you want those two things to happen sequentially, why are you using two threads in the first place?