Results 1 to 4 of 4
  1. #1
    Hemant16 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Question Need a reason for output

    package com.demo;

    public class Sample implements Runnable {

    public void run(){
    for(int i=0;i<5;i++){
    System.out.println("i=>"+i);
    }
    }
    public static void main(String args[]){
    Sample sample = new Sample();
    //sample.start();
    Thread t = new Thread(sample);
    t.start();
    System.out.println("Ends Here");

    }

    }
    output is :
    Ends Here
    0
    1
    2
    3
    4
    Need a reason on above output why not first thread will start prints 0 to 4 n then "Ends Here" ??

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,762
    Rep Power
    25

    Default

    You are asking about the scheduling of when a thread runs.
    You expect the current thread to stop when start is called on another thread.
    Why should the current thread stop executing?
    If there are two cpu/cores perhaps both threads could run in parallel.

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

    Default

    You could've gotten this:

    Java Code:
    0
    1
    2
    Ends Here
    3
    4
    kind regards,

    Jos

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Hemant16 View Post
    Need a reason on above output why not first thread will start prints 0 to 4 n then "Ends Here" ??

    as norm already mentioned the purpose of threads is to run in parallel. so if you want to run your code sequentially, don't use threads or use the join() method from the Thread class, example

    Java Code:
        public static void main(String args[]) {
            Sample sample = new Sample();
            // sample.start();
            Thread t = new Thread(sample);
            t.start();
            try {
                t.join();
            } catch (InterruptedException ex) {
            }
            System.out.println("Ends Here");
    
        }

    will always produce

    i=>0
    i=>1
    i=>2
    i=>3
    i=>4
    Ends Here

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2010, 03:59 AM
  2. Replies: 4
    Last Post: 02-17-2010, 11:17 PM
  3. [SOLVED] Can't pinpoint reason for this error..
    By Reiyn in forum New To Java
    Replies: 6
    Last Post: 03-30-2009, 07:39 PM
  4. Getting errors for some reason
    By Swarvy in forum New To Java
    Replies: 7
    Last Post: 09-30-2008, 03:45 PM

Tags for this Thread

Posting Permissions

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