Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does an anonamous run() method inherit it's parent methond's synchronized block?  RSS feed

 
Mike Retondo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across some code that doesn't look right to me. I striped it down to the minimum.

01 public void foo( )
02 {
03 synchronized ( mLock )
04 {
05 try
06 {
07 // do some work
08 doThis();
09 doThat();
10
11 // Need to do this on another thread to avoid concurrent modification errors
12 new Thread( new Runnable()
13 {
14 public void run()
15 {
16 if ( mMemberVar != null )
17 mMemberVar.doSomething();
18 }
19 }).start();
20 }
21 catch ( Exception e )
22 {
23 }
24 }
25 }
26 public void bar()
27 {
28 synchronized ( mLock )
29 {
30 mMemberVar = null;
31 }
32 }

What I'm concerned about is on lines 16 and 17. What if mMemberVar is true on line 16 and then a task switch happens. Then bar() could be called and null out mMemberVar. Then when run() returns line 17 will fail because mMemberVar will be null. Is this code okay because the run() is nested inside the synchronized ( mLock ) block of foo()?
 
Paul Clapham
Sheriff
Posts: 22528
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Retondo wrote:Is this code okay because the run() is nested inside the synchronized ( mLock ) block of foo()?


No. The synchronized keyword describes what happens at run-time, and it allows you to say that code executed in a synchronized block will be free of interference, if you synchronize on the same object and so on and so forth.

But the contents of that run() method are not executed as part of that synchronized block. They are executed later, in a separate thread, and they aren't synchronized on that object.
 
Mike Retondo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Mike Retondo wrote:Is this code okay because the run() is nested inside the synchronized ( mLock ) block of foo()?


No. The synchronized keyword describes what happens at run-time, and it allows you to say that code executed in a synchronized block will be free of interference, if you synchronize on the same object and so on and so forth.

But the contents of that run() method are not executed as part of that synchronized block. They are executed later, in a separate thread, and they aren't synchronized on that object.

Thanks Paul,

That's exactly what I thought from doing allot of reading up on threads today. I plan to patch it by adding a synchronized block on the same object inside the run() method as follows, will this work? I'm not sure since the run() method will execute imeditetly and the lock on mLock will still be in effect because we have'nt left the outer synchronized block yet so run() will be blocked, so will Start return?

12 new Thread( new Runnable()
13 {
14 public void run()
15 {
synchronized ( mLock )
{
16 if ( mMemberVar != null )
17 mMemberVar.doSomething();
}
18 }
19 }).start();
20 }
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!