Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Letters to the Editor (April 5, 1999)

In this installment of Letters to the Editor, Merlin Hughes helps a former C++ programmer with Java; Mark Johnson answers several questions about his XML series; Bill Venners receives 'one of the best feedback e-mails' ever about his farewell column; and Bill Day fields Java 3D questions

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Step by Step: 'Reading textual data: Fun with streams' by Merlin Hughes

Read

Porsches, Subarus, and fun with streams



Merlin,

I jumped from C++ to Java several months ago. A question comes to mind when reading your code:

public class UndoReader extends Reader {
  protected static final int INITIAL_UNDO_SIZE = 64;

protected Reader reader; protected int[] buffer; ...... }


My question: Reader is an abstract class, that is, it cannot be instantiated. If so, then how can we use it as a data member?

Jianming Wang

Jianming, All subclasses of Reader -- FileReader and InputStreamReader, for example -- can be assigned to a generic Reader variable. As an example, we can treat any car implementation (Porsche or Subaru, for example) as a generic car type. The same applies here -- it doesn't matter what a specific Reader is, it still provides the generic Reader API so it can be stored in a Reader variable. Merlin Hughes


'XML for the absolute beginner' by Mark Johnson

Read

Printing files with XML



Mark,

I read your column this afternoon and found it very educational. I've read a few books on XML, and I think it will be a great standardizing tool to make full-scale applications easily accessible from anywhere -- from custom, system-dependent GUIs, to Web browsers or even future cellular phones.

I'm writing to say I think your article demonstrates, in a rather unexpected way, what XML could do where HTML fails: it allows you to control the placement of your Java code on the page.

Indeed, why not? Readers who don't want to read through the code no longer have to scroll the browser window down page after page and those who want just text can simply scroll the text area.

But what about those who want to print such files? They will, of course, just get the top of the code. With XML and XSL, one could define different ways to display the data on a screen or on a printer. That way, you can receive a full code printout on paper.

But I guess that will come some day.

Nicolas Seigneurin

Nicolas, Thanks for writing. I like your idea of using XML to present articles online in one format and print them in another. It would be interesting if a "print preview" function of an application allowed you to select (or customize) the style sheet to be used for printing. Unfortunately, we're not quite there yet. As for not being able to print the source files when I use TEXTAREA page elements for source code, I've had similar complaints from other people. I've found a solution: If you look closely at the filename of the source file (usually in the caption), you'll notice it's a hyperlink. Click that link, and it will take you to a printable version of the source that's in the text box. Mark Johnson


XML parsers



Mark,

I enjoyed your article, "XML for the Absolute Beginner." I've been searching for C++ parsers for XML. Do you know of any publicly or commercially available C++ class collections for parsing XML?

Stephen Benz

Stephen, The best known parser for C, not C++, is Expat (XML Parser Toolkit) by James Clark. This freely available parser is being used to add XML support to Netscape 5 and Perl. It's highly regarded and is available from: http://www.jclark.com/xml/ IBM has promised XML for C, which I'm interested in having a look at. IBM reps talked about it at XTech '99 in San Jose, CA, a week and a half ago, but it's not yet available on alphaWorks: http://www.alphaworks.ibm.com Although most of the parsers on the Internet.com site are written in and for Java, you may find one here that suits your needs: http://stars.com/Software/XML/parsers.html Microsoft apparently had a free XML parser in IE4. Maybe it'll tell (or sell) you something. A C++ parser, with a MFC-looking API, is among Sam Blackburn's classes at: http://ourworld.compuserve.com/homepages/sam_blackburn/wfc.htm I don't know anything about it. You might also check out "Whisper" by Jesse Jones: http://www.halcyon.com/www3/jesjones/Whisper/Home.html Of course, there may be a good reason for the paucity of C/C++ implementations of XML parsers. Maybe Java's simply a better choice for most (though, of course, not all) applications. Mark Johnson


IBM's XMI proposed standard up to snuff?



Mark,

I just wanted to say that I really enjoyed your JavaWorld articles on XML technology. We're using XML fairly extensively on this project as the import/export format between disparate tools. We're looking forward to the day when a standard DTD is in place for UML.

Keep up the great work!

Mike Clark

Mike, Thanks for writing, Mike. It's always nice to hear from an enthusiastic reader. If you don't already know about it, check out: http://www.software.ibm.com/ad/features/xmi.html This is IBM's home page for XMI, a standard proposed by IBM, Unisys, Oracle, and others for XML representation of UML. It's yet another of the many metadata proposals in the works. The W3C working group on metadata also met at XTech '99 last month in San Jose, CA. Hope this information is of use to you. Mark Johnson


Design Techniques: 'Farewell to Design Techniques' by Bill Venners

Read

Loose threads? Bill Venners responds



Bill,

I have enjoyed reading your columns. Your farewell column prompted me to review your August 1998 column, "Designing for thread safety." While I believe the article is correct in what it says, I'd like to point out some important omissions.

The article focuses on JVMs running on single processors. It does not ask the user to consider what happens when Java is executed on a multiprocessor machine, especially one with a relaxed memory model.

The Java Memory Model (JMM) was written to accommodate multiprocessors with relaxed memory models. Threads are allowed to cache values, and writes to main memory may be reordered. The JMM is specified in the Java Language Specification (JLS, Chapter 17, "Threads and Locks").

Doug Lea has also written a draft chapter for the next edition of his Concurrent Programming in Java, "Programming to the Java Memory Model":

http://gee.cs.oswego.edu/dl/cpj/jmm.html

According to the current spec, including (according to Doug Lea) any revisions that are expected to occur in the near future, being able to atomically access variables does not guarantee the visibility of those variables (visibility between threads, that is, not the 'scope' kind of visibility), nor the relative order in which variables appear to change. Without synchronization, writes might not happen for a long time, and when they do, they may appear out of order. In the presence of multiple threads, a shared object reference is a fragile thing.

To illustrate, consider the "double-check" idiom for singleton construction:

public class Singleton {
     private static Singleton instance;
     private Object value;
     private Singleton() { /* ... */ }
     public static Singleton getInstance() {
         if (instance == null) {
             synchronized (Singleton.class) {
                 if (instance == null) {
                     instance = new Singleton();
                 }
             }
         }
         return instance;
      }
      public static Object getValue() {
          return getInstance().value;
      }
}


One might think that this was a safe, efficient way to construct an immutable singleton object. Not according to the spec!

Consider this statement:

Object v = Singleton.getValue();


The thread that constructs the singleton may write out the shared reference to the singleton instance before the contents of the singleton are written out. (In this case, "contents" is the reference to value plus the contents of the value object itself.) This means that getValue may see a non-null instance reference, but it may not see the fully constructed contents. The only way to prevent a thread that is accessing the contents of this singleton from seeing transitional values is to synchronize the accessor method on the same lock as the getInstance method. That is, the static getValue method would need to be synchronized too.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources