好的多线程Java代码的例子?

时间:2010-01-29 08:25:28

标签: java multithreading

我想研究一些好的多线程Java代码。有人可以请一些例子吗? Apache Web服务器是一个不错的选择吗?

谢谢, 阿比纳夫。

5 个答案:

答案 0 :(得分:15)

我建议你看一下这个book。它涵盖了几乎所有关于java和 并发/多线程,包括编码原则和许多例子。

答案 1 :(得分:8)

我强烈建议你阅读 - 至少两次 - (我现在正在读我的第4篇)The secrets of Concurrency在他的网站上慷慨公开的精湛Dr. Heinz M. Kabutz

主题包括:

The Law of the Sabotaged Doorbell
The Law of the Distracted Spearfisherman
The Law of the Overstocked Haberdashery
The Law of the Blind Spot
The Law of the Leaked Memo
The Law of the Corrupt Politician
The Law of the Micromanager
The Law of Cretan Driving
The Law of Sudden Riches
The Law of the Uneaten Lutefisk
The Law of the Xerox Copier

所有这些都是娱乐性和非常丰富的信息。

除了Overstocked Haberdashery之外的其他地方,您会找到如下代码:

public class ThreadCreationTest {
  public static void main(String[] args) throws InterruptedException {
    final AtomicInteger threads_created = new AtomicInteger(0);
    while (true) {
      final CountDownLatch latch = new CountDownLatch(1);
      new Thread() {
        { start(); }
        public void run() {
          latch.countDown();
          synchronized (this) {
            System.out.println("threads created: " +
                threads_created.incrementAndGet());
            try {
              wait();
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
            }
          }
        }
      };
      latch.await();
    }
  }
}

他不仅使用CountDownLatch AtomicInteger synchronized(this) 处理InterruptedException恰当,他甚至使用double brace initialiser开始线程!!现在,如果那不是史诗java 那是什么?

答案 2 :(得分:1)

关于Java中并发性的最佳教程

The Java Memory Model

答案 3 :(得分:1)

Doug Lea的Concurrent Doubly Linked ListLock Free编码的一个很好的例子。

答案 4 :(得分:0)

concurrency tutorial中,您可以找到像

这样的方面
  • 同步
  • 死锁
  • 以及基本概念

讨论。 如果您不想在实际应用程序中使用它,请查看Jackrabbit