Results 1 to 8 of 8
- 03-18-2014, 08:25 PM #1
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
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"); } }
Java Code:I am going to show you five stars. * * * * * HAPPY NOW? Yayyyy!
Java Code:I am going to show you five stars. HAPPY NOW? Yayyyy! * * * * *
Java Code:new Thread(new Threads1()).start();
- 03-18-2014, 08:43 PM #2
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 08:48 PM.
- 03-18-2014, 08:56 PM #3
Re: Creating a thread. Unexpected output.
execute the thread associated with the run() and then the control should return to the next statement
To make the execution of the statements be serial, use a standard call to the run() method.If you don't understand my response, don't ignore it, ask a question.
- 03-18-2014, 09:57 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Creating a thread. Unexpected output.
Hah. Actually, with threads the unexpected is the expected.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-19-2014, 08:44 AM #5
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
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.
Hah. Actually, with threads the unexpected is the expected.
- 03-19-2014, 08:46 AM #6
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
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.
- 03-19-2014, 09:19 AM #7
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 )"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 03-19-2014, 09:59 AM #8
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
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 )Last edited by Zarah; 03-19-2014 at 10:33 AM.
Similar Threads
-
Unexpected Output… Please help
By Nazneen Ali in forum New To JavaReplies: 7Last Post: 11-23-2013, 08:03 PM -
Unexpected Output
By ankiit in forum New To JavaReplies: 20Last Post: 01-06-2012, 06:48 PM -
A basic method with an unexpected output
By JONCOM in forum New To JavaReplies: 6Last Post: 02-01-2011, 08:25 AM -
Unexpected behavior Thread.sleep() is used within CardLayout
By mityay in forum AWT / SwingReplies: 3Last Post: 06-28-2009, 08:18 PM
Bookmarks