Java Code Examples for java.util.concurrent.LinkedBlockingQueue#isEmpty()

The following examples show how to use java.util.concurrent.LinkedBlockingQueue#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private int bkRecovery(final BookKeeperAdmin bkAdmin, final LinkedBlockingQueue<Long> ledgers,
                       final Set<BookieSocketAddress> bookieAddrs,
                       final boolean dryrun, final boolean skipOpenLedgers)
        throws InterruptedException, BKException {
    final AtomicInteger numPendings = new AtomicInteger(ledgers.size());
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final CountDownLatch doneLatch = new CountDownLatch(concurrency);
    Runnable r = new Runnable() {
        @Override
        public void run() {
            while (!ledgers.isEmpty()) {
                long lid = -1L;
                try {
                    lid = ledgers.take();
                    System.out.println("Recovering ledger " + lid);
                    bkAdmin.recoverBookieData(lid, bookieAddrs, dryrun, skipOpenLedgers);
                    System.out.println("Recovered ledger completed : " + lid + ", " + numPendings.decrementAndGet() + " left");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    doneLatch.countDown();
                    break;
                } catch (BKException ke) {
                    System.out.println("Recovered ledger failed : " + lid + ", rc = " + BKException.getMessage(ke.getCode()));
                }
            }
            doneLatch.countDown();
        }
    };
    for (int i = 0; i < concurrency; i++) {
        executorService.submit(r);
    }
    doneLatch.await();
    SchedulerUtils.shutdownScheduler(executorService, 2, TimeUnit.MINUTES);
    return 0;
}
 
Example 2
Source File: ServerModel_Chatting.java    From MiniWeChat-Server with MIT License 5 votes vote down vote up
/**
 * 获取未被接收的聊天消息
 * 
 * @param receiveUserId
 * @return
 * @author Feng
 */
public ArrayList<Chatting> getChattingNotReceive(String receiveUserId) {
	if (chattingHashtable.containsKey(receiveUserId)) {
		LinkedBlockingQueue<Chatting> chattingQueue = chattingHashtable.get(receiveUserId);
		ArrayList<Chatting> chattingList = new ArrayList<Chatting>();
		while (!chattingQueue.isEmpty())
			chattingList.add(chattingQueue.poll());

		return chattingList;
	}
	return new ArrayList<Chatting>();
}
 
Example 3
Source File: Agent.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
@Override
public String agentStatus() {

	StringBuilder sb = new StringBuilder();

	// Messages
	LinkedBlockingQueue<Message> agentQueue = this.messagingClient.getMessageProcessor().getMessageQueue();
	if( agentQueue.isEmpty() ) {
		sb.append( "There is no message being processed in agent queue\n" );

	} else {
		sb.append( "Agent " + getScopedInstancePath() + " (" + getApplicationName() + ")\n" );
		sb.append( "The number total of messages in agent queue is : " + agentQueue.size() + "\n" );
		sb.append( "The types of messages being processed are : " + "\n");
		for( Message msg : agentQueue ) {
			sb.append( msg.getClass().getSimpleName() + "\n" );
		}
	}

	// Running processes
	Process p = ProcessStore.getProcess(this.applicationName, this.scopedInstancePath);
	if( p != null )
		sb.append( "Be careful. A recipe is under execution." );
	else
		sb.append( "No recipe is under execution." );

	return sb.toString();
}
 
Example 4
Source File: CrailBenchmark.java    From incubator-crail with Apache License 2.0 4 votes vote down vote up
void createFile(String filename, int loop) throws Exception, InterruptedException {
	System.out.println("createFile, filename " + filename  + ", loop " + loop);
	
	//warmup
	ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
	CrailBuffer buf = fs.allocateBuffer();
	bufferQueue.add(buf);
	warmUp(filename, warmup, bufferQueue);		
	fs.freeBuffer(buf);	
	
	//benchmark
	System.out.println("starting benchmark...");
	fs.getStatistics().reset();
	LinkedBlockingQueue<String> pathQueue = new LinkedBlockingQueue<String>();
	fs.create(filename, CrailNodeType.DIRECTORY, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT, true).get().syncDir();
	int filecounter = 0;
	for (int i = 0; i < loop; i++){
		String name = "" + filecounter++;
		String f = filename + "/" + name;
		pathQueue.add(f);
	}		
	
	double ops = 0;
	long start = System.currentTimeMillis();
	while(!pathQueue.isEmpty()){
		String path = pathQueue.poll();
		fs.create(path, CrailNodeType.DATAFILE, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT, true).get().syncDir();
	}
	long end = System.currentTimeMillis();
	double executionTime = ((double) (end - start)) / 1000.0;
	double latency = 0.0;
	if (executionTime > 0) {
		latency = 1000000.0 * executionTime / ops;
	}	
	
	System.out.println("execution time " + executionTime);
	System.out.println("ops " + ops);
	System.out.println("latency " + latency);
	
	fs.getStatistics().print("close");
}
 
Example 5
Source File: CrailBenchmark.java    From crail with Apache License 2.0 4 votes vote down vote up
void createFile(String filename, int loop) throws Exception, InterruptedException {
	System.out.println("createFile, filename " + filename  + ", loop " + loop);
	
	//warmup
	ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
	CrailBuffer buf = fs.allocateBuffer();
	bufferQueue.add(buf);
	warmUp(filename, warmup, bufferQueue);		
	fs.freeBuffer(buf);	
	
	//benchmark
	System.out.println("starting benchmark...");
	fs.getStatistics().reset();
	LinkedBlockingQueue<String> pathQueue = new LinkedBlockingQueue<String>();
	fs.create(filename, CrailNodeType.DIRECTORY, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT).get().syncDir();
	int filecounter = 0;
	for (int i = 0; i < loop; i++){
		String name = "" + filecounter++;
		String f = filename + "/" + name;
		pathQueue.add(f);
	}		
	
	double ops = 0;
	long start = System.currentTimeMillis();
	while(!pathQueue.isEmpty()){
		String path = pathQueue.poll();
		fs.create(path, CrailNodeType.DATAFILE, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT).get().syncDir();
	}
	long end = System.currentTimeMillis();
	double executionTime = ((double) (end - start)) / 1000.0;
	double latency = 0.0;
	if (executionTime > 0) {
		latency = 1000000.0 * executionTime / ops;
	}	
	
	System.out.println("execution time " + executionTime);
	System.out.println("ops " + ops);
	System.out.println("latency " + latency);
	
	fs.getStatistics().print("close");
}
 
Example 6
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
protected int runBKCmd(ZooKeeperClient zkc, BookKeeperClient bkc) throws Exception {
    BookKeeperAdmin bkAdmin = new BookKeeperAdmin(bkc.get());
    try {
        if (query) {
            return bkQuery(bkAdmin, bookiesSrc);
        }
        if (fenceOnly) {
            return bkFence(bkc, ledgers, fenceRate);
        }
        if (!force) {
            System.out.println("Bookies : " + bookiesSrc);
            if (!IOUtils.confirmPrompt("Do you want to recover them: (Y/N)")) {
                return -1;
            }
        }
        if (!ledgers.isEmpty()) {
            System.out.println("Ledgers : " + ledgers);
            long numProcessed = 0;
            Iterator<Long> ledgersIter = ledgers.iterator();
            LinkedBlockingQueue<Long> ledgersToProcess = new LinkedBlockingQueue<Long>();
            while (ledgersIter.hasNext()) {
                long lid = ledgersIter.next();
                if (numPartitions <=0 || (numPartitions > 0 && lid % numPartitions == partition)) {
                    ledgersToProcess.add(lid);
                    ++numProcessed;
                }
                if (ledgersToProcess.size() == 10000) {
                    System.out.println("Processing " + numProcessed + " ledgers");
                    bkRecovery(ledgersToProcess, bookiesSrc, dryrun, skipOpenLedgers);
                    ledgersToProcess.clear();
                    System.out.println("Processed " + numProcessed + " ledgers");
                }
            }
            if (!ledgersToProcess.isEmpty()) {
                System.out.println("Processing " + numProcessed + " ledgers");
                bkRecovery(ledgersToProcess, bookiesSrc, dryrun, skipOpenLedgers);
                System.out.println("Processed " + numProcessed + " ledgers");
            }
            System.out.println("Done.");
            CountDownLatch latch = new CountDownLatch(1);
            latch.await();
            return 0;
        }
        return bkRecovery(bkAdmin, bookiesSrc, dryrun, skipOpenLedgers);
    } finally {
        bkAdmin.close();
    }
}
 
Example 7
Source File: IntegerPolynomial.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
/**
 * Multithreaded version of {@link #resultant()}.
 *
 * @return <code>(rho, res)</code> satisfying <code>res = rho*this + t*(x^n-1)</code> for some integer <code>t</code>.
 */
public Resultant resultantMultiThread()
{
    int N = coeffs.length;

    // upper bound for resultant(f, g) = ||f, 2||^deg(g) * ||g, 2||^deg(f) = squaresum(f)^(N/2) * 2^(deg(f)/2) because g(x)=x^N-1
    // see http://jondalon.mathematik.uni-osnabrueck.de/staff/phpages/brunsw/CompAlg.pdf chapter 3
    BigInteger max = squareSum().pow((N + 1) / 2);
    max = max.multiply(BigInteger.valueOf(2).pow((degree() + 1) / 2));
    BigInteger max2 = max.multiply(BigInteger.valueOf(2));

    // compute resultants modulo prime numbers
    BigInteger prime = BigInteger.valueOf(10000);
    BigInteger pProd = Constants.BIGINT_ONE;
    LinkedBlockingQueue<Future<ModularResultant>> resultantTasks = new LinkedBlockingQueue<Future<ModularResultant>>();
    Iterator<BigInteger> primes = BIGINT_PRIMES.iterator();
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    while (pProd.compareTo(max2) < 0)
    {
        if (primes.hasNext())
        {
            prime = primes.next();
        }
        else
        {
            prime = prime.nextProbablePrime();
        }
        Future<ModularResultant> task = executor.submit(new ModResultantTask(prime.intValue()));
        resultantTasks.add(task);
        pProd = pProd.multiply(prime);
    }

    // Combine modular resultants to obtain the resultant.
    // For efficiency, first combine all pairs of small resultants to bigger resultants,
    // then combine pairs of those, etc. until only one is left.
    ModularResultant overallResultant = null;
    while (!resultantTasks.isEmpty())
    {
        try
        {
            Future<ModularResultant> modRes1 = resultantTasks.take();
            Future<ModularResultant> modRes2 = resultantTasks.poll();
            if (modRes2 == null)
            {
                // modRes1 is the only one left
                overallResultant = modRes1.get();
                break;
            }
            Future<ModularResultant> newTask = executor.submit(new CombineTask(modRes1.get(), modRes2.get()));
            resultantTasks.add(newTask);
        }
        catch (Exception e)
        {
            throw new IllegalStateException(e.toString());
        }
    }
    executor.shutdown();
    BigInteger res = overallResultant.res;
    BigIntPolynomial rhoP = overallResultant.rho;

    BigInteger pProd2 = pProd.divide(BigInteger.valueOf(2));
    BigInteger pProd2n = pProd2.negate();

    if (res.compareTo(pProd2) > 0)
    {
        res = res.subtract(pProd);
    }
    if (res.compareTo(pProd2n) < 0)
    {
        res = res.add(pProd);
    }

    for (int i = 0; i < N; i++)
    {
        BigInteger c = rhoP.coeffs[i];
        if (c.compareTo(pProd2) > 0)
        {
            rhoP.coeffs[i] = c.subtract(pProd);
        }
        if (c.compareTo(pProd2n) < 0)
        {
            rhoP.coeffs[i] = c.add(pProd);
        }
    }

    return new Resultant(rhoP, res);
}
 
Example 8
Source File: IntegerPolynomial.java    From ripple-lib-java with ISC License 4 votes vote down vote up
/**
 * Multithreaded version of {@link #resultant()}.
 *
 * @return <code>(rho, res)</code> satisfying <code>res = rho*this + t*(x^n-1)</code> for some integer <code>t</code>.
 */
public Resultant resultantMultiThread()
{
    int N = coeffs.length;

    // upper bound for resultant(f, g) = ||f, 2||^deg(g) * ||g, 2||^deg(f) = squaresum(f)^(N/2) * 2^(deg(f)/2) because g(x)=x^N-1
    // see http://jondalon.mathematik.uni-osnabrueck.de/staff/phpages/brunsw/CompAlg.pdf chapter 3
    BigInteger max = squareSum().pow((N + 1) / 2);
    max = max.multiply(BigInteger.valueOf(2).pow((degree() + 1) / 2));
    BigInteger max2 = max.multiply(BigInteger.valueOf(2));

    // compute resultants modulo prime numbers
    BigInteger prime = BigInteger.valueOf(10000);
    BigInteger pProd = Constants.BIGINT_ONE;
    LinkedBlockingQueue<Future<ModularResultant>> resultantTasks = new LinkedBlockingQueue<Future<ModularResultant>>();
    Iterator<BigInteger> primes = BIGINT_PRIMES.iterator();
    ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    while (pProd.compareTo(max2) < 0)
    {
        if (primes.hasNext())
        {
            prime = primes.next();
        }
        else
        {
            prime = prime.nextProbablePrime();
        }
        Future<ModularResultant> task = executor.submit(new ModResultantTask(prime.intValue()));
        resultantTasks.add(task);
        pProd = pProd.multiply(prime);
    }

    // Combine modular resultants to obtain the resultant.
    // For efficiency, first combine all pairs of small resultants to bigger resultants,
    // then combine pairs of those, etc. until only one is left.
    ModularResultant overallResultant = null;
    while (!resultantTasks.isEmpty())
    {
        try
        {
            Future<ModularResultant> modRes1 = resultantTasks.take();
            Future<ModularResultant> modRes2 = resultantTasks.poll();
            if (modRes2 == null)
            {
                // modRes1 is the only one left
                overallResultant = modRes1.get();
                break;
            }
            Future<ModularResultant> newTask = executor.submit(new CombineTask(modRes1.get(), modRes2.get()));
            resultantTasks.add(newTask);
        }
        catch (Exception e)
        {
            throw new IllegalStateException(e.toString());
        }
    }
    executor.shutdown();
    BigInteger res = overallResultant.res;
    BigIntPolynomial rhoP = overallResultant.rho;

    BigInteger pProd2 = pProd.divide(BigInteger.valueOf(2));
    BigInteger pProd2n = pProd2.negate();

    if (res.compareTo(pProd2) > 0)
    {
        res = res.subtract(pProd);
    }
    if (res.compareTo(pProd2n) < 0)
    {
        res = res.add(pProd);
    }

    for (int i = 0; i < N; i++)
    {
        BigInteger c = rhoP.coeffs[i];
        if (c.compareTo(pProd2) > 0)
        {
            rhoP.coeffs[i] = c.subtract(pProd);
        }
        if (c.compareTo(pProd2n) < 0)
        {
            rhoP.coeffs[i] = c.add(pProd);
        }
    }

    return new Resultant(rhoP, res);
}