好的多线程 Java 代码示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2160708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:14:34  来源:igfitidea点击:

Examples of good multithreaded Java code?

javamultithreading

提问by abhinav

I want to study some good multi-threaded Java code. Could anyone please suggest some examples ? Is Apache web server a good pick ?

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

Thanks, Abhinav.

谢谢,阿比纳夫。

回答by b_erb

I'd recommend you to have a look at this book. It covers almost everything about java and concurrency/multithreading, including coding principles and many examples.

我建议你看看这本书。它几乎涵盖了有关 Java 和并发/多线程的所有内容,包括编码原理和许多示例。

回答by stacker

In the concurrency tutorialyou find the aspects like

并发教程中,您会发现以下方面

  • synchronization
  • deadlocks
  • as well as basic concepts
  • 同步
  • 僵局
  • 以及基本概念

discussed. If you wan't to how this is used in a real application have look at Hymanrabbit

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

回答by helpermethod

Best tutorial about concurreny in Java ever

关于 Java 并发的最佳教程

The Java Memory Model

Java 内存模型

回答by OldCurmudgeon

I would strongly recommend you read - at least twice - (I am on my 4th reading now) the superb The secrets of Concurrencythat Dr. Heinz M. Kabutzhas generously made public on his website.

我强烈建议你阅读-至少两次- (我在我的第四读数现在)精湛并发的秘密亨氏M. Kabutz博士慷慨地在其网站上公布。

Topics include:

主题包括:

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

被破坏的门铃
定律 分心
的鱼叉渔夫
定律杂货店积压定律 盲点
定律 泄露备忘录
定律 腐败政客
定律 微观管理者
定律 克里特岛驾驶
定律 突然定律财富
未食用的 Lutefisk
法则 施乐复印机法则

All are both entertaining and extremely informative.

所有这些都很有趣,而且信息量很大。

Where else but in the Overstocked Haberdasherywill you find code like:

除了在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();
    }
  }
}

where he not only uses a CountDownLatchandan AtomicIntegerandsynchronized(this)andhandles an InterruptedExceptionapropriately, he even uses a double brace initialiserto start the thread!! Now if that is not epic javawhat is?

在那里,他不仅采用了CountDownLatchAtomicIntegersynchronized(this)手柄的InterruptedExceptionapropriately,他甚至使用了double brace initialiser启动线程!现在,如果那不是史诗般的 java是什么?

回答by OldCurmudgeon

Doug Lea's Concurrent Doubly Linked Listis an excellent example of Lock Freecoding.

Doug Lea 的并发双向链表是一个很好的Lock Free编码示例。