• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method who takes much time to execute  RSS feed

 
Rodrigo Martinez
Greenhorn
Posts: 4
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, it's my first time asking in the forum, and this question it's about something that had been bothering me in the last time. It´s about what Java do when we call a method who modifies the value of a previously defined variable. Then if i call a getter method to get the value of that variable, what happens if the first method takes to much time making its calculations?. To prove this i created this little example:

In the result, the first value printed it's "A", and the last value it's "B", as i was thinking before my doubts arrised. Maybe it's something too basic, but can you give me some advice? or tell me where i can read about this kind questions?

Thanks in advance,

Rodrigo.-
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your question - everything is done sequentially. So the second getValue() can't run until after slowApp() returns. Behind the scenes in the processor there's what's called a program counter and it has the address of the next instruction. It can only be one place at a time! The problem you're thinking of might arise in multi-threaded code. I have limited experience with those in Java. But your example isn't multi-threaded.

 
Rodrigo Martinez
Greenhorn
Posts: 4
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:If I understand your question - everything is done sequentially. So the second getValue() can't run until after slowApp() returns. Behind the scenes in the processor there's what's called a program counter and it has the address of the next instruction. It can only be one place at a time! The problem you're thinking of might arise in multi-threaded code. I have limited experience with those in Java. But your example isn't multi-threaded.



OK, now i get the idea, i wasn't considering that sequentiality.


Thanks Guillermo
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make the loop in slowApp() and endless loop, and you'll see that nothing else ever happens... Oddly I think this question has popped into everybody's head at some early point.

There are languages that aren't so sequential - some languages used with motor controllers, and I think one database language that I used a long time ago. I hate them. They're really rare fortunately.
 
Rodrigo Martinez
Greenhorn
Posts: 4
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help, i tried that and you're right, the method never returns, so the last value never gets printed.

Regards
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!