Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic idea regarding Thread synchronization

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please look at the following code:

class CalculatorThread implements Runnable {
public void run() {
try {
String threadName = Thread.currentThread().getName();
if ( threadName.equals("adderThread") ) {
printMessage("Adder");
}
else if( threadName.equals("subtractorThread") ) {
printMessage("Subtractor");
}
}catch(Exception e) {
e.printStackTrace();
}
}
public void printMessage(String component) {
System.out.println(component+" in Action");
}
}
public class ThreadDemonstrator {
public ThreadDemonstrator() {
Thread threadOne = new Thread(new CalculatorThread(),"adderThread");
Thread threadTwo = new Thread(new CalculatorThread(),"subtractorThread");
threadOne.start();
threadTwo.start();
}
public static void main(String[] args) {
ThreadDemonstrator td = new ThreadDemonstrator();
}
}

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.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
errr ...

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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want those two things to happen sequentially, why are you using two threads in the first place?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
If you want those two things to happen sequentially, why are you using two threads in the first place?

good point,....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic