Results 1 to 6 of 6
  1. #1
    Javasubbu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default method run() problem

    Hi,

    The compilation of below code does not give me an error. But it does not display what is inside method run(). Any thoughts?
    the method start() will call run() for sure, but where am I missing?

    Java Code:
    public class Thrcomtestm implements Runnable{
    	public static void main(String args[]){
    	Thread t1 = new Thread("One");
    	Thread t2 = new Thread("Two");
    	Thread t3 = new Thread("Three");
    	String name;
    	t1.start();
    	t2.start();
    	t3.start();
    	System.out.println("I am here");
    	}
    public void run(){
    	System.out.println("I am here too");
    	String name = Thread.currentThread().getName();
    	System.out.println("The name of Current Thread is" + 
    
    name);
    	}	
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: method run() problem

    Where did you 'link' your Runnable object to those Threads?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Javasubbu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

    Default Re: method run() problem

    May be I am wrong, but will not the start () of instantiated variables t1, t2 and t3 will kick the method run()?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: method run() problem

    Quote Originally Posted by Javasubbu View Post
    May be I am wrong, but will not the start () of instantiated variables t1, t2 and t3 will kick the method run()?
    Yep, you're wrong ;-) You have to pass the Runnable to the Thread when your create the Thread with 'new' ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    24
    Rep Power
    0

    Default Re: method run() problem

    Pass runnable to thread. it executes the run method.

    new Thread(new Thrcomtestm()).start(); will execute the run method

  6. #6
    Javasubbu is offline Member
    Join Date
    Oct 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2011, 07:16 AM
  2. problem with method
    By lyes_djaml in forum New To Java
    Replies: 6
    Last Post: 01-21-2011, 11:04 PM
  3. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  4. Method problem
    By Jeff6461 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:48 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •