my dog learned polymorphism
The moose likes Threads and Synchronization and the fly likes Whether using Threads join correctly or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Whether using Threads join correctly or not" Watch "Whether using Threads join correctly or not" New topic

Whether using Threads join correctly or not

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I Want to execute a Certain Task to take only 1000 MS , if it exceeds , i dont want to continue with the task ,
i have used join for this .

Please tell me and guide me if this is correct or not

import java.util.List;


Save India From Corruption - Anna Hazare.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4200

No, this will not work, because you are waiting on the mainthread to finish, not the childthread. Changing it to to childthread.join(1000) should work. You don't need mainthread.

There are much much better ways to do this though. Take a look at the invokeAll() method of the ExecutorService class.

You are also using raw types. Don't use raw types.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Whether using Threads join correctly or not
It's not a secret anymore!