Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Same name displayed twice!

    please check my code :
    Java Code:
    class Race {
    	static String name;
    	static Thread t;
    	
    	public static class Expression implements Runnable { 
    		
    		Expression(String threadname){
    			name = threadname;
    			t = new Thread(this, name);
    		}
    		
    		public void run(){
    			
    			try{
    			
    			for (int i=0; i<5; i++) {
    				System.out.println("Thread is " + name);
    				Thread.sleep(1000);
    				System.out.println("Slept for 1000");
    				
    			}
    			}catch (InterruptedException e) {
    				System.out.println("Interrupted exception");
    			}
    			
    				
    			
    		}
    	}
    	
    	public static void main(String args[]){
    		
    		Expression ob1 = new Expression("Race1");
    		Expression ob2 = new Expression("Race2");
    		
    		try{
    			ob1.run();
    			Thread.sleep(1000);
    			System.out.println("slept for 1000 on ob1");
    			ob2.run();
    			Thread.sleep(1000);
    			System.out.println("slept for 1000 on ob2");
    			
    		}
    		catch (InterruptedException e) {
    			System.out.println("Interrupted exception");
    		}
    	}
    }

    The output is :

    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    slept for 1000 on ob1
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    Thread is Race2
    Slept for 1000
    slept for 1000 on ob2

    Instead of printing Race1 it is printing Race2 in the first iteration.
    Why is it so?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Same name displayed twice!

    Java Code:
            Expression ob1 = new Expression("Race1");
            Expression ob2 = new Expression("Race2");
    With these two lines you are first setting 'name' to "Race1" then setting it to "Race2".
    This all happens before you even launch the threads, so 'name' is always going to be "Race2" during the thread execution.

    Possibly 'name' should be an attribute of Expression and not Race?

  3. #3
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Act on a not displayed applet
    By cachente in forum New To Java
    Replies: 4
    Last Post: 09-09-2011, 02:37 PM
  2. TextField not displayed
    By weezy2894 in forum Java Applets
    Replies: 6
    Last Post: 04-18-2011, 02:37 AM
  3. output is not displayed
    By meth in forum New To Java
    Replies: 0
    Last Post: 01-07-2011, 05:44 AM
  4. Tooltip not getting displayed...
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-31-2008, 10:00 AM
  5. displayed the rownumbers
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:30 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
  •