Exemplo n.º 1
0
  @Test
  public void testRemoveOneAsynchronous() throws Exception {
    qu = new QuorumUtil(2);
    qu.disableJMXTest = true;
    qu.startAll();
    ZooKeeper[] zkArr = createHandles(qu);

    List<String> leavingServers = new ArrayList<String>();

    // lets remove someone who's not the leader
    leavingServers.add(getLeaderId(qu) == 5 ? "4" : "5");

    LinkedList<Integer> results = new LinkedList<Integer>();

    zkArr[1].reconfig(null, leavingServers, null, -1, this, results);

    synchronized (results) {
      while (results.size() < 1) {
        results.wait();
      }
    }
    Assert.assertEquals(0, (int) results.get(0));

    testNormalOperation(zkArr[1], zkArr[2]);
    for (int i = 1; i <= 5; i++) testServerHasConfig(zkArr[i], null, leavingServers);

    closeAllHandles(zkArr);
  }
Exemplo n.º 2
0
 public void run() {
   while (true) {
     synchronized (JobManagerImpl.this.mySemaphor) {
       if (JobManagerImpl.this.mySemaphor.isClosed()) {
         try {
           JobManagerImpl.this.mySemaphor.wait();
         } catch (InterruptedException e) {
           cleanJobs();
         }
       }
     }
     synchronized (myJobs) {
       if (myJobs.isEmpty()) {
         try {
           myJobs.wait();
         } catch (InterruptedException e) {
           cleanJobs();
         }
       } else {
         InternalJobImpl next = (InternalJobImpl) myJobs.removeFirst();
         IStatus result = next.run(myProgressMonitor);
         next.setResult(result);
         if (result.isOK()) {
         } else {
           cleanJobs();
         }
       }
     }
   }
 }
 private void waitMoreElement() {
   try {
     values.wait(timeout);
   } catch (InterruptedException e) {
     // ignore
   }
 }
Exemplo n.º 4
0
 public void run() {
   try {
     for (; ; ) {
       Target t = null;
       synchronized (pending) {
         while (pending.size() == 0) pending.wait();
         t = (Target) pending.removeFirst();
       }
       t.show();
     }
   } catch (InterruptedException x) {
     return;
   }
 }
 public void printFinishedTargets() {
   // 打印finisedTargets队列中的任务
   try {
     for (; ; ) {
       Target target = null;
       synchronized (finishedTargets) {
         while (finishedTargets.size() == 0) finishedTargets.wait();
         target = (Target) finishedTargets.removeFirst();
       }
       target.show();
     }
   } catch (InterruptedException x) {
     return;
   }
 }
  public HttpUrl dequeueURL() {

    synchronized (toBeAnalyzedQueue) {
      try {
        while (toBeAnalyzedQueue.isEmpty()) {
          toBeAnalyzedQueue.wait();
        }
        return (HttpUrl) toBeAnalyzedQueue.removeFirst();
      } catch (InterruptedException e) {
        return null;
      } catch (NoSuchElementException e) {
        return null;
      }
    }
  }
Exemplo n.º 7
0
 public Runnable getNext() {
   Runnable returnVal = null;
   synchronized (tasks) {
     while (tasks.isEmpty()) {
       try {
         tasks.wait();
       } catch (InterruptedException ex) {
         // System.err.println("Interrupted");
         return null;
       }
     }
     if (tasks.isEmpty()) return null;
     returnVal = tasks.removeFirst();
   }
   return returnVal;
 }
Exemplo n.º 8
0
 /**
  * Waits for tasks to be available on the queue. When a task is available, dequeues it, runs it,
  * and returns to waiting.
  */
 @Override
 public void run() {
   while (!Thread.interrupted()) {
     Task nextTask;
     synchronized (taskQueue) {
       while (taskQueue.isEmpty()) {
         try {
           taskQueue.wait();
         } catch (InterruptedException e) {
           return;
         }
       }
       nextTask = taskQueue.removeFirst();
     }
     nextTask.run();
   }
 }
  /** Run the async dns call in a new thread */
  public void run() {
    while (true) {
      Request req;
      synchronized (queue) {
        if ((queue.size() - waitingThreads <= 0)) {
          try {
            waitingThreads++;
            queue.wait();
          } catch (InterruptedException e) {
            Thread.interrupted();
          }
          waitingThreads--;
        }
        req = (Request) queue.removeFirst();
      }

      Runnable runnable =
          new Runnable() {

            private Request req;

            public void run() {
              IResponseImpl response;
              try {
                response = new IResponseImpl(req.getId(), dnsService.getRecords(req.getValue()));
              } catch (TimeoutException e) {
                response = new IResponseImpl(req.getId(), e);
              }

              req.getResponseQueue().insertResponse(response);
            }

            public Runnable setRequest(Request req) {
              this.req = req;
              return this;
            }
          }.setRequest(req);

      if (multiThread) {
        new Thread(runnable).start();
      } else {
        runnable.run();
      }
    }
  }
Exemplo n.º 10
0
  @Override
  public void run() {
    while (true) {
      synchronized (queue) {
        while (queue.size() >= 100) {
          try {
            System.out.println(curThreadId + " wait " + queue.size());
            queue.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        queue.add(curThreadId + "-" + (i++));
        queue.notifyAll();
      }
    }
  }
Exemplo n.º 11
0
  @SuppressWarnings("unchecked")
  protected void workerLoop() {

    log.debug("workerLoop();");

    if (scsn != null && sessionID != null) {
      startSC();
    }

    synchronized (cmdQueue) {
      while (running) {
        try {
          cmdQueue.wait();
        } catch (InterruptedException e) {
          log.error(e.getMessage(), e);
          return;
        }
        if (!running) return;

        if (!cmdQueue.isEmpty()) {
          // commands to send
          @SuppressWarnings("rawtypes")
          final LinkedList<Command> commands = (LinkedList<Command>) cmdQueue.clone();
          cmdQueue.clear();

          if (!running) return;

          threadPool.background(
              new Runnable() {

                @Override
                public void run() {
                  log.error("run();");
                  try {
                    processRequest(commands);
                  } catch (IOException e) {
                    handleError(e);
                  }
                }
              });
        }
      }
    }
  }
 @Override
 public void run() {
   System.out.println("Connection to the GADM database...");
   Connection conn;
   try {
     conn = GADM.connect_gadm();
   } catch (SQLException e) {
     e.printStackTrace(System.err);
     return;
   }
   System.out.println("Connected, start processing");
   String[] c;
   do {
     synchronized (to_process) {
       if (to_process.isEmpty()) {
         if (stop) break;
         try {
           to_process.wait();
         } catch (InterruptedException e) {
           break;
         }
         continue;
       }
       c = to_process.removeFirst();
     }
     try {
       process(conn, c[0], c[1]);
     } catch (SQLException e) {
       System.err.println("Error processing country " + c[0]);
       e.printStackTrace(System.err);
     }
   } while (true);
   System.out.println("Close GADM connection");
   try {
     conn.close();
   } catch (SQLException e) {
     e.printStackTrace(System.err);
   }
   synchronized (this) {
     stopped = true;
     this.notify();
   }
 }
Exemplo n.º 13
0
  public void run() {

    while (!kill) {
      synchronized (taskQueue) {
        while (taskQueue.isEmpty()) {
          try {
            taskQueue.wait();
          } catch (InterruptedException e) {
          }
        }
        task = taskQueue.poll();
        taskQueue.notify();
      }

      task.doTask();

      task = null;
    }
  }
Exemplo n.º 14
0
 // retourne le prochain imageview a traiter pour le thread interne
 private ImageAndLoader getNextTask() {
   int s;
   synchronized (tasks) {
     s = tasks.size();
   }
   int cs;
   synchronized (cache) {
     cs = cache.size();
   }
   // Log.d(ME, "getNextTask " + s + " todo, cache size is " + cs);
   synchronized (tasks) {
     if (tasks.isEmpty()) {
       try {
         tasks.wait();
       } catch (InterruptedException e) {
         Log.e(ME, "Task interrupted", e);
         stop();
       }
     }
     return tasks.removeFirst();
   }
 }
 /*
  * Handles requests for contact name and number type
  * @see java.lang.Runnable#run()
  */
 public void run() {
   boolean needNotify = false;
   while (!mDone) {
     CallerInfoQuery ciq = null;
     synchronized (mRequests) {
       if (!mRequests.isEmpty()) {
         ciq = mRequests.removeFirst();
       } else {
         if (needNotify) {
           needNotify = false;
           mHandler.sendEmptyMessage(REDRAW);
         }
         try {
           mRequests.wait(1000);
         } catch (InterruptedException ie) {
           // Ignore and continue processing requests
         }
       }
     }
     if (ciq != null && queryContactInfo(ciq)) {
       needNotify = true;
     }
   }
 }
  public void run() {
    for (running = true; running; ) {
      Runnable action = null;

      synchronized (actionList) {
        while (actionList.isEmpty() && running)
          try {
            actionList.wait(1000);
          } catch (InterruptedException ex) {
            running = false;
          }
        if (actionList.isEmpty()) break;
        action = (Runnable) actionList.removeFirst();
      }

      try {
        pauseMutex.acquire();
        action.run();
      } catch (InterruptedException ex) {
        break;
      } catch (Throwable t) {
        logger.exception(t);
      } finally {
        completedCount++;
        pauseMutex.release();
      }
    }

    //
    // Complete pending actions after thread stopped.
    //
    while (!actionList.isEmpty()) {
      Runnable action = (Runnable) actionList.removeFirst();
      action.run();
    }
  }