Is there any way to start thread with out extending Thread class and implementing Run


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Is there any way to start thread with out extending Thread class and implementing Run

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Posts
    2

    Is there any way to start thread with out extending Thread class and implementing Run

    Is there any way to start a thread without extending class Thread and implementing Runnable interface.

    For eg. Consider the code below:

    class Daemon
    {
    public static void main(String arg[])
    {
    String s="thead";
    Thread t = new Thread();
    t.setName(s);
    //t.start();
    }
    public void run()
    {
    System.out.println("in run of main");
    System.out.println("t thread exiting");
    }
    }

    The code above neither extends Thread class or nor implements Runnable interface, even then i am able to create a thread but when i call start() method
    the run() is not getting called as i haven't provided the target.
    Is there any way to get this work without using Runnable or Extending Thread?

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    not particularly. with
    Thread t = new Thread();
    t.setName(s);
    //t.start();
    you create a new thread with an empty run() body and that's not your class.
    what's the reason why you don't want to implement Runnable in your class since you just have to add implements Runnable in your class definition?

  3. #3
    Join Date
    Apr 2006
    Posts
    2
    I just want to know is there any other way we can do this.

    Any way thanks for your reply and would be great if you can tell is there any other way?

  4. #4
    Join Date
    Oct 2005
    Posts
    107
    You could do it with a Anonymous Runnable Class...
    Code:
    Thread t = new Thread(new Runnable(){
    public void run(){
    doWork();
    }
    });
    t.start();
    or if you wanted to run a seperate process you could do...
    Code:
    Runtime runtime = Runtime.getRuntime();
    String [] cmdArgs = {"java.exe", mainClass};
    Process p = runtime.exec(cmdArgs);
    //p.waitFor()
    Why do you not want to implement runnable? It's only one method.

  5. #5
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    Thread t = new Thread(new Runnable(){
    public void run(){
    doWork();
    }
    });
    t.start();
    jep, that's the only way to do it. there is no other posibillity, since you have to use a class that implements runnable. making a anonymous class or any other class which implements runnable seems to to be the only posibillity.

Similar Threads

  1. Please help with threads
    By cookie in forum Java
    Replies: 6
    Last Post: 03-15-2006, 08:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center