<< July 2008 | Home | September 2008 >>

Friday Java Quiz: Accidents Of Implementation

Do you own a copy of The Pragmatic Programmer? Have you ever been to a Dave Thomas talk at a developer conference? Then you no doubt have seen the following snippet of Java code (p.173):

paint(g);
invalidate();
validate();
revalidate();
repaint();
paintImmediately(r);

Here it looks like Fred is desperately trying to get something out on the screen. ...

Q: Can a valid Swing program be written that contains the above lines of code? (No trick answers.)

Q: In which Swing classes or interfaces are each of the above methods defined?

Q: What are the legitimate use cases of each of the methods?

Tags :

Timothy Gowers: The Princeton Companion to Mathematics

Timothy Gowers: A quick post to give an update on the Princeton Companion to Mathematics. It’s been off my hands for a few weeks now. If all goes well it will be printed by the middle of September and should appear in bookshops about a month later. This is what the cover will be like. (If I work out how to do it, I’ll get a reduced-size picture of the cover to appear as part of this post rather than as a link.) If you go to this page on the PUP website then you will find a podcast interview that I gave, which contains information that does not appear on this blog.

Timothy Gowers is a Fields Medal winning British mathematician. This volume seems to be this generation of mathematician's attempt at explaining what they do, just as previous generations of mathematicians have done, for example, with Mathematics, Its Content, Methods, and Meaning by the Russian school and the Bourbaki series by the French school. At 1000 pages and nearly 200 entires, I imagine it would be more like an encyclopedia than anything else. However, a glance at the contributors list will convince any mathematician that this volume is a worthy addition to the bookshelf.

Quote Of The Day

(Sometimes you get bad news, but sometimes you get the good.)

All merged and checked in. I built with all the other CRs and the tests all ran successfully.
Tags :

Re: OpenJDK In Ten Years

David Herron of Sun is attempting to answer the question "What do you think of OpenJDK 10 years from now?"

Ten years is a long time in the computer industry. Ten years ago, we are in the third year of Windows 95, a 32-bit OS for the 32-bit Intel CPUs of the time, after a Finnish student had gotten tired of running a 16-bit OS on 32-bit hardware and started to write his own.

If someone described todays computer hardware to me ten years ago, I probably would say something like "Microsoft had better fix the animation sequence at the end of Solitaire so that it's speed is not CPU frequency dependent, or it would run so fast that nobody would be able to see it. Plus, how do you play Solitaire on a 3.5-inch handheld screen."

Knowing the limitations of human imagination, any predictions we make today would certainly look silly when the time really comes. Nevertheless, we can still extrapolate and know that the following must be true ten years from now:

  • The OpenJDK JVM will remain a 32-bit/64-bit application running in compatibility mode on the prevalent 128-bit machines. People would have realized that porting it to 128-bit is pointless when an "int" is always 32-bit.
  • The OpenJDK distributions would have been split apart, with the most useful portion being maintained while the rotten part—the HTML 3.2 renderer, the com.sun.corba ORB—were cut off.
  • The javac compiler would become multi language front-end capable and become the focal point of all JVM languages—it will be renamed "jcc" for JVM Compiler Collection and it will support Java (30% share), Scala (15%), Groovy (50%) and other languages.
  • When you tell people that you develop software for the JVM, people will treat you the way we treat VB6 developers today. But in your heart, you know the JVM still provide value to your customers, just like the VB6 VM does today.
  • The JVM will be one of four "managed" virtual machine environments, which are the legacies of the 90's and 00's generation. The other three being:
    • .NET, which Microsoft would have abandoned five years earlier just like they are trying to get people off of Win32 now;
    • The Mozilla virtual machine, which under the guise of being part of the browser has grown to be a full featured VM;
    • The Parrot virtual machine, which after about 20 years would finally go 1.0, and attracting the Ruby and Python folks away from the JVM.
  • The JVM will remain a portable UI toolkit and be ported to all sorts of handheld and footheld devices. Instead of shunning the JVM as a ball-and-chain, device vendors will be eager to port the JVM to their devices because it would enable a whole slew of applications on the device.
  • The reason "managed" environments would be out of fashion is because the most important benefit it offers—garbage collection—would have been off loaded to mainstream hardware, and writing "native" applications become fashionable again. And "unmanaged" Java, Clojure, C# will be popular there as well.

I'll come back ten years from now and blog about how accurate my predictions are.

Tags :

Friday Java Quiz: Annotation On Enum Constants

Kevin IMed me yesterday with a question about deprecating a enum member, which I pass on to you today.

Q: Will the following enum compile?

enum Foo {
  BAR, @Deprecated BAZ
}

Q: What about this?

enum Foo {
  BAR,
  /**
   * @deprecated BAZ should not be used any more
   */
  BAZ
}

Q: Will either have the intended effects on client code and Javadoc?

Tags :

Have You Seen This Before?

When I got up, I found my blog down. Tomcat 5.5 on OpenJDK 6 crashed five hours earlier. My first thought was that this was OpenJDK 6's fault. Checking /var/log/tomcat55/catalina_2008-08-20.log, I saw:

2008-08-20 00:31:35,987 [TP-Processor15] INFO  net.sourceforge.pebble.event.resp
onse.SpamScoreListener  - Good site rejected : spam score is 3, threshold is 1
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00002ab5d55c3353, pid=22489, tid=1075845456
#
# Java VM: OpenJDK 64-Bit Server VM (1.6.0_0-b11 mixed mode linux-amd64)
# Problematic frame:
# V  [libjvm.so+0x472353]
#
# An error report file with more information is saved as:
# /tmp/hs_err_pid22489.log
#
# If you would like to submit a bug report, please visit:
#   http://icedtea.classpath.org/bugzilla
#

When I tried to restart Tomcat, it errored out with a peculiar exception:

Using CATALINA_BASE:   /var/lib/tomcat5.5
Using CATALINA_HOME:   /usr/share/tomcat5.5
Using CATALINA_TMPDIR: /var/lib/tomcat5.5/temp
Using JRE_HOME:       /usr/lib/jvm/java-6-openjdk
Using Security Manager
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NoSuc
hMethodError: java.util.logging.LogManager.getStringProperty(Ljava/lang/String;L
java/lang/String;)Ljava/lang/String; (Caused by java.lang.NoSuchMethodError: jav
a.util.logging.LogManager.getStringProperty(Ljava/lang/String;Ljava/lang/String;
)Ljava/lang/String;)
        at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactory
Impl.java:538)
        at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactory
Impl.java:235)
        at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactory
Impl.java:209)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
        at org.apache.catalina.startup.Bootstrap.(Bootstrap.java:53)
Caused by: java.lang.NoSuchMethodError: java.util.logging.LogManager.getStringPr
operty(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
        at java.util.logging.StreamHandler.configure(StreamHandler.java:78)
        at java.util.logging.StreamHandler.(StreamHandler.java:94)
        at java.util.logging.ConsoleHandler.(ConsoleHandler.java:88)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
        at java.lang.Class.newInstance0(Class.java:372)
        at java.lang.Class.newInstance(Class.java:325)
        at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLo
gManager.java:397)
        at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLo
gManager.java:341)
        at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLo
gManager.java:236)
        at java.util.logging.LogManager$2.run(LogManager.java:277)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.util.logging.LogManager.readPrimordialConfiguration(LogManager.j
ava:275)
        at java.util.logging.LogManager.getLogManager(LogManager.java:258)
        at java.util.logging.Logger.(Logger.java:238)
        at java.util.logging.LogManager$RootLogger.(LogManager.java:966)
        at java.util.logging.LogManager$RootLogger.(LogManager.java:963)
        at java.util.logging.LogManager$1.run(LogManager.java:198)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.util.logging.LogManager.(LogManager.java:175)
        at java.util.logging.Logger.getLogger(Logger.java:291)
        at org.apache.commons.logging.impl.Jdk14Logger.getLogger(Jdk14Logger.jav
a:152)
        at org.apache.commons.logging.impl.Jdk14Logger.(Jdk14Logger.java:5
3)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
        at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactory
Impl.java:529)
        ... 4 more
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Could not initial
ize class java.util.logging.LogManager
        at java.util.logging.LogManager$Cleaner.run(LogManager.java:221)
Could not find the main class: org.apache.catalina.startup.Bootstrap. Program wi
ll exit.

Tomcat 5.5 is essentially saying that it could not find the method:

class java.util.logging.LogManager {
  String getStringProperty(String, String);
}

"This can't be good," I said. When I tried to look at the methods in java.util.logging.LogManager with javap, I was surprised to find the following:

[weiqi@gao] $ javap java.util.logging.LogManager
Compiled from "LogManager.java"
public class java.util.logging.LogManager extends java.lang.Object{
    public static final java.lang.String LOGGING_MXBEAN_NAME;
    protected java.util.logging.LogManager();
    public static java.util.logging.LogManager getLogManager();
    public void addPropertyChangeListener(java.beans.PropertyChangeListener) 
   throws java.lang.SecurityException;
    public void removePropertyChangeListener(java.beans.PropertyChangeListener) 
      throws java.lang.SecurityException;
    synchronized java.util.logging.Logger demandLogger(java.lang.String);
    public synchronized boolean addLogger(java.util.logging.Logger);
    public synchronized java.util.logging.Logger getLogger(java.lang.String);
    public synchronized java.util.Enumeration getLoggerNames();
    public void readConfiguration()       throws java.io.IOException, java.lang.
SecurityException;
    public void reset()       throws java.lang.SecurityException;
    public void readConfiguration(java.io.InputStream)       throws java.io.IOEx
ception, java.lang.SecurityException;
    public java.lang.String getProperty(java.lang.String);
    java.lang.StRing getStringProperty(java.lang.String, java.lang.String);
    int getIntProperty(java.lang.String, int);
    boolean getBooleanProperty(java.lang.String, boolean);
    java.util.logging.Level getLevelProperty(java.lang.String, java.util.logging
.Level);
    java.util.logging.Filter getFilterProperty(java.lang.String, java.util.loggi
ng.Filter);
    java.util.logging.Formatter getFormatterProperty(java.lang.String, java.util
.logging.Formatter);
    public void checkAccess()       throws java.lang.SecurityException;
    public static synchronized java.util.logging.LoggingMXBean getLoggingMXBean(
);
    static java.util.logging.LogManager access$002(java.util.logging.LogManager)
;
    static java.util.logging.LogManager access$000();
    static java.util.logging.Logger access$102(java.util.logging.LogManager, jav
a.util.logging.Logger);
    static java.util.logging.Logger access$100(java.util.logging.LogManager);
    static boolean access$302(java.util.logging.LogManager, boolean);
    static boolean access$402(java.util.logging.LogManager, boolean);
    static java.lang.String[] access$600(java.util.logging.LogManager, java.lang
.String);
    static void access$700(java.util.logging.Logger, java.util.logging.Logger);
    static java.util.logging.Level access$800();
    static void access$900(java.util.logging.LogManager);
    static {};
}

No wonder Tomcat 5.5 can't find the method! The methods return type was all messed up. Time for a reboot. When I reboot, Debian threw up some message on the console which essentially said "can't do it, pal, you need to press the reset button."

After pressing the reset button, Tomcat 5.5 start up OK.

The point of this post? There are no points. I just feel that this is peculiar. It's one of those situations where a reboot "fixed" the problem. Something is wrong in this box. But I don't know what it is. I'll just have to wait until the problem recurs in the future.

If you have dealt with such problems before, I would be glad to hear your opinions.

Tags :

JavaFX Desktop SDK By End Of 2008, Without...

... the On2 video codecs for live video streams?

(Via James Weaver.)

Danny Coward: Looking ahead a little, the plan here is to release the final version of the SDK for the desktop at the end of this year and a preview of the mobile version next March or so. I say or so, not just because schedules are schedules, but because we are ready to adjust based on the feedback we get from this preview release.

Release schedules are indeed slippery things. When I read "by the end of this year", I went, "that's not what they said last time?" But what did they say last time, "fall of 2008"? I couldn't find the link! Preserving the link is one of the purposes of this blog.

Another purpose of this blog is to point out something obvious from the quoted interview:

Param Singh, Sun senior director of Java marketing: JavaFX isn't exactly complete yet. Some features, like the On2 video codecs for live video streams, won't be available until next year.

With a little logic, it is not hard to deduce that the "final version of the SDK for the desktop" won't contain "the On2 video codecs for live video streams".

Sigh!

Tags :

Friday Java Final Exam: Class Loaders

Go read Charles Sharp on Java ClassLoaders.

Charles gave the presentation at the St. Louis JUG yesterday, as CodeToJoy reverse-advertised. I actually had a conflict and did not attend. However Charles did the talk at the OCI internal Java lunch just a couple of hours ago.

Charles did such a thorough job at researching the material that I decide to turn his talk into Friday Java Final Exam!

Final exam (100 points):

  1. (40 points) Review the slides
  2. (40 points) Mentally analyze the example classes and explain how each program will behave
  3. (20 points) Enumerate all the ClassLoader related improvements in Java 6
Tags :

Friday Java Quiz: Thread Priorities

The standard advice given about using Java thread priorities can be found on p.218 in Java Concurrency In Practice:

Avoid the temptation to use thread priorities, since they increase platform dependence and can cause liveness problems. Most concurrent applications can use the default priority for all threads.

But when was the last time good advice prevented us from having a little fun?

Q: What will the following Java program print?

public class Foo {
  public static void main(String[] args) throws Exception {
    MyThread[] ts = new MyThread[] {new MyThread(), new MyThread()};
    ts[0].setPriority(Thread.MAX_PRIORITY);
    ts[1].setPriority(Thread.MIN_PRIORITY);
    for (Thread t: ts) t.start();
    Thread.sleep(1000);
    synchronized(MyThread.class) {
      MyThread.done = true;
    }
    for (Thread t: ts) t.join();
    System.out.println(ts[0].counter > ts[1].counter);
  }
}

class MyThread extends Thread {
  public static boolean done = false;
  public int counter = 0;
  public void run() {
    while (true) synchronized(MyThread.class) {
      if (done) return;
      counter++;
    }
  }
}
Tags :

ECMAScript Harmony

Remember this from 281 days ago? It seems that the two sides have apparently patched up their differences and agreed on a future plan.

Brendan Eich: Executive Summary

The committee has resolved in favor of these tasks and conclusions:

  1. Focus work on ES3.1 with full collaboration of all parties, and target two interoperable implementations by early next year.
  2. Collaborate on the next step beyond ES3.1, which will include syntactic extensions but which will be more modest than ES4 in both semantic and syntactic innovation.
  3. Some ES4 proposals have been deemed unsound for the Web, and are off the table for good: packages, namespaces and early binding. This conclusion is key to Harmony.

  4. Other goals and ideas from ES4 are being rephrased to keep consensus in the committee; these include a notion of classes based on existing ES3 concepts combined with proposed ES3.1 extensions.

I don't know about you, but it sounds to me that ES3.1 (Microsoft & Yahoo!) has won, and ES4 (Adobe & Mozilla) is no more.

Maybe James Ward of Adobe played the "Flex 3, which uses ActionScript 4, which will be the next version of ECMAScript" hand once too many!

I don't know.

Tags :

Neal Gafter: Core Team That Guides Java

In an InfoQ interview, Neal Gafter talked about closures, Java 7 features, and Scala--.

However, the following passage resonated with me the most:

Neal Gafter: So, I think most programming languages have one person or a small number of people that form a core team that guides the design of the language, and the evolution of the language. Stroustrup for example for C ++ and Anders for C# and I think that Alex Buckley and James Gosling could play that kind of role for Java.

Someone definitely needs to assert stronger leadership over Java the programming language, to provide a coherent vision, to move it forward, and to make progress.

Tags :

Friday Java Quiz: protected

Brian Goetz made the following claim on the openjfx-compiler project mailing list:

Brian Goetz: In fact, I'll bet that many java developers would be surprised to learn what "protected" really means in Java. Even language experts get confused on these points.

So what does protected really mean? Let's find out if you know the answer.

Q: Will the following Java source files compile together? Will Main run without exceptions? If so, what will be printed?

// base/Base.java
package base;
public class Base {
  protected int i;
  public Base(int i) {
    this.i = i;
  }
}
// derived/Derived.java
package derived;
import base.*;
public class Derived extends Base {
  public Derived(int i) {
    super(i);
  }
  public void foo(Base b) {
    System.out.println(b.i);
  }
}
// Main.java
public class Main {
  public static void main(String[] args) {
    Derived d = new Derived(1024);
    d.foo(d);
  }
}

Today's semi-relaxed rules: no running the compiler, but you can consult the JLS.

Tags :

Upgraded Weblog Software To Pebble 2.3.1

You won't feel it too much, but I have migrated this blog from gao-2002 (a.k.a my wife's workstation) to gao-2006 (my workstation), in order to give gao-2002 a much needed OS upgrade, from Fedora Core 6 to something newer, maybe Fedora 9.

I had a false start on Sunday when my first attempt resulted in some errors. I spent the last few evenings experimenting with different migration procedures. The final move this evening took me about 27 minutes, from 8:43pm to 9:10pm, most of the time was spent on customizing the page templates.

Here's some parameters of the new setup of this blog:

  • AMD Athlon(tm) 64 Processor 3500+
  • cpu MHz: 2210.216
  • Debian GNU/Linux 4.0
  • Java 6 OpenJDK
  • Apache Web Server 2.2.3
  • apache2.2-common including proxy_ajp
  • Tomcat 5.5.26
  • Pebble 2.3.1
  • JavaMail 1.4.1

[Update] I'm having some additional challenges with the new setup. When I saved this blog entry, I received an error page. I'll see if I can get this blog entry published by some other means. The worse thing that can happen is this blog may become unreadable for a short time.

[Update] The above attempt did result in the home page not rendering correctly. I reindexed the whole blog. I hope this time it works.

[Update] OK. It finally worked. It's 11:46pm now. Time for bed.

Watch JavaFX SDK Run---On Linux

Sun released the JavaFX Preview five days ago. One of the key deliverables of this technology preview release is the JavaFX Preview SDK (the other three being a NetBeans bundle, Project Niles, and JRE 6u10).

The preview release is met with general enthusiasm from the Java camp, with predictable "too little, too late" chants from certain corners of the RIA world. I believe Kirk Pepperdine's analysis to be on the mark:

Kirk Pepperdine: JavaFX has already made a big difference in how client side Java works and this promises to get better. Stephan Janssen (www.parleys.com) demonstrated in a keynote at both JavaONE and TSSJS-Europe that JavaFX can provide an experience that rivals Adobe Air. And we can't forget that JavaFX has the added advantage that is it Java and as such will have all of the rich Java APIs available to it. With this in mind, I'm pretty optimistic that JavaFX, being free and open, will become the preferred choice for building rich clients if....

One thing that people noticed right away is the fact that the JavaFX Preview SDK is available for Windows and Intel/Mac, but not Linux or Solaris. A cursory examination of the Windows and Mac OS X version of the JavaFX Preview SDK reveals that they are almost identical in content. The only differences are in native libraries and startup scripts/executables. The native libraries fall into the area of native audio/video playback and (for Windows) some 3D effects.

"Hmm, I bet if I download the zip version of the Mac OSX release, it would work on Linux!"

And guess what? It—most of it anyway, native video playback being the exception—does work.

Here's what I did:

  • Download the zip version of the Mac OSX release (it contains start-up shell scripts that the Windows release does not)
  • Unzip it in /opt to get a /opt/javafx-sdk1.0pre1/ tree
  • Add /opt/javafx-sdk1.0pre1/bin to $PATH
  • Set $JAVAFX_HOME to /opt/javafx-sdk1.0pre1

With this setup, all the little snippets of JavaFX Script code I've written to test out the language features compile and run fine. What's more, the five sample programs that come with the SDK also run fine. Here's a screen cast of them being run on my Debian 4.0 GNU/Linux amd64 (Linuxy format, you may need to download VLC to view):

JavaFX Preview SDK running on Linux

Note that an official Linux version of the JavaFX SDK is promised for release 1.0. This post is only meant for the impatient Linux folks out there who just want to try some of the stuff out.

Tags :

Let 6u10 Be Java 7

Eric Burke: Based on the number of features and time to market, JDK 1.6u10 should just be called Java 7, and make the features java.* or javax.* pkgs.

+1

Tags :

Saturday Java Quiz: Know Your javac Command

Yesterday at lunch, I asked Charles: "It's Friday today, isn't it?" Charles said back: "That's sad."

Sadder still, even after the Friday-ness was confirmed, it didn't occur to me that I have been negligent of my Friday Java Quiz duties for two weeks in a roll.

So here goes, a day late, but just as punishing and irrelevant.

The set up: You have the Sun JDK (or OpenJDK) installed with javac and friends in your $PATH. You do not have $CLASSPATH set. And you have a bunch of jar files sitting in a directory, say, /usr/share/java. And you have written a class Foo.java in your home directory that uses a class in one of the jar files.

Q: Which of the following commands will successfully compile Foo.java:

  1. javac Foo.java
  2. export CLASSPATH=`echo /usr/share/java/* | tr ' ' :` ; javac Foo.java
  3. javac -classpath ".:/usr/share/java/*" Foo.java

You are allowed to check the man page for javac, but don't start to try out the commands before you have read the man page.

The command shown are Linux commands and as shown, won't work on Windows. Windows users should try the Windows equivalents of the above.

Tags :