Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

static variable remains unchanged

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi everyone , this question is from www.javacertifications.com . My question is shouldn't sName change in the run method to become good0123 ??
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hint: did you print the result before or after the other thread changed the static variable?

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in this case the output is
Ankur
good 0 1 2 3
why?
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
same hint
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still no idea.....the name is printed only when the nameTest method completes.....in the first case a new thread runs simultaneously with the main thread.....same happens in the second case.....how does the output change by just adding one more line below start();
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:still no idea.....the name is printed only when the nameTest method completes.....in the first case a new thread runs simultaneously with the main thread.....same happens in the second case.....how does the output change by just adding one more line below start();



I'll take a wild guess . Cause it has to wait for start to begin running in order to print Ankur (before method returns). But I don't get it I mean no guarantees, right?
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Ali wrote:I'll take a wild guess . Cause it has to wait for start to begin running in order to print Ankur (before method returns). But I don't get it I mean no guarantees, right?



Basically, you have a race condition. If the main thread prints the static variable before the newly created thread can get to it, you will have the original value. If the newly created thread gets to it first, you will have the new value.

In the original example, the main thread gets to it first -- although this too, is not guarranteed to always be so.

In the second example, the main thread now has to print out "Ankur". This has two affects. It has to print out "Ankur". And the main thread has now been slowed down enough, by printing out "Ankur", that the newly created thread can get to the static variable first.... but as before, this is not guarranteed to always be so.

Henry
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if in the exam there is a question to determine the output...what should the answer be in both the cases? cannot be predicted? .......
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I refresh this thread as it lacks final answer. Original options are:

A)good
B)good idea
C)good idea good idea
D)good 0 good 0 1

but the correct one is "The output is unpredictable". Right?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in this question there is no option saying "result is not predictable", and "good" is the only answer that may be a possible output so in this case I think 1 is correct answer.
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

Actually, the changes aren't reflected to the variable, the rule is same with local variable..
in this code below, i declare and define value of local variable named a.., after execute nameTest method, any changes happened to a, won't be reflected to a itself..


Result : AAAAAA
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, if we want to change the static variable value of sName..
we can do it as bellow..


Result : good idea
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oopss... Sorry i've a doubt here..


Why the value is different if i use run mode and debug mode..

if i run that code in Netbeans, the result is :


Ankur
good



but, if i debug that code in Netbeans with the line breakpoints as mentioned above, the result is :


Ankur
good 0 1 2 3



of course if i run that code through command prompt, the result is :


Ankur
good



Could you describe why?..
Thanks..
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:in this question there is no option saying "result is not predictable", and "good" is the only answer that may be a possible output so in this case I think 1 is correct answer.



What I meant is the question has no correct answer.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why the value is different if i use run mode and debug mode..

if i run that code in Netbeans, the result is :


Ankur
good



but, if i debug that code in Netbeans with the line breakpoints as mentioned above, the result is :


Ankur
good 0 1 2 3



of course if i run that code through command prompt, the result is :


Ankur
good



Could you describe why the result different while i use debug mode and while i use Run mode in Netbeans?..

Please tell me why it occurs..
Thanks..
 
Are we home yet? Wait, did we forget the tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic