File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Basic idea regarding Thread synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Basic idea regarding Thread synchronization" Watch "Basic idea regarding Thread synchronization" New topic
Author

Basic idea regarding Thread synchronization

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
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

Joined: Oct 26, 2003
Posts: 407
    
    1

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.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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."
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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,....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic idea regarding Thread synchronization
 
Similar Threads
Why doesn't this code work in Netbeans 7.0?
Use of Private Constructor in Thread Example
about notify() and wait()
The static synchronized methods of the same class always block each other as only one lock per class
Threads/Looping: How Can This Be?