File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 3582

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.
I agree. Here's the link:
subject: Whether using Threads join correctly or not
Similar Threads
generics code mixed with legacy collection
Regarding Join method in Threads
[ A good finding ] - User & Deamon Threads
Evidence 4 Main Thread dead after main method exits