Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: Thread's start happens late

  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Thread's start happens late

    I have a class that implements Runnable:

    Java Code:
    public class ContinuousClass implements Runnable {
    
        private Printer<ICartridge> printer = new Printer<>(true, "IPrinter", ColorCartridge.RED);
    
        @Override
        public void run() {
    
            for (int i = 0; i < 100; i++) {
                printer.printUsingCartridge(ColorCartridge.RED, "In Thread " + i);
            }
    
        }
    }
    On the main Class I have this main method:

    Java Code:
        public static void main(String[] args) {
    
            ContinuousClass cc = new ContinuousClass();
            Thread thread = new Thread(cc);
            thread.start();
    
            for (int i = 0; i < 100; i++) {
                System.out.println("Main Thread " + i);
            }
    }
    The output is:
    Java Code:
    Main Thread 0
    Main Thread 1
    Main Thread 2
    .
    .
    Main Thread 98
    Main Thread 99
    In Thread 0
    In Thread 1
    In Thread 2
    In Thread 3
    .
    .
    In Thread 99
    While the output of the tutorial I've followed was this:

    Thread's start happens late-g.png

    The question is why the resulting pattern is so different and if I just run the program several times there might be "In Thread .."s before/between "Main Thread .."s; But never interleave each other like the image.
    Even thought the thread.start() was called before the main method's for loop, Why it is run after that in most of the time?

    Thanks
    Last edited by ghandil; 05-04-2016 at 12:01 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Thread's start happens late

    That is the nature of the way threads work. Their asynchronous behavior can vary between runs. It takes some initial time
    to start the thread. And then you call another routine to print something which may also take some time. By that time, the
    simple loop in main has finished. If you wan to see it behave differently, place a Thread.sleep() statement inside each loop
    with different times.

    BTW, any good tutorial on threads should tell you that the output may be different than that shown.

    Regards,
    Jim
    Last edited by jim829; 05-04-2016 at 02:28 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Thread's start happens late

    The only way (and I exclude pure luck as the odds are tiny) for that output is if the threads are handing off to each other.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Thread's start happens late

    What happens if you change the code to System.out.println( ... ) in your ContinuousClass? (exactly as in the main( ... ) method).

    kind regards.

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Thread's start happens late

    Can you provide a link to the tutorial?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Thread's start happens late

    Quote Originally Posted by jim829 View Post
    That is the nature of the way threads work. Their asynchronous behavior can vary between runs. It takes some initial time
    to start the thread. And then you call another routine to print something which may also take some time. By that time, the
    simple loop in main has finished. If you wan to see it behave differently, place a Thread.sleep() statement inside each loop
    with different times.

    BTW, any good tutorial on threads should tell you that the output may be different than that shown.

    Regards,
    Jim
    Thanks.
    I wonder how the hardware of host machine could exactly shape the pattern? Because on my system it follows almost the same pattern most of the time, whereas apparently on the instructor's system the followd pattern is differrent but also almost consistent.

    Quote Originally Posted by Tolls View Post
    The only way (and I exclude pure luck as the odds are tiny) for that output is if the threads are handing off to each other.
    Thanks you, interesting.

    Quote Originally Posted by JosAH View Post
    What happens if you change the code to System.out.println( ... ) in your ContinuousClass? (exactly as in the main( ... ) method).

    kind regards.

    Jos
    Actually, there's only a System. out. println (... ) as an active statement inside that method, the reason it's this way is that whole code was followed as the continuous workflow of a tutorial.

    Quote Originally Posted by jim829 View Post
    Can you provide a link to the tutorial?

    Regards,
    Jim
    It's a PluralSitght video course here. The last section is Threading.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Thread's start happens late

    Quote Originally Posted by ghandil View Post
    It's a PluralSitght video course here. The last section is Threading.
    I can only assume that the code that produced the output you expected has some extra bits, but since it's a video tutorial and not text I won't be able to see exactly what.
    ghandil likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Thread's start happens late

    Wow! This site is rather pricey (300 USD or 500 USD per year). And I didn't do the free trial because I
    don't need another source of spam. So I can't comment on the quality of their courses. But I suspect
    you can do as well for significantly less (perhaps free). Since you are getting access one way or the other
    did you ask the author of the course any questions?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Thread's start happens late

    Quote Originally Posted by jim829 View Post
    did you ask the author of the course any questions?
    No, I preferred to ask here.
    (And Of course, I'm a mobile spam target :D)

Similar Threads

  1. Cannot start new thread
    By Query in forum Forum Lobby
    Replies: 2
    Last Post: 08-20-2013, 11:52 AM
  2. Replies: 3
    Last Post: 04-16-2013, 02:04 AM
  3. Thread.start()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 08:51 AM
  4. Procedure to start Thread?
    By greatmajestics in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2010, 05:05 PM
  5. My thread will not start
    By markyoung1984 in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-03-2008, 06:32 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
  •