It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes thread synch and inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread synch and inner classes" Watch "thread synch and inner classes" New topic
Author

thread synch and inner classes

tom walters
Ranch Hand

Joined: Feb 14, 2002
Posts: 34
i have a class with 2 inner classes that are threads

how do i make this thread safe? make the check() method in Checker synchronized? put the o = c.check(); in a synchronized block using the monitor String object?
[ April 26, 2002: Message edited by: tom walters ]
Formatted and added code tags - George
[ May 28, 2002: Message edited by: George Brown ]
Thomas Smets
Ranch Hand

Joined: Dec 11, 2001
Posts: 111
Hi Tom,
I simplified your code as the stub you gave would never be able to compile :
Please note that I don't like the idea that the []inotification[/i] has to be in a synchronized block, but it's rather logical to have it so... So !
Please also note that this example s inspired from "JAVA threads" by Scott Oaks & Henry Wong at O'Reilly, ISBN 1-56592-216-6
Make sure you get the second edition at least, while these references dates to the first edition (ISBN number)


Rgds,
Thomas,


Thomas Smets
Just another developper
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Tom, either of your proposed solutions would make that a thread safe operation.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by CL Gilbert:
Tom, either of your proposed solutions would make that a thread safe operation.

Mhhh, you probably meant to write neither?
It's not the check() that is critical, but parallel usage of o. This usage has to be synchronized.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Ilja Preuss:

Mhhh, you probably meant to write neither?
It's not the check() that is critical, but parallel usage of o. This usage has to be synchronized.

Since the threads are not sharing anything, their are no synchronization issues.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread synch and inner classes