| Author |
Codes in the current thread execute first before the codes in Thread.run()
|
Karl San Gabriel
Greenhorn
Joined: May 19, 2010
Posts: 4
|
|
Hello there!
This is not my first time visiting this helpful site. Today I have decided to participate.
I have a question though. I have the following code:
Executing this code always displays lines 1, 3, and 5 first before showing the other values.
I do not know what explanation is for this behavior and I couldn't find anything about this on the Internet.
I'd love to hear your thoughts about this.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
I don't think that you can predict which one will go first. You may have different results, depending on the load on your machine for example.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Karl San Gabriel
Greenhorn
Joined: May 19, 2010
Posts: 4
|
|
Christophe Verré wrote:I don't think that you can predict which one will go first. You may have different results, depending on the load on your machine for example.
Hi Christophe,
I think you are right. That is also my expectation about the Threads. I modified the codes a bit to put in some "load" before the execution of println()'s in the current thread.
Thank you for the help.
Karl
|
 |
Richa Khan
Greenhorn
Joined: Apr 12, 2011
Posts: 1
|
|
Hey for the same program on running several times, I am getting only these 3 solutions. Why so?
1
3
5
4
2
1
3
5
2
4
1
3
2
5
4
Thanks.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Apparently your machine likes it that way You can't predict the ordering. Maybe it's because of the JVM you're using, or your cpu, or the load of the cpu. You can't say. I'm getting different results.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
 |
|
|
subject: Codes in the current thread execute first before the codes in Thread.run()
|
|
|