Writing correct concurrent code is difficult, but not impossible. Using good object-oriented design techniques can make it easier.
Here I listed most useful tips together and hoepfully you don’t fear threads any more.

Basic Lesson: Concurrency

1. ThreadPoolExecutor (Thread pool)
– Maintains caller-specified work queue
– Manages thread pool size and use depending upon queue type and contents, and keepAlive parameter
– Easy instantiation through Executors factory class
– Work queue is implementation of BlockingQueue interface: LinkedBlockingQueue, PriorityBlockingQueue, ArrayBlockingQueue, SynchronousQueue are provided by lib.

1.1 JDK API
1.2 Thread pools and work queues
1.3 A Thread Pool Pattern in Java
1.4 Java 5 Executors: ThreadPool

1.5 How to make your own Thread Pool
– create your ThreadPoolExecutor that includes specific BlockingQueue and Comparator.
– override the beforeExecute and afterExecute
– create your ThreadFactory that implement uncaughtException to prevent thread dead problem

class MyThreadPoolExecutor extends ThreadPoolExecutor{
	protected MyThreadPoolExecutor(){
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue(maxCapacity,new MyComparator()),new MyThreadFactory());
	}
	@Override
	protected void beforeExecute(Thread t,Runnable r){
		/*do your stuff*/
		super.beforeExecute(t, r);
	}
	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		/*do your stuff*/
		super.afterExecute(r, t);
	}

}
	
class MyThreadFactory implements ThreadFactory {
	private final MyThreadGroup tg = new MyThreadGroup();
	    public Thread newThread(Runnable r) {
			     return new Thread(tg,r);
			}
	}
	private class MyThreadGroup extends ThreadGroup{
		private MyThreadGroup(){
			super("MyThreadGroup");
		}
		public void uncaughtException(Thread t, Throwable e){
			log.debug(t);
			/*do something*/				
		}
}

2. Thread and UncaughtException

2.1 UncaughtExceptionHandler
2.2 Hey, where’d my thread go?

3. Collections and Thread

3.1 JDK ConcurrentHashMap
3.2 JDK ConcurrentSkipListSet
3.3 Concurrent collections classes
3.4 What is new in Java 6.0 Collections API?
3.5 Correct use of ConcurrentHashMap
3.6 Concurrent structures and collections in Java 5

Reference: Understand Skip List Map/Set
3.7 Java Ordered Collections: Trees and Skip Lists
3.8 Skip lists

4. Thread Scheduling
The JVM schedules using a preemptive , priority based scheduling algorithm.
All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM.

4.1 JAVA Thread Scheduling
4.2 Course online about OS and Thread
4.3 Achieve strong performance with threads, Part 3
4.4 Thread Scheduling
4.5 Thread Priorities and Thread Scheduling

5. Collections synchronizedList and synchronizedMap

5.1 JDK API
5.2 When A Synchronized Class Isn’t Threadsafe
5.3 Best way to control concurrent access to Java collections

6. ThreadLocal
A thread-local variable effectively provides a separate copy of its value for each thread that uses it.
Each thread can see only the value associated with that thread, and is unaware that other threads may be using or
modifying their own copies.

6.1 Using Thread-Local Variables In Java
6.2 Threading lightly, Part 3: Sometimes it’s best not to share
6.3 Using ThreadLocal and Servlet Filters to cleanly access JPA an EntityManager
6.4 Thread-local variables in Java
6.5 Introduction to reducing thread contention in Java

Advertisements