Results 1 to 5 of 5
  1. #1
    nikhil_me is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Cool same code different outputs

    Greetings!!
    i am new to java so tried some codes but output is different when i run it different times. the code is
    class NewThread implements Runnable {
    String name;
    Thread t;
    NewThread(String threadname){
    name=threadname;
    t=new Thread(this,name);
    System.out.println("new thread:"+t);
    t.start();
    }

    public void run(){
    try{
    for(int i=5;i>0;i--){
    System.out.println(name+":"+i);
    Thread.sleep(1000);
    }
    }catch(InterruptedException e)
    {
    System.out.println(name+"interrupted");
    }
    }
    }



    class NewThread1{
    public static void main(String[] args) {

    NewThread a=new NewThread("one");
    NewThread b=new NewThread("two");
    NewThread c=new NewThread("three");
    try{
    Thread.sleep(1000);
    }catch(InterruptedException e){
    System.out.println("main interrupted");
    }



    }
    }
    and
    output 1)
    new thread:Thread[one,5,main]
    one:5
    new thread:Thread[two,5,main]
    new thread:Thread[three,5,main]
    two:5
    three:5
    one:4
    two:4
    three:4
    one:3
    two:3
    three:3
    one:2
    two:2
    three:2
    one:1
    two:1
    three:1

    Output 2)

    new thread:Thread[one,5,main]
    new thread:Thread[two,5,main]
    one:5
    two:5
    new thread:Thread[three,5,main]
    three:5
    one:4
    two:4
    three:4
    one:3
    two:3
    three:3
    one:2
    two:2
    three:2
    one:1
    two:1
    three:1
    Output 3

    new thread:Thread[one,5,main]
    one:5
    new thread:Thread[two,5,main]
    two:5
    new thread:Thread[three,5,main]
    three:5
    one:4
    two:4
    three:4
    one:3
    two:3
    three:3
    two:2
    one:2
    three:2
    two:1
    three:1
    one:1

    why are these 3 different sequences. shouldn't 3 be the only output given???
    well i guess this happens in every multithread program (may b one that includes sleep). is that true??
    thanxxx
    Last edited by nikhil_me; 01-19-2012 at 09:45 AM. Reason: addition

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: same code different outputs

    You have no great control over when the OS switches between threads.
    I'm surprised they switch as neatly as they do in your example.

  3. #3
    nikhil_me is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: same code different outputs

    thanx for your reply Tolls.
    what if i want a particular execution sequence of the threads????? will that be possible to get.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: same code different outputs

    Then don't use threads, because if it's sequential then threading is a bit pointless.

  5. #5
    nikhil_me is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Same inputs, different outputs.
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 12-15-2011, 07:36 PM
  2. Replies: 5
    Last Post: 11-20-2011, 04:44 AM
  3. I want to put text outputs in the GUI console.
    By illinit in forum New To Java
    Replies: 2
    Last Post: 10-19-2011, 11:21 PM
  4. Replies: 8
    Last Post: 09-12-2011, 01:43 PM
  5. Replies: 0
    Last Post: 07-22-2011, 12:31 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
  •