• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

can a thread having lock of object, simulatenously invoke TWO sunchronized methods ?

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its very important for me to know this and clear my concept
also ...
can a thread obtain lock on object
AND, OR ???
can it obtain lock on synchronized method of object ?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sarim
Your question sounds important to understand the thread concept, but I think there is unclear in your question. What is the actual meaning of "simulatenously invoke TWO sunchronized methods ? ". In my view, there are two situations:
1) thread has enter monitor of the object, so the method invoked by the thread can run. where can the thread get another method? You know, one thread is corresponding to a method, that is 1 thread ----> 1 method (RIGHT?) so it is impossible to invoke another method.
2) Another situation is if you invoke another syschronized method in the current method invoked by the current thread, which is permitted if the current thread is running. For example:

that is OK.
If you can claim your question more definately, i maybe help you more. morover, if I made mistake, plz point them out.Thanks
regds
George

[This message has been edited by George Toronto (edited January 01, 2001).]
[This message has been edited by George Toronto (edited January 01, 2001).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- subj. question makes little sense to me, how can a Thread invoke two methods simultaneously??? As far as I know there's no way to do that. It's possible, however, to call an asynchronous method (like start() in Thread, which is btw is not synchronized, but could be as well) which doesn't block and upon return from this call invoke another synchronized method. In other words, you need to have two threads running in this case. In general, you cannot run two methods (synchronized or not) simultaneously on the same thread. Give more details if this is not what you wanted to know.
- Yes, a thread can obtain a lock on an object:
<pre>
Object o = new SomeClass();
synchronized(o) { // do something }
</pre>
- No, there's no method level locks in Java, but you can play tricks like (quick guess, tell me if wrong...):
<pre>
class X
{
public static final byte[] lock = new byte[0];

public synchronized void m()
{
synchronized (lock)
{
// do something
}
}
}
class Y
{
public void foo()
{
synchronized (X.lock) //need to do smthng before calling m()
{
// ... do things
X x = new X();
x.m();
}
}
}
</pre>
regards,
VG.
[This message has been edited by Vlad G (edited January 01, 2001).]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll take the liberty of presuming your question. Do you mean if a Thread has the monitor of an Object, then can that Thread invoke more than one synchronized method of that Object? If that is what you were asking, then the answer is yes. An Object can have more than one synchronized methods but only has one monitor. Thus, as long as a Thread has that monitor to the Object, then the Thread can freely invoke any synchronized method of that Object.
 
Danger, 10,000 volts, very electic .... tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic