Flylib.com

Books Software

 
 
 

WeakHashMapK,V


WeakHashMap<K,V> java.util

Java 1.2 collection

This class implements Map using an internal hashtable. It is similar in features and performance to HashMap , except that it uses the capabilities of the java.lang.ref package, so that the key-to-value mappings it maintains do not prevent the key objects from being reclaimed by the garbage collector. When there are no more references to a key object except for the weak reference maintained by the WeakHashMap , the garbage collector reclaims the object, and the WeakHashMap deletes the mapping between the reclaimed key and its associated value. If there are no references to the value object except for the one maintained by the WeakHashMap , the value object also becomes available for garbage collection. Thus, you can use a WeakHashMap to associate an auxiliary value with an object without preventing either the object (the key) or the auxiliary value from being reclaimed. See HashMap for a discussion of the implementation features of this class. See Map for a description of the methods it defines.

WeakHashMap is primarily useful with objects whose equals( ) methods use the == operator for comparison. It is less useful with key objects of type String , for example, because there can be multiple String objects that are equal to one another and, even if the original key value has been reclaimed by the garbage collector, it is always possible to pass a String with the same value to the get( ) method.

Figure 16-69. java.util.WeakHashMap<K,V>

public class

WeakHashMap<K,V>

extends AbstractMap<K,V> implements Map<K,V> {

// Public Constructors

public

WeakHashMap

( );  
     public

WeakHashMap

(int

initialCapacity

);

1.3

public

WeakHashMap

(Map<? extends K,? extends V>

t

);  
     public

WeakHashMap

(int

initialCapacity

, float

loadFactor

);

// Methods Implementing Map

public void

clear

( );  
     public boolean

containsKey

(Object

key

);

1.4

public boolean

containsValue

(Object

value

);  
     public Set<Map.Entry<K,V>>

entrySet

( );  
     public V

get

(Object

key

);  
     public boolean

isEmpty

( );

default:true


1.4

public Set<K>

keySet

( );  
     public V

put

(K

key

, V

value

);

1.4

public void

putAll

(Map<? extends K,? extends V>

m

);  
     public V

remove

(Object

key

);  
     public int

size

( );

1.4

public Collection<V>

values

( );  
}


Package java.util.concurrent

Java 5.0

This package includes a number of powerful utilities for multithreaded programming. Most of these utilities fall into three main categories:


Collections

This package extends the Java Collections Framework, adding the threadsafe classes ConcurrentHashMap , CopyOnWriteArrayList , CopyOnWriteArraySet , and ConcurrentLinkedQueue . These classes achieve threadsafety without relying exclusively on synchronized methods , greatly increasing the number of threads that can safely use them concurrently. ConcurrentHashMap implements the ConcurrentMap interface, which adds important atomic methods to the base java.util.Map interface.

In addition to these Map , List , Set , and Queue implementations , this package also defines the BlockingQueue interface. Blocking queues are important in many concurrent algorithms, and this package provides a variety of useful implementations: ArrayBlockingQueue , DelayQueue , LinkedBlockingQueue , PriorityBlockingQueue , and SynchronousQueue .


Asynchronous Execution with Thread Pools

java.util.concurrent provides a robust framework for asynchronous execution of tasks defined by the existing java.lang.Runnable interface or the new Callable interface. The Executor , ExecutorService , and ScheduledExecutorService interfaces define methods for executing (or scheduling for future execution) Runnable and Callable tasks. The Future interface represents the future result of the asynchronous execution of a task. ThreadPoolExecutor and ScheduledThreadPoolExecutor are executor implementations based on highly configurable thread pools. The Executors class provides convenient factory methods for obtaining instances of these thread pool implementations.


Synchronizers

A number of classes in this package are useful for synchronizing two or more concurrent threads. See CountDownLatch , CyclicBarrier , Exchanger , and Semaphore .

Interfaces

public interface

BlockingQueue

<E> extends java.util.Queue<E>;
public interface

Callable

<V>;
public interface

CompletionService

<V>;
public interface

ConcurrentMap

<K, V> extends java.util.Map<K, V>;
public interface

Delayed

extends Comparable<Delayed>;
public interface

Executor

;
public interface

ExecutorService

extends Executor;
public interface

Future

<V>;
public interface

RejectedExecutionHandler

;
public interface

ScheduledExecutorService

extends ExecutorService;
public interface

ScheduledFuture

<V> extends Delayed, Future<V>;
public interface

ThreadFactory

;

Enumerated Types

public enum

TimeUnit

;

Collections

public class

ArrayBlockingQueue

<E> extends java.util.AbstractQueue<E> 
        implements BlockingQueue<E>, Serializable;
public class

ConcurrentHashMap

<K, V> extends java.util.AbstractMap<K, V>
        implements ConcurrentMap<K, V> Serializable;
public class

ConcurrentLinkedQueue

<E> extends java.util.AbstractQueue<E> 
        implements java.util.Queue<E>, Serializable;
public class

CopyOnWriteArrayList

<E> implements java.util.List<E>, java.util.RandomAccess, Cloneable, Serializable;
public class

CopyOnWriteArraySet

<E> extends java.util.AbstractSet<E> 
        implements Serializable;
public class

DelayQueue

<E extends Delayed> extends java.util.AbstractQueue<E> 
        implements BlockingQueue<E>;
public class

LinkedBlockingQueue

<E> extends java.util.AbstractQueue<E> 
        implements BlockingQueue<E>, Serializable;
public class

PriorityBlockingQueue

<E> extends java.util.AbstractQueue<E> 
        implements BlockingQueue<E>, Serializable;
public class

SynchronousQueue

<E> extends java.util.AbstractQueue<E> 
        implements BlockingQueue<E>, Serializable;

Other Classes

public abstract class

AbstractExecutorService

implements ExecutorService;
   public class

ThreadPoolExecutor

extends AbstractExecutorService;
      public class

ScheduledThreadPoolExecutor

extends ThreadPoolExecutor 
         implements ScheduledExecutorService;
public class

CountDownLatch

;
public class

CyclicBarrier

;
public class

Exchanger

<V>;
public class

ExecutorCompletionService

<V> implements CompletionService<V>;
public class

Executors

;
public class

FutureTask

<V> implements Future<V>, Runnable;
public class

Semaphore

implements Serializable;
public static class

ThreadPoolExecutor.AbortPolicy

implements RejectedExecutionHandler;
public static class

ThreadPoolExecutor.CallerRunsPolicy

implements RejectedExecutionHandler;
public static class

ThreadPoolExecutor.DiscardOldestPolicy

implements RejectedExecutionHandler;
public static class

ThreadPoolExecutor.DiscardPolicy

implements RejectedExecutionHandler;

Exceptions

public class

BrokenBarrierException

extends Exception;
public class

CancellationException

extends IllegalStateException;
public class

ExecutionException

extends Exception;
public class

RejectedExecutionException

extends RuntimeException;
public class

TimeoutException

extends Exception;