Run thread

Hello experts!

How can i create a thread which actually runs a class ?
i mean is there any sample code as a pattern  that i have to follow ??

Thanks in advance!
perdoname_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmlonCommented:
The "standard" reference to begin with would be the chapter on concurrency from the Java Tutorial:

http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

0
summerianCommented:
1. Implement Runnable interface
2. Pass it to a new Thread in constructor

If you need to manage your thread (stop it from outside the thread), follow this pattern:

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
public class MyRunnable implements Runnable{
   public void run(){
      //thread logic here, some endless loop probably
   }
}
 
public class Main{
 
   public static void main(String[] args){
      //run MyRunnable as separate thread
      new Thread(new MyRunnable()).start();
 
      //some other logic here
   }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.