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 Run a thread but do not want to wait to complete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Run a thread but do not want to wait to complete" Watch "Run a thread but do not want to wait to complete" New topic
Author

Run a thread but do not want to wait to complete

srinivas k.k
Greenhorn

Joined: Jan 31, 2003
Posts: 1
Hi ,
i will be creating the thread from a different Java class and once thread is created , thread would sleep for 2 mins and start its execution but the java class from where the thread is create should not wait for thread to complete and it has to continue executing its further logic.
How to achieve this?


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18714
    
    8

You achieve it by code just like what you posted. You replace this:



by the code you want to run. Didn't you try that?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You don't use the thread's run method to run it in parallel. You call its start method.

I think this tutorial should help you get started on using thread's in java: http://docs.oracle.com/javase/tutorial/essential/concurrency/


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Oh, I also see that what you call TestThread isn't a thread, it is a Runnable so it won't have a start method and would need to be passed to a thread to run the way you want. You should read the tutorial to learn how to use them properly.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18714
    
    8

Thanks for the correct answer, Steve. I was completely fooled by the runnable object being called "TestThread".
 
wood burning stoves
 
subject: Run a thread but do not want to wait to complete