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

Urgent ..one more Thread ....

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am giving this que... which I think here only umpteenth time being asked ...
but again i am repeating... as i want to clear myself.... and second less time ....
thanks to all in advance .
Que
Question ID :988384270281
Consider the following code:

What will the above code print?
1. keep printing same values for x & y incremented by 1 on each line.
2. keep printing same values for x & y they may be incremented by more 1 on each line.
3. It may print duffrent values for x & Y but both the values will be incremented by 1 each line
4.you can not say any thing abt the values.
Given ans is 1.
but I think 4. because ... lock is on "obj" object of class Object. and here in code we are not using any synchronized method of Object class... so it is as good as not having any lock on that code block.
PLEASE correct me if I am wrong ..... so I think ans is 4.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
just from my own opinion,having a synchronized method inside of a synchronized code block is of no use , as only ONE thread is allowed to execute
the code inside of the synchronized block,and yes i think answer one is correct but i,ll write it compile it and see.
hope this helps.
thanks
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aymen esawey:
hi
having a synchronized method inside of a synchronized code block is of no use , as only ONE thread is allowed to execute
the code inside of the synchronized block


Yes you are right is of no use having sync menthod inside sync block....
but point is this .. on which object we have obtained the lock ...
as run is not sync... and lock is on obj.. which has no sync method ... then other thread's run method can execute whenever JVM schedules it as it won't need monitor..
I think objcet which is locked (either by making instant method to be sync or sync the object).. that object's sync method can not be executed.
CMIW
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
thaks a lot ..
got it .. number 1 is correct ..as obj is static... and when second thread goes to acquire the lock on obj ,it wont get lock as obj is already locked
thabks a lot .. all guys ..
 
aymen esawey
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
lets say:
...main...
{ Test a=new Test(); Test b=new Test();
a.start(); b.start();}
now you have both threads a and b executing their run at the same time,but when they reach the sychronized(obj){}block only one will get in at a time,i know you're htinking where is yield or sleep.but the o/s has it all done automatically without our interference.it's not my words these are from thinking in java.point is the operating system will give each thread a time slice.
hope am right.
thanks
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aymen esawey:
it's not my words these are from thinking in java.point is the operating system will give each thread a time slice.


book is right ... but point is that .. when a thread comes to run (if it comes.. as per book)it finds that .. obj has not released monitor so that thread go back to wait ...
CMIW
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic