@Override
 public void run(String... arg0) throws Exception {
   dictionary.set("java", "object oriented");
   /*dictionary.set("linux", "rulez");
   dictionary.set("mac", "beutifull");
   dictionary.set("solaris", "old but gold");
   dictionary.set("red hat", "new and fit");
   dictionary.set("debian", "just old");*/
   Writer writer = new Writer(dictionary, "Mr. Writer");
   Reader reader1 = new Reader(dictionary, "Mrs Reader 1");
   Reader reader2 = new Reader(dictionary, "Mrs Reader 2");
   Reader reader3 = new Reader(dictionary, "Mrs Reader 3");
   Reader reader4 = new Reader(dictionary, "Mrs Reader 4");
   Reader reader5 = new Reader(dictionary, "Mrs Reader 5");
   log.info("Inciando thread de writer");
   writer.setPriority(Thread.MAX_PRIORITY);
   writer.start();
   log.info("Inciando thread de reader Mrs Reader 1");
   reader1.start();
   log.info("Inciando thread de reader Mrs Reader 2");
   reader2.start();
   log.info("Inciando thread de reader Mrs Reader 3");
   reader3.start();
   log.info("Inciando thread de reader Mrs Reader 4");
   reader4.start();
   log.info("Inciando thread de reader Mrs Reader 5");
   reader5.start();
 }
  public void testMultiThreaded() throws InterruptedException {
    int NUM_THREADS = 20;
    Writer[] w = new Writer[NUM_THREADS];
    CountDownLatch startLatch = new CountDownLatch(1);

    for (int i = 0; i < NUM_THREADS; i++) w[i] = new Writer(i, startLatch);
    for (Writer writer : w) writer.start();

    startLatch.countDown();

    Thread.sleep(250);

    // now stop writers
    for (Writer writer : w) writer.running = false;
    for (Writer writer : w) writer.join();

    // wait for the cache size to drop to CACHE_SIZE, up to a specified amount of time.
    long giveUpTime = System.currentTimeMillis() + (1000 * 10); // 10 sec
    while (cache.getAdvancedCache().getDataContainer().size() > 1
        && System.currentTimeMillis() < giveUpTime) {
      // System.out.println("Cache size is " + cache.size() + " and time diff is " + (giveUpTime -
      // System.currentTimeMillis()));
      Thread.sleep(100);
    }

    assert cache.getAdvancedCache().getDataContainer().size() <= CACHE_SIZE
        : "Expected 1, was "
            + cache.size(); // this is what we expect the cache to be pruned to
  }
Exemplo n.º 3
0
  public void _testPut() throws Exception {

    // put any stuff
    List<NodeData> nodes = prepare();

    Set<Writer> writers = new HashSet<Writer>();
    try {
      // create readers
      for (int t = 1; t <= 100; t++) {
        NodeData[] ns = new NodeData[nodes.size()];
        nodes.toArray(ns);
        Writer r = new Writer(ns, "writer #" + t, 50);
        writers.add(r);
        r.start();
      }

      Thread.sleep(5 * 60 * 1000);
    } finally {
      // join

      for (Writer w : writers) {
        w.cancel();
        w.join();
      }

      // debug result
      for (Writer w : writers) {
        log.info(w.getName() + " " + (w.itemsProcessed));
      }
    }
  }
 private void testReadWrite(Random rng, Writer wr) throws InterruptedException {
   ArrayBlockingQueue<byte[]> q = newQueue();
   InputStream is = create(q);
   wr.init(q);
   wr.start();
   Reader rd = new Reader(rng, is);
   rd.run();
   wr.join();
   Assert.assertNull(wr.exn);
   Assert.assertNull(rd.exn);
   System.out.printf("sums %x -> %x\n", wr.getSum(), rd.getSum());
   Assert.assertEquals(wr.getSum(), rd.getSum());
 }
Exemplo n.º 5
0
  public void _testGetPutRemove() throws Exception {

    List<NodeData> nodes = prepare();

    Set<Reader> readers = new HashSet<Reader>();
    Set<Writer> writers = new HashSet<Writer>();
    Set<Remover> removers = new HashSet<Remover>();
    long start = System.currentTimeMillis();
    try {
      // create readers
      for (int t = 1; t <= 100; t++) {
        NodeData[] ns = new NodeData[nodes.size()];
        nodes.toArray(ns);
        Reader r = new Reader(ns, "reader #" + t);
        readers.add(r);
        r.start();
      }

      // create writers
      for (int t = 1; t <= 5; t++) {
        NodeData[] ns = new NodeData[nodes.size()];
        nodes.toArray(ns);
        Writer w = new Writer(ns, "writer #" + t, 1000);
        writers.add(w);
        w.start();
      }

      // create removers
      for (int t = 1; t <= 5; t++) {
        NodeData[] ns = new NodeData[nodes.size()];
        nodes.toArray(ns);
        Remover r = new Remover(ns, "remover #" + t, 1000);
        removers.add(r);
        r.start();
      }

      log.info("Wait....");

      // Thread.sleep(50400 * 1000); // 50400sec = 14h
      Thread.sleep(20 * 1000); // 20sec.

      log.info("Stopping");
    } finally {
      // join
      for (Remover r : removers) {
        r.cancel();
        r.join();
      }

      for (Writer w : writers) {
        w.cancel();
        w.join();
      }

      for (Reader r : readers) {
        r.cancel();
        r.join();
      }

      // debug result
      long stop = System.currentTimeMillis() - start;
      long totalRead = 0;
      for (Reader r : readers) {
        totalRead += r.itemsProcessed;
        // log.info(r.getName() + " " + (r.itemsProcessed));
      }

      for (Writer w : writers) {
        totalRead += w.itemsProcessed;
        // log.info(w.getName() + " " + (w.itemsProcessed));
      }

      for (Remover r : removers) {
        totalRead += r.itemsProcessed;
        // log.info(r.getName() + " " + (r.itemsProcessed));
      }

      double speed = totalRead * 1d / stop;
      log.info(
          "Total accessed "
              + totalRead
              + ", speed "
              + speed
              + " oper/sec., time "
              + (stop / 1000d)
              + "sec");
    }
  }