Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question about synchronized threads

Reply
Thread Tools

A question about synchronized threads

 
 
byhesed
Guest
Posts: n/a
 
      04-29-2011
public class A {
synchronized void m1() { ... }
synchronized void m2() { ... }
void m3() { ... }
}

The book explains above code:

Given an instance a of class A, when one thread is executing
a.m1(),
another thread will be prohibited from executing a.m1() or a.m2().

I have a question.

The explanation means than when one thread is executing m1() method,
No other threads can execute m1() or m2() thread.

Is it correct? If it is correct, how can I handle it better?
I think it is too ineffectual. Does anybody know?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      04-29-2011
On 04/29/2011 11:35 AM, byhesed wrote:
> public class A {
> synchronized void m1() { ... }
> synchronized void m2() { ... }
> void m3() { ... }
> }
>
> The book explains above code:
>
> Given an instance a of class A, when one thread is executing
> a.m1(),
> another thread will be prohibited from executing a.m1() or a.m2().
>
> I have a question.
>
> The explanation means than when one thread is executing m1() method,
> No other threads can execute m1() or m2() thread.


For that particular instance, yes.

> Is it correct? If it is correct, how can I handle it better?
> I think it is too ineffectual. Does anybody know?


What do you mean by "better"? What precisely is not the way you want it?
What is your standard of effectuality?

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
 
Daniele Futtorovic
Guest
Posts: n/a
 
      04-29-2011
On 29/04/2011 17:35, byhesed allegedly wrote:
> public class A {
> synchronized void m1() { ... }
> synchronized void m2() { ... }
> void m3() { ... }
> }
>
> The book explains above code:
>
> Given an instance a of class A, when one thread is executing
> a.m1(),
> another thread will be prohibited from executing a.m1() or a.m2().
>
> I have a question.
>
> The explanation means than when one thread is executing m1() method,
> No other threads can execute m1() or m2() thread.
>
> Is it correct?


Yes, it is correct.

> If it is correct, how can I handle it better?
> I think it is too ineffectual. Does anybody know?


By defining yourself the monitor you synchronise on.

When you declare a method synchronized, the code will use the instance
of which that method is a member as the monitor. In other words, the
code above is equivalent to this:

public class A {
void m1() {
synchronized( this ){ ... }
}
void m2() {
synchronized( this ){ ... }
}
void m3() { ... }
}

Both method lock on the same monitor, so when one is executed, no other
thread can execute any of them.

If you don't want that interdependency, you can define monitors yourself:

public class A {
private final Object
m1Monitor = new Object(),
m2Monitor = new Object()
;
void m1() {
synchronized( m1Monitor ){ ... }
}
void m2() {
synchronized( m2Monitor ){ ... }
}
void m3() { ... }
}

That way, a thread executing m1 will not prevent another thread from
executing m2, only m1 itself.

--
DF.
An escaped convict once said to me:
"Alcatraz is the place to be"
 
Reply With Quote
 
byhesed
Guest
Posts: n/a
 
      04-29-2011
On 4월30일, 오전12시58분, Daniele Futtorovic <da.futt.n...@laposte-dot-
net.invalid> wrote:
> On 29/04/2011 17:35, byhesed allegedly wrote:
>
>
>
>
>
>
>
>
>
> > public class A {
> > synchronized void m1() { ... }
> > synchronized void m2() { ... }
> > void m3() { ... }
> > }

>
> > The book explains above code:

>
> > Given an instance a of class A, when one thread is executing
> > a.m1(),
> > another thread will be prohibited from executing a.m1() or a.m2().

>
> > I have a question.

>
> > The explanation means than when one thread is executing m1() method,
> > No other threads can execute m1() or m2() thread.

>
> > Is it correct?

>
> Yes, it is correct.
>
> > If it is correct, how can I handle it better?
> > I think it is too ineffectual. Does anybody know?

>
> By defining yourself the monitor you synchronise on.
>
> When you declare a method synchronized, the code will use the instance
> of which that method is a member as the monitor. In other words, the
> code above is equivalent to this:
>
> public class A {
> void m1() {
> synchronized( this ){ ... }
> }
> void m2() {
> synchronized( this ){ ... }
> }
> void m3() { ... }
> }
>
> Both method lock on the same monitor, so when one is executed, no other
> thread can execute any of them.
>
> If you don't want that interdependency, you can define monitors yourself:
>
> public class A {
> private final Object
> m1Monitor = new Object(),
> m2Monitor = new Object()
> ;
> void m1() {
> synchronized( m1Monitor ){ ... }
> }
> void m2() {
> synchronized( m2Monitor ){ ... }
> }
> void m3() { ... }
> }
>
> That way, a thread executing m1 will not prevent another thread from
> executing m2, only m1 itself.
>
> --
> DF.
> An escaped convict once said to me:
> "Alcatraz is the place to be"


Thank you. I understand what you elaborated on.
 
Reply With Quote
 
byhesed
Guest
Posts: n/a
 
      04-29-2011
On 4썡30씪, 삤*12떆53遺, Lew <(E-Mail Removed)> wrote:
> On 04/29/2011 11:35 AM, byhesed wrote:
>
>
>
>
>
>
>
>
>
> > public class A {
> > * * *synchronized void m1() { ... }
> > * * *synchronized void m2() { ... }
> > * * *void m3() { ... }
> > }

>
> > The book explains above code:

>
> > * * *Given an instance a of class A, when one thread is executing
> > a.m1(),
> > * * *another thread will be prohibited from executing a.m1() or a.m2().

>
> > I have a question.

>
> > The explanation means than when one thread is executing m1() method,
> > No other threads can execute m1() or m2() thread.

>
> For that particular instance, yes.
>
> > Is it correct? If it is correct, how can I handle it better?
> > I think it is too ineffectual. Does anybody know?

>
> What do you mean by "better"? *What precisely is not the way you want it?
> What is your standard of effectuality?
>
> --
> Lew
> Honi soit qui mal y pense.http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg


If too much spaces are marked as critical regions,
then the program will not be optimized.
It wastes too much time in waiting for obtaining a right to access
critical regions.

So, in my question, better means optimization when using threads.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-29-2011
On 4/29/2011 9:09 AM, byhesed wrote:
>
> Thank you. I understand what you elaborated on.



I hope so. "Optimizing" threads is tricky, and when you reason about
how to optimize it's easy to make a mistake. Prominent Java engineers
(i.e., Doug Lea) have been known to make mistakes.

Remember, m1 and m2 can't share mutual state. If they do, you're going
to have problems accessing any variables that are shared.

I'd be interested in seeing what it is you want to optimize. What does
this class really look like? I think it might be instructive for you to
post your full class, and let other comment on how best to "optimize" it.

 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      04-29-2011
On 29/04/2011 20:11, markspace allegedly wrote:
> On 4/29/2011 9:09 AM, byhesed wrote:
>>
>> Thank you. I understand what you elaborated on.

>
>
> I hope so. "Optimizing" threads is tricky, and when you reason about
> how to optimize it's easy to make a mistake. Prominent Java engineers
> (i.e., Doug Lea) have been known to make mistakes.
>
> Remember, m1 and m2 can't share mutual state. If they do, you're going
> to have problems accessing any variables that are shared.


> I'd be interested in seeing what it is you want to optimize. What does
> this class really look like? I think it might be instructive for you to
> post your full class, and let other comment on how best to "optimize" it.


Ten bucks say it's a class with too many methods.

 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      04-29-2011
On 4/29/2011 11:19 AM, Daniele Futtorovic wrote:

> On 29/04/2011 20:11, markspace allegedly wrote:
>> I'd be interested in seeing what it is you want to optimize. What does
>> this class really look like? I think it might be instructive for you to
>> post your full class, and let other comment on how best to "optimize" it.



> Ten bucks say it's a class with too many methods.



Uh, yeah, not taking that bet...



 
Reply With Quote
 
Dagon
Guest
Posts: n/a
 
      04-29-2011
byhesed <(E-Mail Removed)> wrote:
>public class A {
> synchronized void m1() { ... }
> synchronized void m2() { ... }
> void m3() { ... }
>}
>The book explains above code:
> Given an instance a of class A, when one thread is executing
>a.m1(), another thread will be prohibited from executing a.m1() or a.m2().


Throw this book away and get a better one (Goetz, "Java Concurrency in
Practict" is good).

>I have a question.
>The explanation means than when one thread is executing m1() method,
>No other threads can execute m1() or m2() thread.
>Is it correct?


This is kind of correct for this example, but it's stated in such a way that
it will confuse you until you learn more completely what synchronized does.

What it really does is: "before any thread enters m1 or m2, it will acquire an
exclusive lock on the instance of A. No other thread may acquire that lock
for that instance of A until the lock-holding thread releases it by exiting
the method."

>If it is correct, how can I handle it better?
>I think it is too ineffectual. Does anybody know?


Depends on what you want to happen - why do you think it's ineffectual? It's
pretty effective if you want to make sure that only one thread at a time
can run those methods on the same instance.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Alice
Guest
Posts: n/a
 
      04-30-2011
On 29/04/2011 6:55 PM, Dagon wrote:
> byhesed<(E-Mail Removed)> wrote:
>> public class A {
>> synchronized void m1() { ... }
>> synchronized void m2() { ... }
>> void m3() { ... }
>> }
>> The book explains above code:
>> Given an instance a of class A, when one thread is executing
>> a.m1(), another thread will be prohibited from executing a.m1() or a.m2().

>
> Throw this book away and get a better one


Why? The statement he quoted is perfectly accurate.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronized Block v.s. Synchronized Method Jerry Java 4 08-11-2010 02:34 PM
Question on synchronized and non synchronized methods ankur Java 4 11-28-2008 09:56 PM
synchronized block in synchronized static method dmcreyno Java 9 06-27-2006 07:43 PM
Use of synchronized variables over synchronized methods? Pep Java 6 08-15-2005 01:29 PM
Synchronized threads inside of a SERVLET gfrommer@hotmail.com Java 0 01-24-2005 06:38 PM



Advertisments