Java Collections.synchronizedCollection Examples

Java Collections.synchronizedCollection - 15 examples found. These are the top rated real world Java examples of java.util.Collections.synchronizedCollection extracted from open source projects. You can rate examples to help us improve the quality of examples.
  /**
   * Test concurrent access: First build a clone iterator in a separate thread that hangs
   * momentarily during its construction; then modify the shared collection in this thread. This
   * would cause a ConcurrentModificationException in the other thread if the clone iterator were
   * not synchronized on the original collection.
   */
  public void testConcurrentAccess() throws Exception {
    SlowCollection slow = new SlowCollection();
    this.populateCollection(slow);
    // using the unsynchronized collection will cause the test to fail
    //		this.originalCollection = slow;
    this.originalCollection = Collections.synchronizedCollection(slow);

    this.concurrentProblem = false;
    this.concurrentCollection = new ArrayList();
    Thread thread = new Thread(this.buildRunnable());
    thread.start();
    while (!slow.hasStartedClone()) {
      // wait for the other thread to start the clone...
      Thread.yield();
    }
    // ...then sneak in an extra element
    this.originalCollection.add("seventeen");
    while (thread.isAlive()) {
      // wait for the other thread to finish
      Thread.yield();
    }
    assertFalse(this.concurrentProblem);
    Collection expected = new ArrayList();
    this.populateCollection(expected);
    assertEquals(expected, this.concurrentCollection);
  }
Example #2
0
  /**
   * Adds the value to the collection specified by the key. If there is not a collection for the
   * given key, a new collection is created and added to the hash.
   *
   * @param key The key whose collection we will add value to.
   * @param value The value to be added.
   * @return the collection containing value and all other items associated with the key.
   */
  @SuppressWarnings("unchecked")
  public Collection<V> put(K key, V value) {
    Pair<Collection<V>, O> objectPair = map.get(key);
    if (objectPair == null) {
      try {
        Collection<V> items;
        if (isSynchronized) {
          items = Collections.synchronizedCollection(collectionType.newInstance());
        } else {
          items = collectionType.newInstance();
        }

        objectPair = new Pair(items, plusProvider.newObject());
        map.put(key, objectPair);
      } catch (InstantiationException ex) {
        ex.printStackTrace();
        return null;
      } catch (IllegalAccessException ex) {
        ex.printStackTrace();
        return null;
      }
    }
    objectPair.getFirst().add(value);
    return objectPair.getFirst();
  }
Example #3
0
 public RunThread(Collection<Operation> ops, User u) {
   super("CacheValidatorThread");
   setDaemon(true);
   operations = ops;
   user = u;
   errs = Collections.synchronizedCollection(new RingBuffer<String>(10));
 }
Example #4
0
 public synchronized void start() throws ResourceManagerSystemException {
   logger.info("Starting ResourceManager");
   operationMode = OPERATION_MODE_STARTING;
   globalTransactions = Collections.synchronizedCollection(new ArrayList());
   // TODO: recover and sync
   doStart();
   recover();
   // sync();
   operationMode = OPERATION_MODE_STARTED;
   if (dirty) {
     logger.warn(
         "Started ResourceManager, but in dirty mode only (Recovery of pending transactions failed)");
   } else {
     logger.info("Started ResourceManager");
   }
 }
Example #5
0
    public static class DefaultEventSource implements EventSource {
      private final Collection<Body> mBodies =
          Collections.synchronizedCollection(new ArrayList<Body>());

      @Override
      public final void connect(Body body) {
        mBodies.add(body);
      }

      public final void write(final String data) {
        final Iterator<Body> i = mBodies.iterator();
        while (i.hasNext()) {
          final Body body = i.next();
          if (body.isStopped()) {
            i.remove();
          } else {
            body.writeEventData(data);
          }
        }
      }

      public final void end(final String data) {
        final Iterator<Body> i = mBodies.iterator();
        while (i.hasNext()) {
          final Body body = i.next();
          if (body.isStopped()) {
            i.remove();
          } else {
            body.writeEventData(data);
            body.stop();
            i.remove();
          }
        }
      }

      public final void end() {
        final Iterator<Body> i = mBodies.iterator();
        while (i.hasNext()) {
          final Body body = i.next();
          if (!body.isStopped()) {
            body.stop();
          }
          i.remove();
        }
      }
    }
Example #6
0
 public Pluginer(
     File source,
     File process,
     File destination,
     Downloader coworker,
     ImageProcessingLogger log,
     ArrayList<Plugin> plugins) {
   super(log);
   this.source = source;
   this.process = process;
   this.destination = destination;
   this.coworker = coworker;
   this.plugins = plugins;
   this.filePlugin = new ArrayList<String>();
   this.filePlugin.addAll(Arrays.asList(destination.list()));
   Collections.synchronizedCollection(filePlugin);
 }