• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exactly how syc. methods & threads work?  RSS feed

 
Wahid Sadik
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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?

 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • 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).]
 
Kalpesh Soni
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • 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: 312
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
George Toronto
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!