Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

thread

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A extends Thread{
static int x = 0;
public void Runnable(){
for(int i = 0;i <100; i++)
System.out.println(++x);
}
public static void main(String ar[]){
A a = new A();
a.start();
for(int i=0; i<100; i++)
System.out.println(--x);
}
}
i think the answer should be 0,0
but will hapen if it is x++, --x it's bit complex for me
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hasitha Randika:



Hi Hasitha,

First, start to get used to using code tags when posting code snippets. It makes it easier for the rest of us 100,000,000+ people reading your post to understand .

To answer your question, since there are threads involved modifying a common static variable, the output cannot be predicted. There is no control (i.e. synchronized) on the thread.

I think you may be more confused on the pre/post incrementer/decrementer.
That being the case, here's an example:

result:
1
x is incremented before the println().

result:
0
x is incremented after the println().

Beware of the post incrementer/decrementer though. It's interpretation is subjective. For example:

could be interpreted as increment x after the x++ > 0 half of the conditional -or- increment x twice after evaluating the entire conditional.

Hope this helps.
Aloha,
Doug
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Doug said is correct, there is no way to tell which thread will execute when, so there is no way to predict the exact output. However, the code you have provided never launches a second thread so the output will always be -100. (Yeah, I ran it. )
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program didnt start new Thread because of this line:

Its the run method which got invoked when you start a new Thread.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic