Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: Creating a thread. Unexpected output.

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Creating a thread. Unexpected output.

    Java Code:
    package Threads;
    
    public class Threads1 implements Runnable {
    	public void run(){
    		System.out.println("\n\t*\t*\t*\t*\t*\n");
    	}
    	public static void main(String[] args) {
    		System.out.println("I am going to show you five stars.\n");
    		new Thread(new Threads1()).start();
    		System.out.println("\n HAPPY NOW? Yayyyy!\n");
    	}
    }
    What I expected from this piece of code is:

    Java Code:
    I am going to show you five stars.
    
    	*	*	*	*	*
    
     HAPPY NOW? Yayyyy!
    But the output I got is:

    Java Code:
    I am going to show you five stars.
    
    
     HAPPY NOW? Yayyyy!
    
    
    	*	*	*	*	*
    Can anyone tell me why?
    Java Code:
    new Thread(new Threads1()).start();
    should execute the thread associated with the run() and then the control should return to the next statement in the calling thread (the thread from which the other thread (associated with run()) was called); isn't it?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Creating a thread. Unexpected output.

    Is there any particular reason you need to execute the code in a thread?
    I think that in order to ensure that the 'Happy Now' line doesn't execute until after the thread has started, you need to use a CountdownLatch.

    The fact that you're executing the * printing code in a thread suggests that it is an asynchronous operation, when in reality, what you want is things to be printed in order, so a thread doesn't seem appropriate there.
    I could be wrong, though.
    Last edited by sehudson; 03-18-2014 at 07:48 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Creating a thread. Unexpected output.

    execute the thread associated with the run() and then the control should return to the next statement
    The start() method tells the JVM to put the Thread in a queue where it will be executed some time in the future. The start() method immediately returns and execution continues.
    To make the execution of the statements be serial, use a standard call to the run() method.
    Zarah likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Creating a thread. Unexpected output.

    Hah. Actually, with threads the unexpected is the expected.

    Regards,
    Jim
    Norm likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Creating a thread. Unexpected output.

    The start() method tells the JVM to put the Thread in a queue where it will be executed some time in the future. The start() method immediately returns and execution continues.
    To make the execution of the statements be serial, use a standard call to the run() method.
    OK great, thank you.

    Hah. Actually, with threads the unexpected is the expected.
    Oh OK, I'll try to avoid using them.

  6. #6
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Creating a thread. Unexpected output.

    Is there any particular reason you need to execute the code in a thread?
    I think that in order to ensure that the 'Happy Now' line doesn't execute until after the thread has started, you need to use a CountdownLatch.

    The fact that you're executing the * printing code in a thread suggests that it is an asynchronous operation, when in reality, what you want is things to be printed in order, so a thread doesn't seem appropriate there.
    I could be wrong, though.
    This was just the simplest example; I was reading a tutorial on threads and trying it to see how things work. Thank you for answering.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Creating a thread. Unexpected output.

    Quote Originally Posted by Zarah View Post
    OK great, thank you.

    Oh OK, I'll try to avoid using them.
    Don't avoid using Threads because you can't line up the output. Threads are very important when you are writing user interfaces, client/server programs or general computation programs and lots more. Use Threads (or their brethren SwingWorkers, Future) when you need to, but don't use them "just because you can". Everybody can spawn a Thread, but proper multithreading is hard to master. Fortunately, the java.util.concurrent package makes it easy for us. Check out the links below for more concurrency goodies.

    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    java.util.concurrent (Java Platform SE 7 )
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Creating a thread. Unexpected output.

    Don't avoid using Threads because you can't line up the output. Threads are very important when you are writing user interfaces, client/server programs or general computation programs and lots more. Use Threads (or their brethren SwingWorkers, Future) when you need to, but don't use them "just because you can". Everybody can spawn a Thread, but proper multithreading is hard to master. Fortunately, the java.util.concurrent package makes it easy for us. Check out the links below for more concurrency goodies.

    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    java.util.concurrent (Java Platform SE 7 )
    Thank you. I am actually already following the first one of your links. I'll get to multithreading (I think multithreading is an advanced concept) if any of my future projects demands it, because I've learnt from experience that it is no use to learn something if you are not putting it into real, serious practice.
    Last edited by Zarah; 03-19-2014 at 09:33 AM.

Similar Threads

  1. Unexpected Output… Please help
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 11-23-2013, 07:03 PM
  2. Unexpected Output
    By ankiit in forum New To Java
    Replies: 20
    Last Post: 01-06-2012, 05:48 PM
  3. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 07:25 AM
  4. Replies: 3
    Last Post: 06-28-2009, 07:18 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
  •