Example #1
0
 public void clearAllMessages() {
   if (messages.size() > 0) {
     Debug.warn(
         "Message inbox of "
             + username
             + " contained "
             + messages.size()
             + " extra messages, discarding...");
   }
   messages.clear();
 }
  @TaskAction
  public void doTask()
      throws ParserConfigurationException, SAXException, IOException, InterruptedException {
    File out = new File(getAssetsDir(), "objects");
    out.mkdirs();

    AssetIndex index = getIndex();

    for (Entry<String, AssetEntry> e : index.objects.entrySet()) {
      Asset asset = new Asset(e.getValue().hash, e.getValue().size);
      File file = new File(out, asset.path);

      // exists but not the right size?? delete
      if (file.exists() && file.length() != asset.size) file.delete();

      // does the file exist (still) ??
      if (!file.exists()) filesLeft.offer(asset);
    }

    getLogger().info("Finished parsing JSON");
    int max = filesLeft.size();
    getLogger().info("Files Missing: " + max + "/" + index.objects.size());

    // get number of threads
    int threadNum = max / 100;
    if (threadNum == 0 && max > 0) threadNum++; // atleats 1 thread

    // spawn threads
    for (int i = 0; i < threadNum; i++) spawnThread();

    getLogger().info("Threads initially spawned: " + threadNum);

    while (stillRunning()) {
      int done = max - filesLeft.size();
      getLogger()
          .lifecycle(
              "Current status: "
                  + done
                  + "/"
                  + max
                  + "   "
                  + (int) ((double) done / max * 100)
                  + "%");
      spawnThread();
      Thread.sleep(1000);
    }

    if (errored) {
      // CRASH!
      getLogger().error("Something went wrong with the assets downloading!");
      this.setDidWork(false);
      return;
    }
  }
  public void doFactLeak() throws InterruptedException {
    // DROOLS-131
    String drl =
        "package org.drools.test; \n"
            + "global "
            + ConcurrentLinkedQueue.class.getCanonicalName()
            + " list; \n"
            +
            // "global " + AtomicInteger.class.getCanonicalName() + "counter; \n" +
            ""
            + "rule Intx when\n"
            + " $x : Integer() from entry-point \"x\" \n"
            + "then\n"
            + " list.add( $x );"
            + "end";
    int N = 1100;

    KnowledgeBase kb = loadKnowledgeBaseFromString(drl);
    final StatefulKnowledgeSession ks = kb.newStatefulKnowledgeSession();
    ConcurrentLinkedQueue list = new ConcurrentLinkedQueue<Integer>();
    AtomicInteger counter = new AtomicInteger(0);
    ks.setGlobal("list", list);
    // ks.setGlobal( "counter", counter );

    new Thread() {
      public void run() {
        ks.fireUntilHalt();
      }
    }.start();

    for (int j = 0; j < N; j++) {
      ks.getEntryPoint("x").insert(new Integer(j));
    }

    int count = 0;
    while (list.size() != N && count++ != 1000) {
      Thread.sleep(200);
    }

    ks.halt();
    if (list.size() != N) {
      for (int j = 0; j < N; j++) {
        if (!list.contains(new Integer(j))) {
          System.out.println("missed: " + j);
        }
      }
    }

    assertEquals(N, list.size());
  }
Example #4
0
  public static void main(String[] args) {

    Thread[] threads = new Thread[LIST_SIZE];
    ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

    for (int i = 0; i < LIST_SIZE; i++) {
      threads[i] = new Thread(new AddTask(queue));
      threads[i].start();
    }
    for (int i = 0; i < LIST_SIZE; i++) {
      try {
        threads[i].join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("AddTAsks has add");

    for (int i = 0; i < LIST_SIZE; i++) {
      threads[i] = new Thread(new PollTask(queue));
      threads[i].start();
    }
    for (int i = 0; i < LIST_SIZE; i++) {
      try {
        threads[i].join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("PollTasks has add");

    System.out.println("The size of list is " + queue.size());
  }
Example #5
0
 public String toString() {
   return "RecordsRotator{"
       + "startWhenIteration="
       + startWhenIteration
       + ", min="
       + min
       + ", max="
       + max
       + ", current="
       + current
       + ", queue="
       + queue.size()
       + ", minSize="
       + minSize
       + ", runThroughDataSize="
       + runThroughDataSize
       + ", fetchDelay="
       + fetchDelay
       + ", noDataDelay="
       + noDataDelay
       + ", insufficientDataDelay="
       + insufficientDataDelay
       + ", fetchSize="
       + fetchSize
       + ", fillTries="
       + fillTries
       + ", running="
       + running
       + ", maxLogsCount="
       + maxLogsCount
       + ", logs="
       + logs
       + '}';
 }
  /** stop all streams from being cast to the server */
  void doUnpublish() {
    for (String key : mLocalStream.keySet()) {
      final StreamDescription stream = mLocalStream.get(key);
      if (stream != null && stream.isLocal()) {
        stream.pc.removeStream(lMS);

        for (RoomObserver obs : mObservers) {
          obs.onStreamRemoved(stream);
        }

        if (mObservers.size() == 0) {
          destroy(stream);
        }
      }
    }
    mLocalStream.clear();

    if (lMS != null) {
      lMS.dispose();
    }
    if (mVideoCapturer != null) {
      mVideoCapturer.dispose();
    }

    lMS = null;
    mVideoCapturer = null;
    if (mVideoSource != null && !mVideoStopped) {
      mVideoSource.stop();
    }
    mVideoSource = null;
  }
Example #7
0
  /** Flush any caches to the database TODO */
  private void _flush() {
    // periodically update protections in the database if a non-critical change was made
    if (protectionUpdateQueue.size() > 0) {
      Connection connection = lwc.getPhysicalDatabase().getConnection();
      Protection protection = null;

      try {
        connection.setAutoCommit(false);
      } catch (SQLException e) {
        e.printStackTrace();
      }

      /*
       * Loop through
       */
      while ((protection = protectionUpdateQueue.poll()) != null) {
        protection.saveNow();
      }

      /*
       * Commit
       */
      try {
        connection.commit();
        connection.setAutoCommit(true);
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

    flush = false;
    lastUpdate = System.currentTimeMillis();
  }
  /**
   * Removes any tasks waiting to be run. Will not interrupt any tasks currently running if {@link
   * #tick(ExceptionHandlerInterface)} is being called. But will avoid additional tasks from being
   * run on the current {@link #tick(ExceptionHandlerInterface)} call.
   *
   * <p>If tasks are added concurrently during this invocation they may or may not be removed.
   *
   * @return List of runnables which were waiting in the task queue to be executed (and were now
   *     removed)
   */
  public List<Runnable> clearTasks() {
    List<TaskContainer> containers;
    synchronized (scheduledQueue.getModificationLock()) {
      containers = new ArrayList<TaskContainer>(executeQueue.size() + scheduledQueue.size());

      Iterator<? extends TaskContainer> it = executeQueue.iterator();
      while (it.hasNext()) {
        TaskContainer tc = it.next();
        /* we must use executeQueue.remove(Object) instead of it.remove()
         * This is to assure it is atomically removed (without executing)
         */
        if (!tc.running && executeQueue.remove(tc)) {
          int index = ListUtils.getInsertionEndIndex(containers, tc, true);
          containers.add(index, tc);
        }
      }

      it = scheduledQueue.iterator();
      while (it.hasNext()) {
        TaskContainer tc = it.next();
        if (!tc.running) {
          int index = ListUtils.getInsertionEndIndex(containers, tc, true);
          containers.add(index, tc);
        }
      }
      scheduledQueue.clear();
    }

    return ContainerHelper.getContainedRunnables(containers);
  }
Example #9
0
  public static void release(SessionRemote sr) {
    if (sr == null || sr.isClosed()) return;

    ConcurrentLinkedQueue<SessionRemote> queue = getQueue(sr.getURL());
    if (queue.size() > corePoolSize) sr.close();
    else queue.offer(sr);
  }
Example #10
0
  /** Transport connected. {@link IOTransport} calls this when a connection is established. */
  public void transportConnected() {

    setState(STATE_READY);
    if (reconnectTask != null) {
      reconnectTask.cancel();
      reconnectTask = null;
    }
    resetTimeout();
    synchronized (outputBuffer) {
      if (transport.canSendBulk()) {
        ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer;
        this.outputBuffer = new ConcurrentLinkedQueue<String>();
        try {
          // DEBUG
          String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]);
          log.debug("Bulk start:");
          for (String text : texts) {
            log.debug("> " + text);
          }
          log.debug("Bulk end");
          // DEBUG END
          transport.sendBulk(texts);
        } catch (IOException e) {
          this.outputBuffer = outputBuffer;
        }
      } else {
        String text;
        while ((text = outputBuffer.poll()) != null) {
          sendPlain(text);
        }
      }
      this.keepAliveInQueue = false;
    }
  }
  protected ReceiveStatus fetch(
      List<? super M> list,
      Duration timeoutDuration,
      FunctionThrowsExceptions<java.util.Queue<Message>, Boolean> fetcher)
      throws DataStreamInfrastructureException, InterruptedException {
    ConcurrentLinkedQueue<Message> fetched = new ConcurrentLinkedQueue<>();
    boolean outOfRangeReached = false;
    try {
      outOfRangeReached = fetcher.apply(fetched);
    } catch (TimeoutException | UncheckedTimeoutException e) {
      // do nothing
    } catch (InterruptedException e) {
      throw e;
    } catch (JMSException e) {
      throw new DataStreamInfrastructureException(e);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }

    int fetchedCount =
        fetched.size(); // the working thread may still being adding new element to fetched
    if (fetchedCount > 0) {
      Message msg = null;
      while (fetchedCount-- > 0) {
        msg = fetched.remove();
        list.add(convert(msg));
      }
      return new SimpleReceiveStatus(position(msg), enqueuedTime(msg), outOfRangeReached);
    } else {
      return new SimpleReceiveStatus(null, null, outOfRangeReached);
    }
  }
Example #12
0
 /**
  * Returns an instance of a specific checksum algorithm for caching.
  *
  * @param instance of the algorithm
  */
 public void returnAlgorithm(ChecksumAlgorithm algorithm) {
   ConcurrentLinkedQueue<ChecksumAlgorithm> cache = pool.get(algorithm.getName());
   if (cache.size() < MAX_CACHE_SIZE) {
     algorithm.reset();
     cache.add(algorithm);
   }
 }
Example #13
0
 private void cacheNotification(ApnsNotification notification) {
   cachedNotifications.add(notification);
   while (cachedNotifications.size() > cacheLength) {
     cachedNotifications.poll();
     logger.debug("Removing notification from cache " + notification);
     UILogger.getInstance().add("Removing notification from cache " + notification);
   }
 }
Example #14
0
 public void restore(TileGenerator tileGenerator) {
   if (mPool.size() < mMaxSize && mPool.offer(tileGenerator)) {
     return;
   }
   // pool is too big or returning to pool failed, so just try to clean
   // up.
   tileGenerator.cleanUp();
 }
 /** triggers the event that a stream was removed */
 void triggerStreamRemoved(StreamDescription stream) {
   for (RoomObserver obs : mObservers) {
     obs.onStreamRemoved(stream);
   }
   if (mObservers.size() == 0) {
     destroy(stream);
   }
 }
Example #16
0
 @Override
 public boolean offer(E e) {
   if (queue.size() > capacity) {
     queue.poll();
   }
   queue.offer(e);
   return true;
 }
Example #17
0
  public TrPeerInfo getPeerForAssimilation() {
    if (peers.isEmpty()) {
      // We need to use a public peer
      final ArrayList<File> publicNodeIdFiles = node.getPublicNodeIdFiles();
      final File pubPeerFile =
          publicNodeIdFiles.get(TrUtils.rand.nextInt(publicNodeIdFiles.size()));
      final TrPeerInfo pnii = Persistence.loadReadOnly(TrPeerInfo.class, pubPeerFile);
      return pnii;
    } else {
      /**
       * Here we use a trick to pick peers in proportion to the probability that they will be the
       * fastest peer
       */
      TrPeerInfo bestPeer = null;
      double bestTimeEstimate = Double.MAX_VALUE;
      final LinearStat globalSuccessTime = new LinearStat(Integer.MAX_VALUE);
      globalSuccessTime.sample(1000);
      globalSuccessTime.sample(2000);
      for (final TrPeerInfo ifo : peers.values()) {
        if (ifo.assimilation.successTimeSqrt.total > 0) {
          globalSuccessTime.sample(ifo.assimilation.successTimeSqrt.mean());
        }
      }
      for (final Entry<PhysicalNetworkLocation, TrPeerInfo> e : peers.entrySet()) {
        final double guessFailureProb = e.getValue().assimilation.successRate.getBetaRandom();
        double guessSuccessTime;
        // If we don't have at least 2 samples, use our global success
        // time
        if (e.getValue().assimilation.successTimeSqrt.total > 2) {
          final double guessSuccessTimeSqrt =
              e.getValue().assimilation.successTimeSqrt.getNormalRandom();
          guessSuccessTime = guessSuccessTimeSqrt * guessSuccessTimeSqrt;
        } else {
          final double guessSuccessTimeSqrt = globalSuccessTime.getNormalRandom();
          guessSuccessTime = guessSuccessTimeSqrt * guessSuccessTimeSqrt;
        }
        double timeEstimate =
            guessSuccessTime
                + AssimilateSessionImpl.RELAY_ASSIMILATION_TIMEOUT_SECONDS
                    * 1000l
                    * guessFailureProb;

        if (lastAttemptedRelays.contains(e.getValue())) {
          timeEstimate *= RECENTLY_ATTEMPTED_PENALTY;
        }

        if (timeEstimate < bestTimeEstimate) {
          bestPeer = e.getValue();
          bestTimeEstimate = timeEstimate;
        }
      }
      lastAttemptedRelays.add(bestPeer);
      while (lastAttemptedRelays.size() > 5) {
        lastAttemptedRelays.poll();
      }
      return bestPeer;
    }
  }
Example #18
0
 private synchronized void waitForData() {
   if (queue.size() == 0 && !stop) {
     try {
       wait(5000L);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
  public void addSavingOperation(WiFiApConfig... config) {
    saveOperations.addAll(Arrays.asList(config));
    Timber.d("Save operations: %d (after ADD)", saveOperations.size());

    Intents.callIntent(context, Intents.PROXY_REFRESH_UI);

    Intent serviceIntent = new Intent(context, SaveWifiNetworkService.class);
    context.startService(serviceIntent);
  }
 /**
  * 是否有下载任务
  *
  * @return
  */
 public boolean hasDownloadTask() {
   if (mDownloadingTasks.size() > 0
       || mPausingTasks.size() > 0
       || mWaitTasks.size() > 0
       || mCompleteTasks.size() > 0) {
     return true;
   }
   return false;
 }
  /*
   * Process a sync request
   */
  public synchronized void sync() {
    if (pendingSyncs.size() == 0) {
      LOG.warn("Not expecting a sync.");
      return;
    }

    Request r = pendingSyncs.remove();
    commitProcessor.commit(r);
  }
 /** Send the email. */
 public void sendEmail(Email outgoing) {
   LOG.trace("ENTER");
   outgoing.setStatus(Email.STATUS_OUTBOX);
   outbox.add(outgoing);
   if (emailListener != null) {
     emailListener.outgoingEmailEvent(null, outgoing);
   }
   LOG.debug("E-mail added to outbox. Size is [" + outbox.size() + "]");
   LOG.trace("EXIT");
 }
  public int getPriorityOperationExecutorQueueSize() {
    int size = 0;

    for (OperationThread t : partitionOperationThreads) {
      size += t.priorityWorkQueue.size();
    }

    size += genericPriorityWorkQueue.size();
    return size;
  }
Example #24
0
  public synchronized CObj waitForOut() {
    if (outqueue.size() == 0 && !stop) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    return outqueue.poll();
  }
 /** The array readersStateArrayRef will only be rebuild if a new thread is added or removed */
 private State[] rebuildArrayIfNeeded() {
   State[] localReadersStateArray = readersStateArrayRef.get();
   if (localReadersStateArray == null) {
     // Set to dummyArray before scanning the readersStateQ
     readersStateArrayRef.set(dummyArray);
     // Copy the elements in readersStateQ to an array
     localReadersStateArray = readersStateQ.toArray(new State[readersStateQ.size()]);
     readersStateArrayRef.compareAndSet(dummyArray, localReadersStateArray);
   }
   return localReadersStateArray;
 }
Example #26
0
 @Test
 public void testSubscribeOnScheduling() {
   // in a loop for repeating the concurrency in this to increase chance of failure
   for (int i = 0; i < 100; i++) {
     int NUM = (int) (RxRingBuffer.SIZE * 2.1);
     AtomicInteger c = new AtomicInteger();
     ConcurrentLinkedQueue<Thread> threads = new ConcurrentLinkedQueue<Thread>();
     TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
     // observeOn is there to make it async and need backpressure
     incrementingIntegers(c, threads)
         .subscribeOn(Schedulers.computation())
         .observeOn(Schedulers.computation())
         .take(NUM)
         .subscribe(ts);
     ts.awaitTerminalEvent();
     ts.assertNoErrors();
     System.out.println(
         "testSubscribeOnScheduling => Received: "
             + ts.getOnNextEvents().size()
             + "  Emitted: "
             + c.get());
     assertEquals(NUM, ts.getOnNextEvents().size());
     assertTrue(c.get() < RxRingBuffer.SIZE * 4);
     Thread first = null;
     for (Thread t : threads) {
       System.out.println("testSubscribeOnScheduling => thread: " + t);
       if (first == null) {
         first = t;
       } else {
         if (!first.equals(t)) {
           fail("Expected to see the same thread");
         }
       }
     }
     System.out.println(
         "testSubscribeOnScheduling => Number of batch requests seen: " + threads.size());
     assertTrue(threads.size() > 1);
     System.out.println(
         "-------------------------------------------------------------------------------------------");
   }
 }
 private int updateText() {
   String s = mMsg.poll();
   if (s == null) {
     s = "";
     List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
     for (Sensor ss : sensors) {
       s += "Sensor " + ss.getType() + " " + ss.getVendor() + " " + ss.getName() + "\n";
     }
   }
   final String s2 = s;
   runOnUiThread(
       new Runnable() {
         public void run() {
           mText.setText(s2);
         }
       });
   if (mMsg.size() > 15) return 20;
   if (mMsg.size() > 8) return 100;
   if (mMsg.size() > 4) return 500;
   return 1000;
 }
Example #28
0
 /** Copies the next values to the snapshot */
 @Override
 public void copySnapshot() {
   if (dirty.size() > 0) {
     snapshot.clear();
     synchronized (live) {
       for (T o : live) {
         snapshot.add(o);
       }
     }
   }
   dirty.clear();
 }
 private void processNextClientRequest() {
   QueueEntry qe = (QueueEntry) qClientRequests.poll();
   if (qe != null) {
     EventSocket sock = qe.sock;
     String message = qe.message;
     boolean isAllowed = false;
     LockHolder lock = getLockForCommand(message);
     if (lock != null) {
       // if the lock exists check whether the user is allowed to use this command
       int[] privs = lock.getAllowedPrivileges();
       for (int priv : privs) if (priv == sock.getPrivilege()) isAllowed = true;
       if (isAllowed) {
         if (lockHolder == null) { // if no lock is set we try to execute the command
           setLock(lock.setLockAction(message));
           Debg.print("lock set and executed");
         } else { // if a lock is already set the command might contain an abort message
           String cmd = Parser.getValue(message, NanoComm.COMMAND_CMD);
           String[] abortCmds = lockHolder.getAbortCommands();
           boolean isAbortable = false;
           if (abortCmds != null)
             for (int i = 0; i < abortCmds.length; i++) {
               if (cmd.equals(abortCmds[i])) isAbortable = true;
             }
           if (isAbortable) {
             setLock(lockHolder.takeAbortActions("Overwriting command has been sent"));
           } else sock.sendClientInfo("Please be patient, the remote experiment is still busy");
         }
       } else sock.sendClientInfo("Sorry, you are not allowed to use this command!");
     } else Debg.print("No lock found for: " + message);
   }
   if (qClientRequests.size() > 10) {
     Debg.err(
         "I have been flooded by "
             + qClientRequests.size()
             + " client requests! I dropped everything except for the oldest one");
     qe = (QueueEntry) qClientRequests.poll();
     qClientRequests.clear();
     qClientRequests.add(qe);
   }
 }
 public void recordFailMsg(Packet packet, String failReason) {
   if (channelContext.isNeedRecordSendFailMsg()) {
     ConcurrentLinkedQueue<PacketVo> sendFailQueue = getSendFailQueue(true);
     if (sendFailQueue != null) {
       PacketVo packetPojo =
           PacketVo.createPacketVo(packet, SystemTimer.currentTimeMillis(), failReason);
       if (sendFailQueue.size() >= channelContext.getCountOfRecordSendFail()) {
         sendFailQueue.poll();
       }
       sendFailQueue.add(packetPojo);
     }
   }
 }