<< August 14, 2008 | Home | August 16, 2008 >>

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 :