java - Is this class Threadsafe? -


hi
class below threadsafe?

class immutablepossiblythreadsafeclass<k, v> {      private final map<k, v> map;      public immutablepossiblythreadsafeclass(final map<k, v> map) {         this.map = new hashmap<k, v>();          (entry<k, v> entry : map.entryset()) {             this.map.put(entry.getkey(), entry.getvalue());         }     }      public v get(k key) {         return this.map.get(key);     } } 

if whole class definition, i.e. there no other setter/modifier methods, usage of map thread safe:

  • its initialization , visibility safeguarded declaring final,
  • the containing class has getter, no modifier methods,
  • the contents of map copied constructor parameter map, there can no outside references through can modified.

(note though refers structure of map - individual elements inside map may still unsafe.)

update

regarding claim class not guarded constructor parameter being concurrently modified during construction, tend agree others (incl. @bozho) have pointed out that

  1. there no known way guard against (not using concurrenthashmap @grundlefleck suggested - checked source code , constructor calls putall(), not guarded against either),
  2. it caller's responsibility rather callee's ensure constructor parameter not being concurrently modified. in fact, can define "right" behaviour processing constructor parameter being concurrently modified? should created object contain original elements in parameter map, or latest elements?... if 1 starts think this, imho not difficult realize consistent solution disallow concurrent modification of constructor parameter, , can ensured caller.

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -