List of usage examples for java.util.concurrent ForkJoinTask join
public final V join()
From source file:net.openhft.chronicle.timeseries.Columns.java
public static <T> void setAll(LongColumn col, Supplier<T> perThread, LongColumnIndexObjectConsumer<T> consumer) { long length = col.length(); int chunks = Math.toIntExact((length - 1) / CHUNK_SIZE + 1); ForkJoinPool fjp = ForkJoinPool.commonPool(); int procs = Runtime.getRuntime().availableProcessors(); List<ForkJoinTask> tasks = new ArrayList<>(procs); int chunksPerTask = (chunks - 1) / procs + 1; for (int i = 0; i < procs; i++) { int si = i * chunksPerTask; int ei = Math.min(chunks, si + chunksPerTask); tasks.add(fjp.submit(() -> {//from w ww . j av a 2 s .c om T t = perThread.get(); long first = (long) si * CHUNK_SIZE; int max = (int) Math.min((ei - si) * CHUNK_SIZE, length - first); for (int j = 0; j < max; j++) { consumer.apply(col, first + j, t); } })); } for (ForkJoinTask task : tasks) { task.join(); } }
From source file:net.openhft.chronicle.timeseries.Columns.java
public static void generateBrownian(DoubleColumn col, double start, double end, double sd) { long length = col.length(); double sd2 = sd / Math.sqrt(length); NormalDistribution nd = new NormalDistribution(0, sd2 * CHUNK_SIZE); int trendLength = Math.toIntExact((length - 1) / CHUNK_SIZE + 2); BytesStore trend = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(trendLength * 8L); double x = start; RandomGenerator rand = new MersenneTwister(); for (int i = 0; i < trendLength - 1; i++) { float f = rand.nextFloat(); trend.writeDouble((long) i << 3, x); x += nd.inverseCumulativeProbability(f); }//from www . j av a 2 s . c o m trend.writeDouble((long) (trendLength - 1) << 3, x); double diff = end - x; double gradient = diff / (trendLength - 1); for (int i = 0; i < trendLength; i++) { double y = trend.addAndGetDoubleNotAtomic((long) i << 3, i * gradient); // System.out.println(i + ": "+y); } int procs = Runtime.getRuntime().availableProcessors(); int chunksPerTask = (trendLength - 1) / procs + 1; ForkJoinPool fjp = ForkJoinPool.commonPool(); List<ForkJoinTask> tasks = new ArrayList<>(procs); for (int i = 0; i < procs; i++) { int si = i * chunksPerTask; int ei = Math.min(trendLength, si + chunksPerTask); tasks.add(fjp.submit(() -> { NormalDistribution nd2 = new NormalDistribution(0, sd2); RandomGenerator rand2 = new MersenneTwister(); for (int j = si; j < ei; j++) { generateBrownian(col, (long) j * CHUNK_SIZE, trend.readDouble((long) j << 3), trend.readDouble((long) (j + 1) << 3), nd2, rand2); } })); } for (ForkJoinTask task : tasks) { task.join(); } trend.release(); }
From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java
@Test public void testMultipleGetWithRangeAsync() throws IOException, URISyntaxException, InterruptedException { final String testResourceUrl = servletRoot + "/foo.txt"; // prepare 8MiB test data: final byte[] plaintextData = new byte[2097152 * Integer.BYTES]; final ByteBuffer plaintextDataByteBuffer = ByteBuffer.wrap(plaintextData); for (int i = 0; i < 2097152; i++) { plaintextDataByteBuffer.putInt(i); }//www .j av a 2 s . c o m try (WritableFile w = fs.file("foo.txt").openWritable()) { plaintextDataByteBuffer.flip(); w.write(plaintextDataByteBuffer); } final MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager(); cm.getParams().setDefaultMaxConnectionsPerHost(50); final HttpClient client = new HttpClient(cm); // multiple async range requests: final List<ForkJoinTask<?>> tasks = new ArrayList<>(); final Random generator = new Random(System.currentTimeMillis()); final AtomicBoolean success = new AtomicBoolean(true); // 10 full interrupted requests: for (int i = 0; i < 10; i++) { final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> { try { final HttpMethod getMethod = new GetMethod(testResourceUrl); final int statusCode = client.executeMethod(getMethod); if (statusCode != 200) { LOG.error("Invalid status code for interrupted full request"); success.set(false); } getMethod.getResponseBodyAsStream().read(); getMethod.getResponseBodyAsStream().close(); getMethod.releaseConnection(); } catch (IOException e) { throw new RuntimeException(e); } }); tasks.add(task); } // 50 crappy interrupted range requests: for (int i = 0; i < 50; i++) { final int lower = generator.nextInt(plaintextData.length); final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> { try { final HttpMethod getMethod = new GetMethod(testResourceUrl); getMethod.addRequestHeader("Range", "bytes=" + lower + "-"); final int statusCode = client.executeMethod(getMethod); if (statusCode != 206) { LOG.error("Invalid status code for interrupted range request"); success.set(false); } getMethod.getResponseBodyAsStream().read(); getMethod.getResponseBodyAsStream().close(); getMethod.releaseConnection(); } catch (IOException e) { throw new RuntimeException(e); } }); tasks.add(task); } // 50 normal open range requests: for (int i = 0; i < 50; i++) { final int lower = generator.nextInt(plaintextData.length - 512); final int upper = plaintextData.length - 1; final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> { try { final HttpMethod getMethod = new GetMethod(testResourceUrl); getMethod.addRequestHeader("Range", "bytes=" + lower + "-"); final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1); final int statusCode = client.executeMethod(getMethod); final byte[] responseBody = new byte[upper - lower + 10]; final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody); getMethod.releaseConnection(); if (statusCode != 206) { LOG.error("Invalid status code for open range request"); success.set(false); } else if (upper - lower + 1 != bytesRead) { LOG.error("Invalid response length for open range request"); success.set(false); } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) { LOG.error("Invalid response body for open range request"); success.set(false); } } catch (IOException e) { throw new RuntimeException(e); } }); tasks.add(task); } // 200 normal closed range requests: for (int i = 0; i < 200; i++) { final int pos1 = generator.nextInt(plaintextData.length - 512); final int pos2 = pos1 + 512; final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> { try { final int lower = Math.min(pos1, pos2); final int upper = Math.max(pos1, pos2); final HttpMethod getMethod = new GetMethod(testResourceUrl); getMethod.addRequestHeader("Range", "bytes=" + lower + "-" + upper); final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1); final int statusCode = client.executeMethod(getMethod); final byte[] responseBody = new byte[upper - lower + 1]; final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody); getMethod.releaseConnection(); if (statusCode != 206) { LOG.error("Invalid status code for closed range request"); success.set(false); } else if (upper - lower + 1 != bytesRead) { LOG.error("Invalid response length for closed range request"); success.set(false); } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) { LOG.error("Invalid response body for closed range request"); success.set(false); } } catch (IOException e) { throw new RuntimeException(e); } }); tasks.add(task); } Collections.shuffle(tasks, generator); final ForkJoinPool pool = new ForkJoinPool(4); for (ForkJoinTask<?> task : tasks) { pool.execute(task); } for (ForkJoinTask<?> task : tasks) { task.join(); } pool.shutdown(); cm.shutdown(); Assert.assertTrue(success.get()); }