• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run a thread but do not want to wait to complete

 
srinivas k.k
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20724
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20724
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the correct answer, Steve. I was completely fooled by the runnable object being called "TestThread".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic