Please help with threads


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Please help with threads

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    Post Please help with threads

    Hi guys, I am really really confused with thread and I would really appreciate it if you can help me. Here are my problems:

    There are 2 ways to create thread - one extending Thread class and another one is implementing the Runnable interface.

    Why many thread does the program has if you don't create any thread using these methods? I've read it from some website that when you run a program JVM auto creates a thread. So, even if you run a very simple program like HelloWorld, it will have at least have one thread? If it is true, does it counted as a thread is you use activeCount() method to get the number of active threads? In one of the program I wrote, the activeCount() returns 7threads even though I created only 2.

    You need an object to create a thread right? Why do you need to create an object of the current class to run? I mean in extending methods
    Code:
    public class myThread extends Thread
    {
    	pubilc void run()
    	{
    		System.out.println ("thread running");
    	}
    	
    	public static void main (String[] args)
    	{
    		//why this line works 
    		myThread yes = new myThread();
    		yes.start()
    		
    		//and why this doesn't?
    		Thread no = new Thread;		
    		no.start();
    	}
    }
    Also in Runnable interface
    Code:
    public class myThread implements Runnable
    {
    	pubilc void run()
    	{
    		System.out.println ("thread running");
    	}
    	
    	public static void main (String[] args)
    	{
    		//what is the difference between this
    		myThread yes = new myThread(); 
    		Thread t = new Thread (yes);
    		t.start()
    		
    		//and this? 'this' refer to the object itself right?
    		//In this case I haven't create any object of myThread class how come it works? 
    		Thread t = new Thread(this);		
    		t.start();
    	}
    }
    Thank you in advance.

  2. #2
    Join Date
    Mar 2006
    Posts
    4
    please, anyone?

  3. #3
    Join Date
    Mar 2004
    Posts
    635
    //and why this doesn't?
    Thread no = new Thread;
    no.start();

    Cause you forgot the parenthesis on the "new Thread()" part.


    //In this case I haven't create any object of myThread class how come it works?
    Thread t = new Thread(this);

    That actually won't work because you can't use "this" inside a static method, such as main(). Because an object is not created when you call a static method, "this" has nothing to refer to.

    Now you could do this instead:
    Code:
    public class MyThread implements Runnable
    {
    
        private Thread thread = null;
        
        public MyThread()
        {
            thread = new Thread(this);
        }
    
        public void run()
        {
            System.out.println ("thread running");
        }
        
        public static void main (String[] args)
        {
            MyThread thread = new MyThread();
        }
    }
    This works because in your main() method, you have created an object and "this" will refer to that instance of the object.

    I hope this makes sense, I'm not to good at explaining "this" stuff.

  4. #4
    Join Date
    Mar 2006
    Posts
    4
    Thanks for answering my questions Phaelax. I just compiled the new code with parenthesis and it compiled but it did not create any thread. It did not print out "thread running".

    Code:
    public class myThread extends Thread
    {
    	public void run()
    	{
    		System.out.println ("thread running");
    	}
    
    	public static void main (String[] args)
    	{
    		Thread no = new Thread();
    		no.start();
    	}
    }
    And

    Code:
    public class myThread implements Runnable
    {
        private Thread thread = null;
    
        public myThread()
        {
            thread = new Thread(this);
        }
    
        public void run()
        {
            System.out.println ("thread running");
        }
    
        public static void main (String[] args)
        {
            myThread thread = new myThread();
            thread.start();
        }
    }
    generated error that says
    "cannot resolve symbol
    symbol : method start ()
    location: class myThread
    thread.start();"

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    First example prints nothing because that instance of run() is never called. You've made a Thread object, not a MyThread object. If you really wanted to do it that way, it'd have to be like this:
    Code:
    Thread no = new Thread(new MyThread());
    start() doesn't exist in the second class because we've emplemented the interface, which only defines methods. Runnable only defines run(), which we have but no where in the class is there a start() method. In the first example, start() exists because it belongs to the super class "Thread" that we've extended and therefore it gets called by default since it hasn't been overridden.

    And its good coding practice to start capitalizing your class names. MyThread instead of myThread

  6. #6
    Join Date
    Mar 2006
    Posts
    4
    Thanks for the reply guys, I think I understand how thread works now.

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    You can also do something like:
    Code:
    
    new Thread(new Runnable() {
        public void run() {
            System.out.println("run()");
        }
    }).start();

Similar Threads

  1. access violation using threads in Applet
    By martinfr79 in forum Java
    Replies: 1
    Last Post: 05-15-2005, 10:18 AM
  2. Heavy math routines and separate threads?
    By rechmbrs in forum Java
    Replies: 13
    Last Post: 03-22-2005, 02:14 PM
  3. Threads
    By Jonas in forum .NET
    Replies: 2
    Last Post: 02-16-2002, 10:51 AM
  4. Replies: 1
    Last Post: 12-10-2000, 02:59 PM
  5. Replies: 0
    Last Post: 09-26-2000, 09:52 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