Example usage for java.lang Runtime availableProcessors

List of usage examples for java.lang Runtime availableProcessors

Introduction

In this page you can find the example usage for java.lang Runtime availableProcessors.

Prototype

public native int availableProcessors();

Source Link

Document

Returns the number of processors available to the Java virtual machine.

Usage

From source file:no.sintef.ict.splcatool.CoveringArrayAlgICPL.java

private List<Pair2> getInvalid(int coveredInitially, List<Pair2> uncovered) {
    int uncTotal = uncovered.size() + coveredInitially;

    // This should run
    Runtime runtime = Runtime.getRuntime();
    int threads = runtime.availableProcessors();
    System.out.println("Threads for this task: " + threads);

    // Remove invalid
    List<List<Pair2>> uncSplit = new ArrayList<List<Pair2>>();
    int beg = 0, range = uncovered.size() / threads + 1;
    for (int i = 0; i < threads; i++) {
        if (beg + range > uncovered.size())
            range = uncovered.size() - beg;
        uncSplit.add(uncovered.subList(beg, beg + range));
        //System.out.println(beg + " ->" + (beg+range));
        beg += range;//  www  .jav a2 s . c o m
    }

    List<RIThread> rits = new ArrayList<RIThread>();
    List<Thread> ts = new ArrayList<Thread>();
    for (int i = 0; i < threads; i++) {
        RIThread rit = new RIThread(cnf, uncSplit.get(i), nrid, idnr);
        rits.add(rit);
        Thread t = new Thread(rit);
        ts.add(t);
    }

    for (int i = 0; i < threads; i++) {
        ts.get(i).start();
    }

    // Start monitoring thread
    List<ProgressReporter> prs = new ArrayList<ProgressReporter>(rits);
    ProgressThread pt = new ProgressThread("Find invalid pairs", prs, uncTotal);
    Thread ptt = new Thread(pt);
    ptt.start();

    // Wait for all threads to finish
    for (int i = 0; i < threads; i++) {
        try {
            ts.get(i).join();
        } catch (InterruptedException e) {
        }
    }

    // Stop monitoring
    pt.stop();

    // Collect
    uncovered = new ArrayList<Pair2>();
    for (int i = 0; i < threads; i++) {
        uncovered.addAll(rits.get(i).getValid());
    }

    // Return
    return uncovered;
}

From source file:no.sintef.ict.splcatool.CoveringArrayAlgICPL.java

private List<Pair3> getInvalid3(int coveredInitially, List<Pair3> uncovered) {
    int uncTotal = uncovered.size() + coveredInitially;

    // This should run
    Runtime runtime = Runtime.getRuntime();
    int threads = runtime.availableProcessors();
    System.out.println("Threads for this task: " + threads);

    // Remove invalid
    List<List<Pair3>> uncSplit = new ArrayList<List<Pair3>>();
    int beg = 0, range = uncovered.size() / threads + 1;
    for (int i = 0; i < threads; i++) {
        if (beg + range > uncovered.size())
            range = uncovered.size() - beg;
        uncSplit.add(uncovered.subList(beg, beg + range));
        //System.out.println(beg + " ->" + (beg+range));
        beg += range;//from ww  w . jav  a 2 s. c  o  m
    }

    List<RIThread3> rits = new ArrayList<RIThread3>();
    List<Thread> ts = new ArrayList<Thread>();
    for (int i = 0; i < threads; i++) {
        RIThread3 rit = new RIThread3(cnf, uncSplit.get(i), nrid, idnr);
        rits.add(rit);
        Thread t = new Thread(rit);
        ts.add(t);
    }

    for (int i = 0; i < threads; i++) {
        ts.get(i).start();
    }

    // Start monitoring thread
    List<ProgressReporter> prs = new ArrayList<ProgressReporter>(rits);
    ProgressThread pt = new ProgressThread("Find invalid", prs, uncTotal);
    Thread ptt = new Thread(pt);
    ptt.start();

    // Wait for all threads to finish
    for (int i = 0; i < threads; i++) {
        try {
            ts.get(i).join();
        } catch (InterruptedException e) {
        }
    }

    // Stop monitoring
    pt.stop();

    // Collect
    uncovered = new ArrayList<Pair3>();
    for (int i = 0; i < threads; i++) {
        uncovered.addAll(rits.get(i).getValid());
    }

    for (int i = 0; i < threads; i++) {
        invalid3w.addAll(rits.get(i).getInvalid());
    }

    // Return
    return uncovered;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private List<Pair2> getInvalid(int coveredInitially, List<Pair2> uncovered) {
    int uncTotal = uncovered.size() + coveredInitially;

    // This should run
    Runtime runtime = Runtime.getRuntime();
    int threads = runtime.availableProcessors();
    System.out.println("Threads for this task: " + threads);

    // Remove invalid
    List<List<Pair2>> uncSplit = new ArrayList<List<Pair2>>();
    int beg = 0, range = uncovered.size() / threads + 1;
    for (int i = 0; i < threads; i++) {
        if (beg + range > uncovered.size())
            range = uncovered.size() - beg;
        uncSplit.add(uncovered.subList(beg, beg + range));
        //System.out.println(beg + " ->" + (beg+range));
        beg += range;//w ww.  j av  a  2  s.co m
    }

    List<RIThread> rits = new ArrayList<RIThread>();
    List<Thread> ts = new ArrayList<Thread>();
    for (int i = 0; i < threads; i++) {
        RIThread rit = new RIThread(cnf, uncSplit.get(i), nrid, idnr);
        rits.add(rit);
        Thread t = new Thread(rit);
        ts.add(t);
    }

    for (int i = 0; i < threads; i++) {
        ts.get(i).start();
    }

    // Start monitoring thread
    List<ProgressReporter> prs = new ArrayList<ProgressReporter>(rits);
    ProgressThread pt = new ProgressThread("Find invalid", prs, uncTotal);
    Thread ptt = new Thread(pt);
    ptt.start();

    // Wait for all threads to finish
    for (int i = 0; i < threads; i++) {
        try {
            ts.get(i).join();
        } catch (InterruptedException e) {
        }
    }

    // Stop monitoring
    pt.stop();

    // Collect
    uncovered = new ArrayList<Pair2>();
    for (int i = 0; i < threads; i++) {
        uncovered.addAll(rits.get(i).getValid());
    }

    // Return
    return uncovered;
}

From source file:no.sintef.ict.splcatool.CoveringArrayChvatal.java

private List<Pair3> getInvalid3(int coveredInitially, List<Pair3> uncovered) {
    int uncTotal = uncovered.size() + coveredInitially;

    // This should run
    Runtime runtime = Runtime.getRuntime();
    int threads = runtime.availableProcessors();
    System.out.println("Threads for this task: " + threads);

    // Remove invalid
    List<List<Pair3>> uncSplit = new ArrayList<List<Pair3>>();
    int beg = 0, range = uncovered.size() / threads + 1;
    for (int i = 0; i < threads; i++) {
        if (beg + range > uncovered.size())
            range = uncovered.size() - beg;
        uncSplit.add(uncovered.subList(beg, beg + range));
        //System.out.println(beg + " ->" + (beg+range));
        beg += range;//from   w  ww.  j ava 2s. c  om
    }

    List<RIThread3> rits = new ArrayList<RIThread3>();
    List<Thread> ts = new ArrayList<Thread>();
    for (int i = 0; i < threads; i++) {
        RIThread3 rit = new RIThread3(cnf, uncSplit.get(i), nrid, idnr);
        rits.add(rit);
        Thread t = new Thread(rit);
        ts.add(t);
    }

    for (int i = 0; i < threads; i++) {
        ts.get(i).start();
    }

    // Start monitoring thread
    List<ProgressReporter> prs = new ArrayList<ProgressReporter>(rits);
    ProgressThread pt = new ProgressThread("Find invalid", prs, uncTotal);
    Thread ptt = new Thread(pt);
    ptt.start();

    // Wait for all threads to finish
    for (int i = 0; i < threads; i++) {
        try {
            ts.get(i).join();
        } catch (InterruptedException e) {
        }
    }

    // Stop monitoring
    pt.stop();

    // Collect
    uncovered = new ArrayList<Pair3>();
    for (int i = 0; i < threads; i++) {
        uncovered.addAll(rits.get(i).getValid());
    }

    // Return
    return uncovered;
}

From source file:us.daveread.basicquery.BasicQuery.java

/**
 * Reports statistics on various data such as the date a query was executed
 * and//from w  w w . j  av  a 2 s .c o m
 * the date the results were fetched. Other statistics are reported on such as
 * the
 * the date a connection was asked for, and the date it was actually received.
 * The report statistics are written to an external text file
 * represented by DBSTATS_NAME.
 * 
 * @param sqlStatement
 *          The SQL statement
 * @param connAsk
 *          The time when the connection was requested
 * @param connGot
 *          The time when the connection was returned
 * @param stmtGot
 *          The time when the statement was returned
 * @param queryStart
 *          The time when query exeution began
 * @param queryReady
 *          The time when the query finished exeuting
 * @param queryRSFetched
 *          The time when the result set had been completely fetched
 * @param queryRSProcessed
 *          The time when the result set had been completely processed
 * @param rows
 *          The number of rows in the result set
 * @param cols
 *          The number of columns in the result set
 * @param model
 *          The table model for the results
 * @param outParams
 *          The output parameters defined for this SQL statement
 */
private void reportStats(String sqlStatement, java.util.Date connAsk, java.util.Date connGot,
        java.util.Date stmtGot, java.util.Date queryStart, java.util.Date queryReady,
        java.util.Date queryRSFetched, java.util.Date queryRSProcessed, long rows, int cols, TableModel model,
        List<Object> outParams) {
    Runtime runtime;
    String runStats;
    PrintWriter out;
    boolean firstEntry;
    final String valueNotApplicable = "\"" + Resources.getString("proValueNotApplicable") + "\",";

    runStats = "";
    out = null;
    firstEntry = false;

    if (fileLogStats.isSelected()) {
        // Identify whether file exists, if not create and add header row
        try {
            new FileReader(DBSTATS_NAME).close();
        } catch (Exception any) {
            firstEntry = true;
        }

        try {
            out = new PrintWriter(new FileWriter(DBSTATS_NAME, true));
        } catch (Exception any) {
            LOGGER.error("Failed to write the statistics file [" + DBSTATS_NAME + "]", any);
            messageOut(Resources.getString("errFailWriteStatsFile", DBSTATS_NAME, any.getMessage()), STYLE_RED);
        }
    }

    // Make sure it is always safe to write to "out" -- simplifies logic
    if (out == null) {
        out = new PrintWriter(new StringWriter());
    }

    // Header, if needed
    if (firstEntry) {
        out.print(Resources.getString("proQueryStatsExportHeader"));
        if (outParams != null && outParams.size() > 0) {
            for (int param = 0; param < outParams.size(); ++param) {
                out.print(Resources.getString("proQueryStatsExportHeaderParam", param + ""));
            }
        }
        out.println();
    }

    // Output Query Index Number
    out.print(querySelection.getSelectedIndex() + ",");

    // Output SQL, replacing quotes with apostrophes
    out.print("\"" + sqlStatement.replace('"', '\'') + "\",");

    // Output timestamp
    out.print(Utility.formattedDate(new java.util.Date()) + ",");

    // Output time required to get connection to database
    if (connAsk != null && connGot != null) {
        runStats += Resources.getString("proTimeConnOpen", (connGot.getTime() - connAsk.getTime()) + "");
        runStats += "  ";
        out.print((connGot.getTime() - connAsk.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    // Output time required to get statement object
    if (connGot != null && stmtGot != null) {
        runStats += Resources.getString("proTimeStmtAccess", (stmtGot.getTime() - connGot.getTime()) + "");
        runStats += "  ";
        out.print((stmtGot.getTime() - connGot.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    // Time it took to configure statement
    if (queryStart != null && stmtGot != null) {
        runStats += Resources.getString("proTimeStmtSetup", (queryStart.getTime() - stmtGot.getTime()) + "");
        runStats += "  ";
        out.print((queryStart.getTime() - stmtGot.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    runStats += "\n          ";

    // Output time DB took to execute query
    if (queryStart != null && queryReady != null) {
        runStats += Resources.getString("proTimeDBExecute", (queryReady.getTime() - queryStart.getTime()) + "");
        runStats += "  ";
        out.print((queryReady.getTime() - queryStart.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    // Output time it took to fetch all results
    if (queryReady != null && queryRSFetched != null) {
        runStats += Resources.getString("proTimeResultsFetch",
                (queryRSFetched.getTime() - queryReady.getTime()) + "");
        runStats += "  ";
        out.print((queryRSFetched.getTime() - queryReady.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    // Output time it took to process all results
    if (queryReady != null && queryRSProcessed != null) {
        runStats += Resources.getString("proTimeResultSet",
                (queryRSProcessed.getTime() - queryReady.getTime()) + "");
        runStats += "  ";
        out.print((queryRSProcessed.getTime() - queryReady.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    if (runStats.length() > 0) {
        messageOut(Resources.getString("proTimeDBStats") + " ", STYLE_SUBTLE, false);
        messageOut(runStats, STYLE_NORMAL, false);
        runStats = "";
    }

    // Output total time it took to obtain connection, execute SQL and obtain
    // results
    if (connAsk != null && queryRSFetched != null) {
        runStats += Resources.getString("proTimeTotal", (queryRSFetched.getTime() - connAsk.getTime()) + "");
        runStats += "  ";
        out.print((queryRSFetched.getTime() - connAsk.getTime()) + ",");
    } else if (connAsk != null && queryRSProcessed != null) {
        runStats += Resources.getString("proTimeTotal", (queryRSProcessed.getTime() - connAsk.getTime()) + "");
        runStats += "  ";
        out.print((queryRSProcessed.getTime() - connAsk.getTime()) + ",");
    } else if (connAsk != null && queryReady != null) {
        runStats += Resources.getString("proTimeTotal", (queryReady.getTime() - connAsk.getTime()) + "");
        runStats += "  ";
        out.print((queryReady.getTime() - connAsk.getTime()) + ",");
    } else {
        out.print(valueNotApplicable);
    }

    messageOut(runStats, STYLE_BOLD, true);

    // Output number of columns in resultset
    out.print(cols + ",");

    // Output number of rows returned or modified
    out.print(rows + "," + maxRows.getSelectedItem().toString() + ",");

    runtime = Runtime.getRuntime();

    // Output environment information
    out.print(runtime.totalMemory() + "," + runtime.freeMemory() + ","
            + (runtime.totalMemory() - runtime.freeMemory()) + "," + runtime.maxMemory() + ","
            + runtime.availableProcessors());

    if (configDisplayClientInfo.isSelected()) {
        runStats = Resources.getString("proMemAlloc", runtime.totalMemory() + "");
        runStats += " " + Resources.getString("proMemFree", runtime.freeMemory() + "");
        runStats += " "
                + Resources.getString("proMemUsed", (runtime.totalMemory() - runtime.freeMemory()) + "");
        runStats += " " + Resources.getString("proMemMax", runtime.maxMemory() + "");
        runStats += " " + Resources.getString("proProcessors", runtime.availableProcessors() + "");

        messageOut(Resources.getString("proClientEnv") + " ", STYLE_SUBTLE, false);
        messageOut(runStats);
    }

    if (poolConnect.isSelected()) {
        messageOut(Resources.getString("msgPoolStats") + " ", STYLE_SUBTLE, false);
        messageOut(Resources.getString("msgPoolStatsCount", getDBPool().getNumActive() + "",
                getDBPool().getNumIdle() + ""));
    }

    // If output parameters, list them
    if (outParams != null && outParams.size() > 0) {
        for (int param = 0; param < outParams.size(); ++param) {
            out.print(",");
            if (outParams.get(param) instanceof String) {
                out.print("\"");
            }
            out.print(outParams.get(param));
            if (outParams.get(param) instanceof String) {
                out.print("\"");
            }
        }
    }

    // If model given, output describe content
    if (model != null) {
        for (int row = 1; row < model.getRowCount(); ++row) {
            out.print(",\"");

            // Column Name
            out.print(model.getValueAt(row, 0));

            // Type
            out.print(" " + model.getValueAt(row, 1));

            // Size
            out.print(" " + model.getValueAt(row, 2));

            // Precision
            out.print(" (" + model.getValueAt(row, 3) + "," + model.getValueAt(row, 4) + ")");

            out.print("\"");
        }
    }

    out.println();
    out.close();
}