The Artima Developer Community
Sponsored Link

Java Buzz Forum
Friday Java Quiz: Thread Priorities

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Friday Java Quiz: Thread Priorities Posted: Aug 15, 2008 8:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Friday Java Quiz: Thread Priorities
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

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++;
    }
  }
}

Read: Friday Java Quiz: Thread Priorities

Topic: That's Something I Do Previous Topic   Next Topic Topic: IT Management Podcast #17 - Agent Builder/ACE, iPhone Telneting, iLog, RiverMuse, Cloud Hype...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use