Concurrent Programming을 위한 Java의 Concurrency Utilities

27 Comments

Sun의 Java White Papers에서 Java의 Concurrency Utilites에 대한 글이 있어서 포스팅한다.

Concurrent Programming을 C/C++ 등의 언어에서 생각하면, 일단, legacy code나 reference code 또는 자신이 정리해둔 코드가 없다면 귀차니즘이 발동함과 동시에 (설령 베이스 코드들이 있다고 하더라도) 온갖 에러들 (race condition 등)과 맞서 싸울 준비를 해야한다.

Java에서는 이런 것들이 한결 쉬울 것이다. 별 생각 없이 쉽게 쓴다는 이 점 보다는 좀 더 가치 있는 것을 생각할 시간을 제공해주는 이점이 있다.

아무튼, 여지 껏 Java 보다는 C/C++에 더 친숙해 있고, Java의 thread나 synchronized, timing 등을 별 생각 없이 사용했는데, Java Concurrency Utitlites White Paper를 보니 또 한 번 우물안 개구리를 느끼게 해준다.

Performance를 향상할 수 있고 여러 가지 측면에서 추천하는 방법들을 알 수 있어 유익한 것 같다.

Concurrency Utilities Overview

위 문서 중에서 제공되는 Features들만 나열해보면 다음과 같다.

  • Task Scheduling Framework – The Executor framework is a framework for standardizing invocation, scheduling, execution, and control of asynchronous tasks according to a set of execution policies. Implementations are provided that allow tasks to be executed within the submitting thread, in a single background thread (as with events in Swing), in a newly created thread, or in a thread pool, and developers can create of Executor supporting arbitrary execution policies. The built-in implementations offer configurable policies such as queue length limits and saturation policy which can improve the stability of applications by preventing runaway resource consumption.
  • Concurrent Collections – Several new Collections classes have been added, including the new Queue and BlockingQueue interfaces, and high-performance, concurrent implementations of Map, List, and Queue.
  • Atomic Variables – Classes for atomically manipulating single variables (primitive types or references), providing high-performance atomic arithmetic and compare-and-set methods. The atomic variable implementations in java.util.concurrent.atomic offer higher performance than would be available by using synchronization (on most platforms), making them useful for implementing high-performance concurrent algorithms as well as conveniently implementing counters and sequence number generators.
  • Synchronizers – General purpose synchronization classes, including semaphores, mutexes, barriers, latches, and exchangers, which facilitate coordination between threads.
  • Locks – While locking is built into the Java language via the synchronized keyword, there are a number of inconvenient limitations to built-in monitor locks. The java.util.concurrent.locks package provides a high-performance lock implementation with the same memory semantics as synchronization, but which also supports specifying a timeout when attempting to acquire a lock, multiple condition variables per lock, non-lexically scoped locks, and support for interrupting threads which are waiting to acquire a lock.
  • Nanosecond-granularity timing – The System.nanoTime method enables access to a nanosecond-granularity time source for making relative time measurements, and methods which accept timeouts (such as the BlockingQueue.offer, BlockingQueue.poll, Lock.tryLock, Condition.await, and Thread.sleep) can take timeout values in nanoseconds. The actual precision of System.nanoTime is platform-dependent.