Help coderanch get a
new server
by contributing to the fundraiser
  • 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

JQPlus Thread Question

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Following is the question from JQPlus
Question ID :988383539812
Consider the following code:
public class Test extends Thread
{
static int x, y;
public synchronized void setX(int i) { x++; }
public synchronized void setY(int j) {y++; }
public void setXY(int i, int j) { setX(i); setY(j); }
public boolean testXY() { return x != y;}

public void run(){ setXY(); System.out.println(testXY()); } //line1
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
The above code will always print "false".

The anwser given is false means it will always prints TRUE.
But the above program will fail to compile because at line 1 method setXY() is called with no arguments and there is no such method.
Also if you call method with (1,1) or (1,2) it will always prints false.
I think this is a wrong question.
Paul please correct me if I am wrong.
Thanks,
Avinash
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Avinash Rai:
Hi All,
Following is the question from JQPlus
Question ID :988383539812
Consider the following code:
public class Test extends Thread
{
static int x, y;
public synchronized void setX(int i) { x++; }
public synchronized void setY(int j) {y++; }
public void setXY(int i, int j) { setX(i); setY(j); }
public boolean testXY() { return x != y;}

public void run(){ setXY(); System.out.println(testXY()); } //line1
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
The above code will always print "false".

The anwser given is false means it will always prints TRUE.
But the above program will fail to compile because at line 1 method setXY() is called with no arguments and there is no such method.
Also if you call method with (1,1) or (1,2) it will always prints false.
I think this is a wrong question.
Paul please correct me if I am wrong.
Thanks,
Avinash


Hello Avinash..
I just wanted to add my 2 cents...
Yeah, This code will not compile, and didnt compile for me either
But, there is few other things i found out.
1.Whatever combo of inputs you provide to the setXY(param1 param2) method, the output will be the same, coz the params are not taken into account, all the setter does is postincerement of the static variables.
2. If you look threadically i feel these threads are not contending for the locks at all..when you specify the params in the run method
Hope that helps
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avinash, Paul has to clarify this. This code doesn't compile and if we modify setXY(1,2) it will print always false.
Vanitha.
 
Enthuware Software Support
Posts: 4844
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It shows setXY(1, 2) in my copy so I think it was corrected.
This question already has a detailed explanation which you probably did not read I am copying it here...
You are creating 2 threads in main().
Consider this scenerio:
thread 1, calls setXY, in setXY it calls setX() (after getting the lock) and sets x to 1. Now setX ends and the lock is released. Before it could again get the lock to call setY(), the OS runs thread 2. The thread 2 again increments x to 2 and then it calls setY thereby incrementing y to 1. Now, it calls testXY and x is 2 and y is 1 so it will return true!
The point to note here is, calling two synchronized methods one after another from a non-synchronized method does not guarantee the order of updation of shared data.
HTH,
Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Avinash Rai
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul
In my copy it shows setXY().
Does it mean I have an old copy.
Please let me know how I will get the latest one.
Thanks.
Avinash
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul I bought in June . In my copy also it has setXY(). Do you have errata page?
Please let us know.
Vanitha.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
I am getting the x and y values as 2,2.That measn taht my OS is scheduling or.......
If u can explain it will b great.
Thanx
Rajani
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic