• 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

Exactly how syc. methods & threads work?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These following questions are on synchronized methods. I've included my guess.
Anyone finding these wrong or willing to add anything to these
are most welcome. I really need help on these. I look for experts on these.

Q1. What happens to a thread that fails to grab a monitor for a specific object?

I think:
-------
This thread will look for another schedule i.e. it will be at the mercy of thread scheduler.

Q2. How an InterruptedException is thrown?

I think:
-------
Consider following situation :
Reference oD has 3 methods; mAA, mBB are synchronized, mCC is not.
mAA, mBB, mCC all modify oD.
3 threads tA, tB, tC all operate on oD.
tA calls mAA, tB calls mBB, tC calls mCC.
Now,
(1) As these threads are scheduled, let's say,tA is running first.
tA grabs the monitor on oD, calls mAA and is now working.
(2) Now the scheduler runs tB. As tB tries to grab monitor on oD, it
sees monitor is busy and looks for next schedule.
(3) Now tC gets to run. It calls mCC, mCC tries to modify oD. Now
InterruptedException is thrown.

Q3. When a thread calls 'sleep()' or 'wait()' or 'yield()' in synchronized methods, is the monitor is released?

I think:
-------
According to a Mughal example (9.3), 'wait()' releases monitor.
A wild guess is,
as a method calling 'wait()' must be notified by other threads, it should
release monitor.
I don't know about 'sleep()'.
But one thing I cannot understand is that,
'sleep()' and 'wait()' throw InterruptedException. If 'wait()' releases
the monitor, how and when this exception is thrown.

Q4. Related to Q3, join( , ) method is marked as synchronized
but join() is not. Both of them throw InterruptedException.
Why these two are different? How the exceptions are thrown?

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the 1.3 API, I don't see this. None of the join
methods are synchronized. They are all just public final void. In what doc are you
seeing the synchronized join?
[This message has been edited by Jim Baiter (edited January 08, 2001).]
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
InterruptedException is called when thread is interrupted using interrupt() method of Thread class.
read javadocs for further info

------------------
KS
 
Kalpesh Soni
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And one other thing
suppose if there are two methods in one class
one is synchronised and the other is not
and there is a variable in that class
if the unsynchronised method changes the variable when there is a lock on the other thread
nothing happens
no exception is thrown
it is the programmers responsibility to see when a variable is changed and all those methods must be synchronised

------------------
KS
 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
I found that those two versions I mentioned are synchronized in Java in a Nutshell, 3rd Edition, pg 371.
Please comment on it.
------------------
Regards - Wahid
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wahid Sadik:

Q1. What happens to a thread that fails to grab a monitor for a specific object?

I think:
-------
This thread will look for another schedule i.e. it will be at the mercy of thread scheduler.


of coz, that thread that wanna get a monitor has to wait for another thread to release the lock of monitor of the specified object.

Q3. When a thread calls 'sleep()' or 'wait()' or 'yield()' in synchronized methods, is the monitor is released?[/b]
I think:
-------
According to a Mughal example (9.3), 'wait()' releases monitor.


Ya, u got it!

I don't know about 'sleep()'.


sleep() does not release lock. There r two different concepts between monitor lock and CPU schedulor. If u can remember the difference, u do not feel confused any more. u know, monitor is owned by a object, just like static members of an oject, of coz, static members use anther monitor mechanism. when we talk about sleep() method, we are discussing about the CPU time schedulor, right? but not monitor of an object. sleep() just puts a thread into a waiting state, then CPU schedulor arranges when the thread should be running after weaking. However, a thread grads a monitor, that is, other threads have to wait for the thread's termination to access the sych'ed resource of that specific object.right? so sleep() doesnot release the lock.

'sleep()' and 'wait()' throw InterruptedException. If 'wait()' releases the monitor, how and when this exception is thrown.


InterruptedException is thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.


Q4. Related to Q3, join( , ) method is marked as synchronized
but join() is not. Both of them throw InterruptedException.
Why these two are different? How the exceptions are thrown?


public final void join() throws InterruptedException
 
reply
    Bookmark Topic Watch Topic
  • New Topic