Example usage for java.util.concurrent ThreadLocalRandom current

List of usage examples for java.util.concurrent ThreadLocalRandom current

Introduction

In this page you can find the example usage for java.util.concurrent ThreadLocalRandom current.

Prototype

public static ThreadLocalRandom current() 

Source Link

Document

Returns the current thread's ThreadLocalRandom .

Usage

From source file:client.welcome3.java

private void genRepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genRepButtonActionPerformed
    Document doc = new Document();
    PdfWriter docWriter = null;//from   www  . j  av  a 2 s.  c o  m
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();

    try {

        //special font sizes
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Project Report- " + dt + " Status " + report_status + " .pdf";
        String path = "src/ProjectReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
        String sd2 = dateFormat2.format(pBeginDateChooser.getDate());
        String ed2 = dateFormat2.format(pEndDateChooser.getDate());
        Image image = Image.getInstance("src/Images/logo for pdf.png");
        Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see all projects from " + sd2 + " to " + ed2);
        image.setAlignment(Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 2f, 2f, 2f, 2f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Project ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Project Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Start Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Due Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Status", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5;
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String sd = dateFormat1.format(pBeginDateChooser.getDate());
        String ed = dateFormat1.format(pEndDateChooser.getDate());

        if (report_status.equals("All")) {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where due_date >= '"
                        + sd + "' and due_date <= '" + ed + "' and start_date >= '" + sd
                        + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where status = '"
                        + report_status + "' and due_date >= '" + sd + "' and due_date <= '" + ed
                        + "' and start_date >= '" + sd + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        }

        //add the PDF table to the paragraph 
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nProject Status Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(report_status, date, repID);
}

From source file:com.epam.catgenome.manager.FeatureIndexManagerTest.java

@Test
@Ignore // TODO: remove this test before merging to master
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void performanceTest() throws Exception {
    Reference hg38 = EntityHelper.createG38Reference(referenceGenomeManager.createReferenceId());
    referenceGenomeManager.register(hg38);

    FeatureIndexedFileRegistrationRequest request = new FeatureIndexedFileRegistrationRequest();
    request.setReferenceId(hg38.getId());
    request.setPath("/home/kite/Documents/sampleData/Dream.set3.VarDict.SV.vcf");

    VcfFile vcfFile1 = vcfManager.registerVcfFile(request);

    request.setPath("/home/kite/Documents/sampleData/synthetic.challenge.set3.tumor.20pctmasked.truth.vcf");
    VcfFile vcfFile2 = vcfManager.registerVcfFile(request);

    Project project = new Project();
    project.setName(TEST_PROJECT_NAME + 1);
    project.setItems(Arrays.asList(new ProjectItem(new BiologicalDataItem(vcfFile1.getBioDataItemId())),
            new ProjectItem(new BiologicalDataItem(vcfFile2.getBioDataItemId()))));

    projectManager.saveProject(project);

    IndexSearchResult<VcfIndexEntry> entriesRes = featureIndexManager.filterVariations(new VcfFilterForm(),
            project.getId());//from w w  w  .ja  va 2 s .c o  m
    List<VcfIndexEntry> entries = entriesRes.getEntries();
    Assert.assertFalse(entries.isEmpty());
    logger.info("!! Variations count: {}", entries.size());

    TestUtils.warmUp(() -> featureIndexManager.filterVariations(new VcfFilterForm(), project.getId()),
            PERFORMANCE_TEST_WARMING_COUNT);

    double averageTime = TestUtils.measurePerformance(
            () -> featureIndexManager.filterVariations(new VcfFilterForm(), project.getId()),
            PERFORMANCE_TEST_ATTEMPTS_COUNT);

    logger.info("!! Performing index search took: {} ms", averageTime);

    TestUtils.warmUp(() -> featureIndexManager.filterVariations(new VcfFilterForm(), project.getId()),
            PERFORMANCE_TEST_WARMING_COUNT);

    averageTime = TestUtils.measurePerformance(
            () -> featureIndexManager.filterVariations(new VcfFilterForm(), project.getId()),
            PERFORMANCE_TEST_ATTEMPTS_COUNT);

    logger.info("!! Performing index search paging took: {} ms", averageTime);

    final VcfFilterForm filterForm = new VcfFilterForm();
    filterForm.setPage(1);
    filterForm.setPageSize(PERFORMANCE_TEST_PAGE_SIZE);
    TestUtils.warmUp(() -> featureIndexManager.filterVariations(filterForm, project.getId()),
            PERFORMANCE_TEST_WARMING_COUNT);

    averageTime = TestUtils.measurePerformance(
            () -> featureIndexManager.filterVariations(filterForm, project.getId()),
            PERFORMANCE_TEST_ATTEMPTS_COUNT);

    logger.info("!! Performing index search single page took: {} ms", averageTime);

    TestUtils.warmUp(
            () -> ThreadLocalRandom.current().nextInt(1, entries.size() / PERFORMANCE_TEST_PAGE_SIZE + 1),
            (page) -> {
                filterForm.setPage(page);
                featureIndexManager.filterVariations(filterForm, project.getId());
            }, PERFORMANCE_TEST_WARMING_COUNT);

    List<Double> timings = TestUtils.measurePerformanceTimings(
            () -> ThreadLocalRandom.current().nextInt(1, entries.size() / PERFORMANCE_TEST_PAGE_SIZE + 1),
            (page) -> {
                filterForm.setPage(page);
                featureIndexManager.filterVariations(filterForm, project.getId());
            }, PERFORMANCE_TEST_ATTEMPTS_COUNT);

    timings.forEach(t -> logger.info("!! Performed index search random page took: {}", t));
    averageTime = TestUtils.calculateAverage(timings);

    logger.info("!! Average Performing index search random page took: {} ms", averageTime);

    /*TestUtils.warmUp(() -> featureIndexManager.getTotalPagesCount(new VcfFilterForm(), project.getId()),
                 PERFORMANCE_TEST_WARMING_COUNT);
    averageTime = TestUtils.measurePerformance(
    () -> featureIndexManager.getTotalPagesCount(new VcfFilterForm(), project.getId()),
    PERFORMANCE_TEST_ATTEMPTS_COUNT);
    logger.info("!! Performing total facet page count lookup took: {} ms", averageTime);*/
}

From source file:client.welcome2.java

private void genRepButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genRepButtonActionPerformed
    Document doc = new Document();
    PdfWriter docWriter = null;/*from w ww .j  av  a  2  s. c  o m*/
    int repID = ThreadLocalRandom.current().nextInt(10000, 99999 + 1);
    DecimalFormat df = new DecimalFormat("0.00");
    //Date d = Calendar.getInstance().getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();

    try {

        //special font sizes
        Font bfBold12 = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
        Font bf12 = new Font(FontFamily.TIMES_ROMAN, 12);

        //file path
        String dt = dateFormat.format(date);
        sFileName = "Report No- " + repID + " Project Report- " + dt + " Status " + report_status + " .pdf";
        String path = "src/ProjectReports/" + sFileName;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));

        DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String d = dateFormat3.format(Calendar.getInstance().getTime());

        //document header attributes
        doc.addCreationDate();
        doc.setPageSize(PageSize.LETTER);

        //open document
        doc.open();

        //create a paragraph
        DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
        String sd2 = dateFormat2.format(pBeginDateChooser.getDate());
        String ed2 = dateFormat2.format(pEndDateChooser.getDate());
        Image image = Image.getInstance("src/Images/logo for pdf.png");
        Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLD);
        Paragraph paragraph = new Paragraph();
        Paragraph paragraph2 = new Paragraph("This report was generated by " + loginGUI.username + " at " + d
                + "\nYou can see all projects from " + sd2 + " to " + ed2);
        image.setAlignment(Image.RIGHT);
        doc.add(image);

        //specify column widths
        float[] columnWidths = { 2f, 2f, 2f, 2f, 2f };
        //create PDF table with the given widths
        PdfPTable table = new PdfPTable(columnWidths);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        //insert column headings
        insertCell(table, "Project ID", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Project Name", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Start Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Due Date", Element.ALIGN_CENTER, 1, bfBold12);
        insertCell(table, "Status", Element.ALIGN_CENTER, 1, bfBold12);
        table.setHeaderRows(1);

        //insert an empty row
        /* insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);*/
        //create section heading by cell merging
        /* insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12);*/
        /*double orderTotal, total = 0;*/

        String add1, add2, add3, add4, add5;
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String sd = dateFormat1.format(pBeginDateChooser.getDate());
        String ed = dateFormat1.format(pEndDateChooser.getDate());

        if (report_status.equals("All")) {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where due_date >= '"
                        + sd + "' and due_date <= '" + ed + "' and start_date >= '" + sd
                        + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        } else {
            try {

                String sql = "select project_id,project_name,start_date,due_date,status from projects where status = '"
                        + report_status + "' and due_date >= '" + sd + "' and due_date <= '" + ed
                        + "' and start_date >= '" + sd + "'and start_date <= '" + ed + "'";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                while (rs.next()) {
                    add1 = rs.getString("project_id");
                    add2 = rs.getString("project_name");
                    add3 = dateFormat.format(rs.getDate("start_date"));
                    add4 = dateFormat.format(rs.getDate("due_date"));
                    add5 = rs.getString("status");
                    insertCell(table, add1, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add2, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add3, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add4, Element.ALIGN_CENTER, 1, bf12);
                    insertCell(table, add5, Element.ALIGN_CENTER, 1, bf12);

                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);

            }
        }

        //add the PDF table to the paragraph
        paragraph2.add(table);

        // add the paragraph to the document
        doc.add(new Paragraph("\nProject Status Report " + dt + "\n", font1));
        doc.add(paragraph2);
    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            //close the document
            doc.close();
            JOptionPane.showMessageDialog(null, "Report No " + repID + " Generated!");
        }
        if (docWriter != null) {
            //close the writer
            docWriter.close();
        }
    }
    saveToDB(report_status, date, repID);
}

From source file:org.neo4j.io.pagecache.PageCacheTest.java

@Test(timeout = SEMI_LONG_TIMEOUT_MILLIS)
public void freshlyCreatedPagesMustContainAllZeros() throws IOException {
    ThreadLocalRandom rng = ThreadLocalRandom.current();

    getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL);

    try (PagedFile pagedFile = pageCache.map(existingFile("a"), filePageSize);
            PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        for (int i = 0; i < 100; i++) {
            assertTrue(cursor.next());/*from   w w  w. ja v a2 s . c o m*/
            for (int j = 0; j < filePageSize; j++) {
                cursor.putByte((byte) rng.nextInt());
            }
        }
    }
    pageCache.close();
    pageCache = null;
    System.gc(); // make sure underlying pages are finalizable
    System.gc(); // make sure underlying pages are finally collected

    getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL);

    try (PagedFile pagedFile = pageCache.map(existingFile("b"), filePageSize);
            PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        for (int i = 0; i < 100; i++) {
            assertTrue(cursor.next());
            for (int j = 0; j < filePageSize; j++) {
                assertThat(cursor.getByte(), is((byte) 0));
            }
        }
    }
}

From source file:org.neo4j.io.pagecache.PageCacheTest.java

@Test
public void mustSupportUnalignedWordAccesses() throws Exception {
    // 8 MB pages, 10 of them for 80 MB.
    // This way we are sure to write across OS page boundaries. The default
    // size of Huge Pages on Linux is 2 MB, but it can be configured to be
    // as large as 1 GB - at least I have not heard of anyone trying to
    // configure it to be more than that.
    int pageSize = 1024 * 1024 * 8;
    getPageCache(fs, 10, pageSize, PageCacheTracer.NULL);

    ThreadLocalRandom rng = ThreadLocalRandom.current();

    try (PagedFile pagedFile = pageCache.map(file("a"), filePageSize);
            PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());// ww  w  .  j ava  2 s.  com

        for (int i = 0; i < pageSize - 8; i++) {
            cursor.setOffset(i);
            long x = rng.nextLong();
            cursor.putLong(x);
            cursor.setOffset(i);
            String reason = "Failed to read back the value that was written at " + "offset " + toHexString(i);
            assertThat(reason, toHexString(cursor.getLong()), is(toHexString(x)));
        }
    }
}

From source file:com.spotify.docker.client.DefaultDockerClientTest.java

private String randomName() {
    return nameTag + '-' + toHexString(ThreadLocalRandom.current().nextLong());
}