• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

question about thread

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

can you help me answering the following question

1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }

Under which condition will check return true when called from a different class?

A. check can never return true.
B. check can return true when setXY is called by multiple threads.
C. check can return true when multiple threads call setX and setY separately.
D. check can return true only if SyncTest is changed to allow x and y to be set
separately.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
another Q
If a class is compiled with assertions enabled using JDK 1.4, which of the following will skip the
execution of the assert statements at runtime?

A : by running it using jdk1.3
B: by disabling assertion at runtme by using approprite command line switch
C: none of these

i'm saying the correct answer is c because assertion is disabled by default in runtime in jdk1.4
but the simulator say it is b
who is right
[ March 15, 2006: Message edited by: mohamed ewis ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mohamed,

here are my thoughts about the questions:

first question:
A) is clearly "no". example:
assume x and y are zero. one thread (say "t1") executes setXY(5). after it has executed setX(5), it releases the object's lock. at this moment, another thread "t2" executes check(), returning <false>.

B) is a somewhat confusing question, because it does not say anything about the thread "t2" in the above example. basically, calling setXY() with multiple threads will not perpetually make i != j. but calling it with multiple threads while simultaneously calling check() with another thread might still return <false> (as in A). so i would answer "yes".

C) assuming "seperately" means "not through setXY, but directly", then the answer is "no", because they are private methods and can only be called through setXY.

D) "no" based on my above answer to "A"

Second question:
A) i assume the code will not run with 1.3 --> execution of main() method is skipped --> i.e. execution of ALL statements is skipped --> assertion statements are skipped --> then the answer is clearly "yes"
if my assumption is correct, this is an example of an imo ill-defined question, where you have to change the meaning of the question by yourself to answer it 'correctly' (in this case, you have to add "...but the other statements are not skipped").

I would say B is "yes", because "appropriate command line switch" may include "no command line switch"

as you may have noticed, some of these answers are highly subjective

regards
tiloif
[ March 16, 2006: Message edited by: Tilo Hemp ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello tilo,

i dint understand the explanation for the first part... you say that first setXY(int i) method gets excecuted.. then setX(int i) is called which is a synchronized method.. thats ok, but how can you tell that the next method to be executed is the check() method... it can be any other synchronized method..

please correct me if i am wrong.....

thank you..
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello mithun,

yes, i made a mistake, this is meant to be an example where check() returns <true>, ie. x != y. if the other synchronized method (ie. setY()) is executed , then a subsequent call to check() would return <false>.

regards
tilo
 
reply
    Bookmark Topic Watch Topic
  • New Topic