List of usage examples for java.lang String wait
public final void wait() throws InterruptedException
From source file:net.daporkchop.porkbot.command.music.CommandPlayAll.java
public void execute(MessageReceivedEvent evt, String[] split, String rawContent) { if (split.length < 2) { sendErrorMessage(evt.getTextChannel(), "Not enough arguments!"); return;/*w w w.ja va2s . c om*/ } if (validator.isValid(split[1])) { try { String[] data = HTTPUtils.performGetRequest(HTTPUtils.constantURL(split[1]), 32000).trim() .split("\n"); int i = 0; Message message = evt.getChannel() .sendMessage("Loading " + data.length + " tracks... (this might take a while)").complete(); for (String s : data) { s = s.trim(); if (!s.contains("~") && validator.isValid(s)) { AudioUtils.loadAndPlay(evt.getTextChannel(), s, evt.getMember(), false); try { synchronized (s) { s.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } i++; } } message.editMessage("Loaded " + i + " tracks! (Out of " + data.length + ")").queue(); } catch (Exception e) { MessageUtils.sendException(e, evt); } } else { MessageUtils.sendMessage("Invalid URL: " + split[1], evt.getTextChannel()); } }
From source file:org.alfresco.wcm.client.impl.AbstractCachingSectionFactoryImpl.java
/** * Refreshes the section cache if empty or expired. * /*from www . ja va 2 s.co m*/ * @param rootSectionId * the id of the parent web root */ private void refreshCacheIfRequired(String rootSectionId) { SectionCache cache = rootSectionsByWebsite.get(rootSectionId); while (cache == null || cache.isExpired()) { String existingSectionToken = sectionsBeingLoaded.putIfAbsent(rootSectionId, rootSectionId); if (existingSectionToken == null) { try { //It looks like we have to load this section tree, //but before we do, let's just check that another thread hasn't got in between us checking the cache //and checking whether it's already being loaded... cache = rootSectionsByWebsite.get(rootSectionId); if (cache != null && !cache.isExpired()) { //This section is now in the cache, so we don't need to do anything return; } if (log.isDebugEnabled()) { log.debug(Thread.currentThread().getName() + " started refreshing tree cache for section " + rootSectionId); } //This section isn't currently being loaded. Load it. Map<String, Section> sections = findSectionWithChildren(rootSectionId); Section rootSection = sections.get(rootSectionId); SectionCache cachedRootSection = new SectionCache(rootSection); rootSectionsByWebsite.put(rootSectionId, cachedRootSection); sectionsById.putAll(sections); if (log.isDebugEnabled()) { log.debug(Thread.currentThread().getName() + " finished refreshing tree cache for section " + rootSectionId); } return; } finally { //There may be other threads waiting for us to finish loading this section tree. //Let them know that we've finished synchronized (rootSectionId) { sectionsBeingLoaded.remove(rootSectionId); rootSectionId.notifyAll(); } } } else { //This section is currently being loaded if (cache != null) { //We are currently refreshing the cache, but the requested section does already //appear in the cache. Therefore we'll let the caller simply use the currently-cached //copy return; } else { //This section isn't currently cached, but is already being loaded //Wait for it to be loaded... synchronized (existingSectionToken) { if (sectionsBeingLoaded.containsKey(existingSectionToken)) { try { if (log.isDebugEnabled()) { log.debug(Thread.currentThread().getName() + " started waiting for section tree to be loaded " + rootSectionId); } existingSectionToken.wait(); } catch (InterruptedException e) { } if (log.isDebugEnabled()) { log.debug(Thread.currentThread().getName() + " finished waiting for section tree to be loaded " + rootSectionId); } } } } } cache = rootSectionsByWebsite.get(rootSectionId); } }
From source file:org.deegree.tools.rendering.dem.filtering.DEMRasterFilterer.java
/** * @throws IOException// ww w . j av a2 s. c o m * @throws InterruptedException * @throws Exception * */ private void applyFilter() throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); int processors = rt.availableProcessors(); LOG.info("Number of processors: {}", processors); // calculate the rows. RasterGeoReference geoRef = raster.getRasterReference(); Envelope renv = raster.getEnvelope(); RasterRect rect = geoRef.convertEnvelopeToRasterCRS(raster.getEnvelope()); int width = raster.getColumns(); int height = raster.getRows(); int numberOfTiles = Rasters.calcApproxTiles(width, height, TILE_SIZE); int tileWidth = Rasters.calcTileSize(width, numberOfTiles); int tileHeight = Rasters.calcTileSize(height, numberOfTiles); int columns = (int) Math.ceil(((double) width) / tileWidth); int rows = (int) Math.ceil((double) height / tileHeight); GridWriter gridWriter = new GridWriter(columns, rows, renv, geoRef, tmpGridFile, raster.getRasterDataInfo()); FilteredResultWiter resultWriter = new FilteredResultWiter(gridWriter); Stack<RasterFilterer> filters = new Stack<RasterFilterer>(); String lock = "lock"; for (int i = 0; i < processors; ++i) { RasterFilterer rf = new RasterFilterer(this.raster, kernelSize, resultWriter, stdCorr, lock, filters); filters.push(rf); } Thread outputThread = new Thread(resultWriter, "result writer"); outputThread.start(); LOG.info("Tiling raster of {} x {} pixels (width x height) into {} rows and {} columns.", new Object[] { rect.width, rect.height, rows, columns }); int kernelHalf = (this.kernelSize - 1) / 2; long totalTime = currentTimeMillis(); for (int row = 30; row < rows; ++row) { long currentTime = currentTimeMillis(); for (int col = 0; col < columns; ++col) { RasterFilterer filterer = null; while (filterer == null) { synchronized (lock) { if (filters.isEmpty()) { lock.wait(); } else { filterer = filters.pop(); } } } RasterRect outputRect = new RasterRect(((col * tileWidth) - kernelHalf), ((row * tileHeight) - kernelHalf), tileWidth + this.kernelSize, tileHeight + this.kernelSize); filterer.setRasterInformation(outputRect); new Thread(filterer, "row_" + row + "_col_" + col).start(); } double rPT = Math.round((Math.round((currentTimeMillis() - currentTime) / 10d) / 100d)); if (row + 1 < rows) { double remain = rPT * (rows - (row + 1)); LOG.info( "Filtering row: {}, took approximately: {} seconds, estimated remaining time: {} seconds " + ((remain > 60) ? "( {} minutes)." : "."), new Object[] { (row + 1), rPT, remain, Math.round(remain / 60d) }); } System.gc(); RasterCache.dispose(); } while (true) { synchronized (lock) { RasterCache.dispose(); if (filters.size() < processors) { try { // wait for all lock.wait(); } catch (InterruptedException e) { LOG.error( "Could not wait for all filter threads to end because: " + e.getLocalizedMessage(), e); } } else { break; } } } resultWriter.stop(); // outputThread.interrupt(); outputThread.join(); gridWriter.writeMetadataFile(null); StringBuilder sb = new StringBuilder("Processing "); sb.append(rows).append(" rows and "); sb.append(columns).append(" columns of rasters with width: "); sb.append(tileWidth).append(" and height: "); sb.append(tileHeight).append(", took: "); sb.append((Math.round((currentTimeMillis() - totalTime) / 10d) / 100d)).append(" seconds"); LOG.info(sb.toString()); // now output the filtered tiles. outputTiles(); }