开发者

Examples of good multithreaded Java code? [closed]

开发者 https://www.devze.com 2022-12-18 16:09 出处:网络
开发者_StackOverflow As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will like
开发者_StackOverflow As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

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

Thanks, Abhinav.


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.


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

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

All are both entertaining and extremely informative.

Where else but in the Overstocked Haberdashery will you find code like:

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 CountDownLatch and an AtomicInteger and synchronized(this) and handles an InterruptedException apropriately, he even uses a double brace initialiser to start the thread!! Now if that is not epic java what is?


Doug Lea's Concurrent Doubly Linked List is an excellent example of Lock Free coding.


Best tutorial about concurreny in Java ever

The Java Memory Model


In the concurrency tutorial you 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 Jackrabbit

0

精彩评论

暂无评论...
验证码 换一张
取 消