• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQ+question on Threads.

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a JQ+question
Consider the following code:
public class Test extends Thread
{
static int x, y;
public synchronized void run(){ for(; { x++; y++; System.out.println(x+" "+y);} }
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
What will the above code print?
Ans: You cannot say anything about the values.
------
This is the answer given by JQ plus.
I think the answer should be:
x and y should increment by 1 on each line
Why i think this is right because, any thread that goes into the
synchronised method will definitely increment x and y by 1 before it prints anything.
It is possible that the 2nd thread enters the synchronized method before the first thread exits out of it but , still since the variables x and y are static and they are incremented inside the infinite for loop
they should increment by 1 on each line.
Any comments on this??
Thanks in advance
Padmini
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini
I agree with what you are saying, but what other answers did they give you as possibilities?
Dave
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And more importantly, is a new Java operator?
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got to pay a little more attention here.
Look at the main method :
new Test().start();
new Test().start();
There are two object of class Test! synchronized will work only if both the threads are created out of one Test object:
Test t = new Test();
new Thread(t).start();
new Thread(t).start();

This is already given in the explanation provided.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul
I didn't even notice that when I tested it. So they could change the numbers after one changes them but before it prints them. OK, got it.
By the way you'll be getting my order tomorrow for the JQ+.
Scott, it took me a minute to realize what had happened. When Pamini posted the code that is an empty for loop so when she put the ; and ) next to each other to close it the ubb made it a
I guess that's a good examle of why we should use the code tag.

Dave
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,
Well, somehow i misunderstood the matter. Well now I understand.
Maybe, this is because, there could be a possibility like this:
The first thread enters run and it increment s x++.//here x is 1
while it is doing so, maybe the second thread also enters run and does x++ and y++ .//here x is 2 and y is 1
immediately, after this , the first thread which has finished incrementing x++ now starts incrementing y++// here y is 2.
So, overall, nothing can be said about the values. I hope i am right.
------------------------------
Actually Scott, When I posted the code for an empty for loop like this for ( ; ; ) , so when I put the ; and ) next to each other to0 close that it somehow made up the symbol becuase of that . Next time i will make it a point to see that the ; ) are not so close.
Padmini

[This message has been edited by padmini Babu (edited June 21, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic