aspose file tools
The moose likes Threads and Synchronization and the fly likes Codes in the current thread execute first before the codes in Thread.run() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Codes in the current thread execute first before the codes in Thread.run() " Watch "Codes in the current thread execute first before the codes in Thread.run() " New topic
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
    
  11

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Codes in the current thread execute first before the codes in Thread.run()
 
Similar Threads
doubt on a thread program
Threading related question from khalid mughal's book
Overloading and Overriding
A hard question
Is it a safe assumption when it comes to THREADS?