Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
In my application, I have a key-value map that serves as a central repository for storing data that is used to return to a defined state after a crash or restart (checkpointing). The application is multithreaded and several threads may put key-value pairs into that map. One thread is responsible for regularly creating a checkpoint, i. e. serialize the map to persistant storage. While the chec...
I have been reading for concurency since yesterday and i dont know much things... However some things are starting to getting clear... I understand why double check locking isnt safe (i wonder what is the propability the rare condition to occur) but volatile fixes the issue in 1.5 +.... But i wonder if this occurs with putifAbsent like... myObj = new myObject("CodeMonkey"); cHashM.putIfAbse...
Suppose we are developing class which implements simple CRUD operations for working with DB. This class also maintain cache for increasing performance. public class FooTableGateway { Map<Integer, Foo> id2foo = new HashMap<Integer, Foo> (); public void getFoo (int id) { if (id2foo.containsKey (id) { return id2foo.get (id); } String query = "select ...
I'm doing a server/client program in Java using RMI. When server crashes it's not a problem, clients get a RemoteException and disconnects. However I have problems when clients crashes. My server uses a Timer to ping all client objects every now and then, when it gets no connection to a client it will catch a RemoteException. Then, it's supposed to remove the client object from the server (ju...
In C++, I can look up a key in a map and insert it if it's not there for the cost of a single look up. Can I do the same in Java? Update: (For those of you who must see code.) long id = 0xabba; int version = 0xb00b; for (List<Object> key : keys) { if (!index.containsKey(key)) { index.put(key, Maps.<Long,Integer>newHashMap()); } index.get(key).put(id, version);...
I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap. I need to make this code thread safe and avoid unnecessary calls to initialize the shared resource. Here's the buggy code: private ConcurrentMap<String, Resource> map; // ..... String key = "...
Hopefully the code itself explains the issue here: class Server { public void main() { // ... ServerSocket serverSocket = new ServerSocket(PORT); while (true) { Socket socket = serverSocket.accept(); Thread thread = new Thread(new Session(socket)); thread.start(); } // .. } public static synchronized Session findByUser(String user) { ...
I need to create a Custom Hashtable extends java.lang.Hashtable and i need to override the get method to achieve the following behavior : if the key == null, it will return a new object of the type V if the super.get(key) == null, it will also return a new object of type V. Can anyone help me. I try to do this but I know it's wrong. import java.util.Hashtable; public class CustomHashtabl...
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
   *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 /*
  * This file is available under and governed by the GNU General Public
  * License version 2 only, as published by the Free Software Foundation.
  * However, the following notice accompanied the original version of this
  * file:
  *
  * Written by Doug Lea with assistance from members of JCP JSR-166
  * Expert Group and released to the public domain, as explained at
  * http://creativecommons.org/licenses/publicdomain
  */
 
 package java.util.concurrent;
 import java.util.Map;

A java.util.Map providing additional atomic putIfAbsent, remove, and replace methods.

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a ConcurrentMap as a key or value happen-before actions subsequent to the access or removal of that object from the ConcurrentMap in another thread.

This interface is a member of the Java Collections Framework.

Parameters:
<K> the type of keys maintained by this map
<V> the type of mapped values
Author(s):
Doug Lea
Since:
1.5
 
 public interface ConcurrentMap<K, V> extends Map<K, V> {
    
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to
   if (!map.containsKey(key))
       return map.put(key, value);
   else
       return map.get(key);
except that the action is performed atomically.

Parameters:
key key with which the specified value is to be associated
value value to be associated with the specified key
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)
Throws:
java.lang.UnsupportedOperationException if the put operation is not supported by this map
java.lang.ClassCastException if the class of the specified key or value prevents it from being stored in this map
java.lang.NullPointerException if the specified key or value is null, and this map does not permit null keys or values
java.lang.IllegalArgumentException if some property of the specified key or value prevents it from being stored in this map
 
     V putIfAbsent(K key, V value);

    
Removes the entry for a key only if currently mapped to a given value. This is equivalent to
   if (map.containsKey(key) && map.get(key).equals(value)) {
       map.remove(key);
       return true;
   } else return false;
except that the action is performed atomically.

Parameters:
key key with which the specified value is associated
value value expected to be associated with the specified key
Returns:
true if the value was removed
Throws:
java.lang.UnsupportedOperationException if the remove operation is not supported by this map
java.lang.ClassCastException if the key or value is of an inappropriate type for this map (optional)
java.lang.NullPointerException if the specified key or value is null, and this map does not permit null keys or values (optional)
    boolean remove(Object keyObject value);

    
Replaces the entry for a key only if currently mapped to a given value. This is equivalent to
   if (map.containsKey(key) && map.get(key).equals(oldValue)) {
       map.put(key, newValue);
       return true;
   } else return false;
except that the action is performed atomically.

Parameters:
key key with which the specified value is associated
oldValue value expected to be associated with the specified key
newValue value to be associated with the specified key
Returns:
true if the value was replaced
Throws:
java.lang.UnsupportedOperationException if the put operation is not supported by this map
java.lang.ClassCastException if the class of a specified key or value prevents it from being stored in this map
java.lang.NullPointerException if a specified key or value is null, and this map does not permit null keys or values
java.lang.IllegalArgumentException if some property of a specified key or value prevents it from being stored in this map
    boolean replace(K key, V oldValue, V newValue);

    
Replaces the entry for a key only if currently mapped to some value. This is equivalent to
   if (map.containsKey(key)) {
       return map.put(key, value);
   } else return null;
except that the action is performed atomically.

Parameters:
key key with which the specified value is associated
value value to be associated with the specified key
Returns:
the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.)
Throws:
java.lang.UnsupportedOperationException if the put operation is not supported by this map
java.lang.ClassCastException if the class of the specified key or value prevents it from being stored in this map
java.lang.NullPointerException if the specified key or value is null, and this map does not permit null keys or values
java.lang.IllegalArgumentException if some property of the specified key or value prevents it from being stored in this map
    V replace(K key, V value);
New to GrepCode? Check out our FAQ X