I want to learn some good multi-threaded Java code. Can anyone suggest some examples? Is the Apache web server a good choice?
Thanks, Abhinav.
I would recommend you take a look at this book . It covers almost everything about Java and concurrency / multithreading, including coding principles and many examples.
I would strongly recommend that you read - at least twice - (I'm reading the 4th reading now) excellent Concurrency Secrets that Dr. Heinz M. Kabutz generously unveiled his website.
Topics include:
Sabotage Doorbell ActAct of Distracted SpearfishermanThe law of crowded haberdasheryBlind Spot LawLaw of Leakage MemosLaw of a corrupt politicianMicromanager LawThe Law of Cretan DrivingLaw of sudden wealthUneaten Lutefisk ActXerox Law Xerox
All of them are interesting and extremely informative.
Where else, but in Overstocked Haberdashery you will 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 uses not only CountDownLatch and , but AtomicInteger and synchronized(this) and handles InterruptedException he even uses double brace initialiser to start the stream !! Now, if it's not epic java , what is it?
CountDownLatch
AtomicInteger
synchronized(this)
InterruptedException
double brace initialiser
Best concurreny tutorial in Java ever
Java memory model
Doug Lea A shared double-linked list is a great example of Lock Free coding.
Lock Free
In the concurrency tutorial you will find aspects such as
are being discussed. If you do not understand how this is used in a real application, check out Jackrabbit