[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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!
0
perdoname_
Asked:
perdoname_
1 Solution
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now