[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programming Java threads in the real world, Part 7 - JavaWorld



Oyvind Teig wrote:

> Thanks, Richard. Working on the article:
>   http://www.javaworld.com/javaworld/jw-04-1999/jw-04-toolbox.html
> Learning on a steep curve.
>
> What does it mean when he states that
>
>   class Color_scheme
>     ...
>     /*package*/ synchronized change_color_scheme(
>              Color foreground, Color background )
>
>   Note that this is one of the few cases in which you must use
>   package access rather than an inner class.

> Where is the package defined? (So, what is a package, anyhow..)

Java packages encapsulate sets of classes in a common name scope. A
special feature of a package is that all class members without 'public',
'private', or 'protected' will have package wide scope, i.e. every other
class in the same package has access to such members.

See the tutorial for more info:
http://java.sun.com/docs/books/tutorial/java/more/packages.html

The example code does not include a package statement (there exists an
unnamed default package which might be where this code would be
compiled.) If there were a package statement, it would be the first real
line of code in the file and the file itself would normally be found in
a subdirectory formed from the package name, replacing dots with
slashes.

The example explains why an inner class would not solve the particular
race problem. It's not very clearly explained, IMHO. What they meant to
say was that it would be tempting to use an inner class (i.e. a nested
class) to encapsulate the colour scheme, but doing so would not be
robust. Careless users could easily access the members in such a way
that race conditions could exist. Only by having a separate class can
this be avoided for sure.

Arguably, the package issue is a red herring in the context of the
discussion about race conditions. There may or may not be good software
engineering reasons for sharing a common package in this example, but
the colour scheme class could, in principle, be in any well-defined
package, not necessarily the same package as the hypothetical Window
code.

Rick
--
Richard Beton B.Sc. C.Phys. M.Inst.P.
Roke Manor Research Limited (http://www.roke.co.uk/)
--------- Standard Disclaimer about my own views etc etc --------
---------  My mail client accepts rich text (HTML) mail  --------
Welsh Highland Railway: http://www.whr.co.uk/WHR/WHR.html