• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Codes in the current thread execute first before the codes in Thread.run()

 
Karl San Gabriel
Greenhorn
Posts: 4
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karl San Gabriel
Greenhorn
Posts: 4
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic