Showing posts with label contoc. Show all posts
Showing posts with label contoc. Show all posts

Wednesday, 22 June 2011

Java 5 Concurrency Utilities - java.util.concurrent

Java Concurrency Utilities - Introduction

Highly Concurrent datastructures
BlockingQueue
BlockingDeque
  • LinkedBlockingDeque
ConcurrentMap
ConcurrentNavigableMap
CountDownLatch
CyclicBarrier
Exchanger
Semaphore

ExecutorService
ThreadPoolExecutor
ScheduledExecutorService

Locks
Lock
ReadWriteLock

Monday, 20 June 2011

Interfaces in java concurrency utils

Java’s concurrency library, java.util.concurrent, has a variety of useful classes to make concurrent code easier to write and more reliable. (If you’re not familiar with that library, you’ll find this post a lot more comprehensible if you read Sun’s tutorial on concurrency.)
There are many ways to put the various building blocks that java.util.concurrent (j.u.c from now on) provides to create concurrent systems. Before we begin, here’s a short reminder of what the most basic j.u.c classes and interfaces are.


Runnable
Intended to be more or less equivalent to starting a Thread. It defines a method that does not return anything and cannot throw checked exceptions.
Callable
A more flexible variant of the same idea. It defines a method that can return a value and throw a checked exception.
Future
Represents the result of a computation (e.g. a Callable). It is used in place of a value that may not be computed yet.
Executor
Accepts Runnables and returns void.
ExecutorService
Extends Executor to also provide a method that accepts a Callable and returns a Future.
Executors
Provides simple methods to get common types of ExecutorService implementations.
Some of them are:
CompletionService

Saturday, 18 June 2011

Synchronizers in java

Few of the recent posts described the use of the various constructs provided by Java 5, through the java.util.concurrent package. The next set describes which scenarios can be solved by the use of each of these constructs. I tried to gather the usage scenarios for the various synchronizers available in Java 5 in this post.
 

These are synchronizers in java -

Brief introduction to above synchronizers

  • Semaphores: A semaphore is the classic method for restricting access to shared resources in a multi-processing environment. While a synchronized block allows only one thread to access a resource, a semaphore allows multiple threads to access a shared resource. Semaphores are often used to restrict the number of threads than can access some resource.
    • Maintaining multiple connections to a database: Define a semaphore, which has same number of permits as there are connections to the database. If all the permits are used, then a thread requesting a connection will be blocked until another thread releases a permit, when this thread may acquire a permit.
    • Binary semaphore can be used in place of any Lock implementation. Such a implementation has an advantage when recovering from deadlocks, since the lock can be unlocked by another thread.
    • When throughput advantages of non-fair ordering often outweigh fairness considerations. Semphores allow barging behaviour. The tryAcquire() method can be used for barging ahead of other threads, irrespective of fairness settings. The tryAcquire(0, TimeUnit.SECONDS) respects fairness setting.
  • Barriers: A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. Scenrarios:
    • Joins: When you join a set of threads and start a new set, there may be a need for each thread to save state at the join point. A cyclic barrier may be used for such a scenario.
  • Latches: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. Usage Scenarios:
    • Divide a problem into N parts, describe each part with a Runnable that executes that portion and counts down on the latch, and queue all the Runnables to an Executor. When all sub-parts are complete, the coordinating thread will be able to pass through await.
    • Latching works well with initialization tasks, where you want no process to run until everything needed is initialized.
  • Exchangers: A synchronization point at which two threads can exchange objects, the condition being that the exchanging threads have to be paired up, and a specific data type must be exchanged. Usage Scenarios:
    • An Exchanger is often used when you have two threads, one consuming a resource, and the other producing it. Similar to the producer/consumer problem, but where the buffer can be in one of only two states - empty or full.

Thursday, 23 September 2010

Threads in java

What Is a Thread?

A thread--sometimes called an execution context or a lightweight process--is a single sequential flow of control within a program. You use threads to isolate tasks. When you run one of these sorting applets, it creates a thread that performs the sort operation. Each thread is a sequential flow of control within the same program (the browser). Each sort operation runs independently from the others, but at the same time.

Customizing a Thread's run Method

First, you need to get a thread to do something by providing the run method for a thread. This section shows you two different ways to do this.

The Life Cycle of a Thread

Once you know how to get a thread to do something, you need to understand the life cycle of a Thread

Understanding Thread Priority

A thread's priority affects when it runs in relation to other threads. This section talks about how this affects your programs.

Synchronizing Threads

The first sample programs in this lesson use either one thread or multiple threads that run asynchronously. However, it is often useful to use multiple threads that share data and therefore must synchronize their activities. In this section you will learn how to synchronize threads and how to avoid problems such as starvation and deadlock.

Grouping Threads

This section shows you how to group threads and what you can do with a group of threads.

Summary

When you've completed this lesson on threads, you will have toured the intricacies of Java threads including the life cycle of a Java thread (as represented by its state), scheduling, thread groups, and synchronization. The Java development environment supports multithreaded programs through the language, the libraries, and the runtime system. This summary page highlights the features in the Java development environment that support threads and gives you links to further documentation about those features.