Example usage for java.lang Thread start

List of usage examples for java.lang Thread start

Introduction

In this page you can find the example usage for java.lang Thread start.

Prototype

public synchronized void start() 

Source Link

Document

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Usage

From source file:com.vaadin.tools.ReportUsage.java

public static FutureTask<Void> checkForUpdatesInBackgroundThread() {
    FutureTask<Void> task = new FutureTask<>(new Callable<Void>() {
        @Override//w  w  w .  ja v a  2  s  . c o m
        public Void call() throws Exception {
            ReportUsage.report();
            return null;
        }
    });
    Thread checkerThread = new Thread(task, "Vaadin Update Checker");
    checkerThread.setDaemon(true);
    checkerThread.start();
    return task;
}

From source file:Main.java

public static void sleep(int milliseconds) {
    final int ms = milliseconds;
    Thread t = new Thread() {
        public void run() {
            try {
                sleep(ms);//from ww  w  .  ja va2s. c o  m
            } catch (Exception e) {
            }
        }
    };
    t.start();
    try {
        t.join();
    } catch (Exception e) {
    }
}

From source file:mServer.upload.MserverFtp.java

public static boolean uploadFtp(String srcPathFile_, String destFileName_, MserverDatenUpload datenUpload_) {
    try {/*from   w ww .j ava2 s  .co m*/
        srcPathFile = srcPathFile_;
        destFileName = destFileName_;
        datenUpload = datenUpload_;
        server = datenUpload.arr[MserverDatenUpload.UPLOAD_SERVER_NR];
        strPort = datenUpload.arr[MserverDatenUpload.UPLOAD_PORT_NR];
        username = datenUpload.arr[MserverDatenUpload.UPLOAD_USER_NR];
        password = datenUpload.arr[MserverDatenUpload.UPLOAD_PWD_NR];
        MserverLog.systemMeldung("");
        MserverLog.systemMeldung("----------------------");
        MserverLog.systemMeldung("Upload start");
        MserverLog.systemMeldung("Server: " + server);
        Thread t = new Thread(new Ftp());
        t.start();

        int warten = MserverKonstanten.MAX_WARTEN_FTP_UPLOAD /*Minuten*/;
        MserverLog.systemMeldung("Max Laufzeit FTP[Min]: " + warten);
        MserverLog.systemMeldung("-----------------------------------");
        warten = 1000 * 60 * warten;
        t.join(warten);

        if (t != null) {
            if (t.isAlive()) {
                MserverLog.fehlerMeldung(396958702, MserverFtp.class.getName(),
                        "Der letzte FtpUpload luft noch");
                MserverLog.systemMeldung("und wird gekillt");
                t.stop();
                retFtp = false;
            }
        }
    } catch (Exception ex) {
        MserverLog.fehlerMeldung(739861047, MserverFtp.class.getName(), "uploadFtp", ex);
    }
    return retFtp;
}

From source file:com.github.horrorho.inflatabledonkey.util.LZFSEExtInputStream.java

public static LZFSEExtInputStream create(Path cmd, InputStream is) throws IOException {
    Process process = Runtime.getRuntime().exec(cmd + " -decode");
    LZFSEExtInputStream instance = new LZFSEExtInputStream(process, null);
    Thread thread = new Thread(() -> pipe(is, process.getOutputStream(), instance::error));
    thread.start();
    return instance;
}

From source file:functionaltests2.SchedulerCommandLine.java

/**
 * Start a Scheduler and Resource Manager.
 */// ww w  . ja  va2s  .  c o m
public static void startSchedulerCmdLine(boolean restart, File proactiveConf) throws Exception {

    File schedHome = new File(System.getProperty("pa.scheduler.home")).getCanonicalFile();
    File rmHome = new File(System.getProperty("pa.rm.home")).getCanonicalFile();
    if (proactiveConf != null) {
        FileUtils.copyFile(proactiveConf,
                new File(schedHome, "config" + fs + "proactive" + fs + "ProActiveConfiguration.xml"));
    }

    System.out.println(schedHome);

    p = null;
    ProcessBuilder pb = new ProcessBuilder();
    if (OperatingSystem.getOperatingSystem().equals(OperatingSystem.unix)) {
        pb.directory(new File(schedHome + fs + "bin" + fs + "unix"));
        pb.command("/bin/bash", restart ? "scheduler-start" : "scheduler-start-clean",
                "-Dproactive.communication.protocol=pnp", "-Dproactive.pnp.port=9999");
        pb.environment().put("SchedulerTStarter", "SchedulerTStarter");
        p = pb.start();

    } else {

        pb.directory(new File(schedHome + fs + "bin" + fs + "windows"));

        pb.command("cmd.exe", "/c", restart ? "scheduler-start.bat" : "scheduler-start-clean.bat",
                "-Dproactive.communication.protocol=pnp", "-Dproactive.pnp.port=9999");
        pb.environment().put("SchedulerTStarter", "SchedulerTStarter");
        p = pb.start();

    }

    IOTools.LoggingThread lt1 = new IOTools.LoggingThread(p.getInputStream(), "[SchedulerTStarter]",
            System.out);
    Thread t1 = new Thread(lt1, "SchedulerTStarter");
    t1.setDaemon(true);
    t1.start();

    // waiting the initialization
    RMAuthentication rmAuth = RMConnection.waitAndJoin("pnp://localhost:9999");

    System.out.println("RM successfully joined.");

    SchedulerConnection.waitAndJoin("pnp://localhost:9999");
    System.out.println("Scheduler successfully joined.");

}

From source file:com.googlecode.icegem.cacheutils.common.Utils.java

/**
 * Executes the thread with specified timeout.
 * //w w w. ja v  a2s  .  co  m
 * @param thread
 *            - the thread to execute.
 * @param timeout
 *            - the timeout.
 */
public static void execute(Thread thread, long timeout) {
    thread.start();

    try {
        thread.join(timeout);
    } catch (InterruptedException e) {
        // Should not be interrupted normally.
    }

    if (thread.isAlive()) {
        thread.interrupt();
    }
}

From source file:de.flapdoodle.embed.process.io.file.FileCleaner.java

public synchronized static void forceDeleteOnExit(File fileOrDir) {
    //      FileUtils.forceDeleteOnExit(file);
    if (cleaner == null) {
        cleaner = new Cleaner();

        Thread cleanerThread = new Thread(new CleanerThreadRunner(cleaner));
        cleanerThread.setDaemon(true);/*  w ww.  java2  s  .  co  m*/
        cleanerThread.start();

        Runtime.getRuntime().addShutdownHook(new Thread(new CleanerShutdownHook(cleaner)));
    }

    cleaner.forceDelete(fileOrDir);
}

From source file:Main.java

/**
 * Executes the network requests on a separate thread.
 *
 * @param runnable//w  ww . j a  va2  s .  c  o m
 *            The runnable instance containing network mOperations to be
 *            executed.
 */
public static Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {

            }
        }
    };
    t.start();
    return t;
}

From source file:net.lldp.checksims.util.threading.ParallelAlgorithm.java

/**
 * Internal backend: Execute given tasks on a new thread pool.
 *
 * Expects Callable tasks, with non-void returns. If the need for void returning functions emerges, might need
 * another version of this?/*  ww  w . ja  v a  2s. c  o m*/
 *
 * @param tasks Tasks to execute
 * @param <T> Type returned by the tasks
 * @return Collection of Ts
 */
private static <T, T2 extends Callable<T>> Collection<T> executeTasks(Collection<T2> tasks, StatusLogger logger)
        throws ChecksimsException {
    checkNotNull(tasks);

    if (tasks.size() == 0) {
        logs.warn("Parallel execution called with no tasks - no work done!");
        return new ArrayList<>();
    }

    if (executor.isShutdown()) {
        throw new ChecksimsException("Attempted to call executeTasks while executor was shut down!");
    }

    logs.info("Starting work using " + threadCount + " threads.");

    // Invoke the executor on all the worker instances
    try {
        // Create a monitoring thread to show progress
        MonitorThread monitor = new MonitorThread(executor, logger);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();

        List<Future<T>> results = executor.invokeAll(tasks);

        // Stop the monitor
        monitor.shutDown();

        // Unpack the futures
        ArrayList<T> unpackInto = new ArrayList<>();

        for (Future<T> future : results) {
            try {
                unpackInto.add(future.get());
            } catch (ExecutionException e) {
                executor.shutdownNow();
                logs.error("Fatal error in executed job!");
                throw new ChecksimsException("Error while executing worker for future", e.getCause());
            }
        }

        return unpackInto;
    } catch (InterruptedException e) {
        executor.shutdownNow();
        throw new ChecksimsException("Execution of Checksims was interrupted!", e);
    } catch (RejectedExecutionException e) {
        executor.shutdownNow();
        throw new ChecksimsException("Could not schedule execution of all tasks!", e);
    }
}

From source file:edu.wpi.checksims.util.threading.ParallelAlgorithm.java

/**
 * Internal backend: Execute given tasks on a new thread pool.
 *
 * Expects Callable tasks, with non-void returns. If the need for void returning functions emerges, might need
 * another version of this?// ww w  .j a  v  a 2s  .  c  o  m
 *
 * @param tasks Tasks to execute
 * @param <T> Type returned by the tasks
 * @return Collection of Ts
 */
private static <T, T2 extends Callable<T>> Collection<T> executeTasks(Collection<T2> tasks) {
    checkNotNull(tasks);

    if (tasks.size() == 0) {
        logs.warn("Parallel execution called with no tasks - no work done!");
        return new ArrayList<>();
    }

    if (executor.isShutdown()) {
        throw new RuntimeException("Attempted to call executeTasks while executor was shut down!");
    }

    logs.info("Starting work using " + threadCount + " threads.");

    // Invoke the executor on all the worker instances
    try {
        // Create a monitoring thread to show progress
        MonitorThread monitor = new MonitorThread(executor);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();

        List<Future<T>> results = executor.invokeAll(tasks);

        // Stop the monitor
        monitor.shutDown();

        // Unpack the futures
        ArrayList<T> unpackInto = new ArrayList<>();

        for (Future<T> future : results) {
            try {
                unpackInto.add(future.get());
            } catch (ExecutionException e) {
                executor.shutdownNow();
                logs.error("Fatal error in executed job!");
                throw new RuntimeException("Error while executing worker for future", e.getCause());
            }
        }

        return unpackInto;
    } catch (InterruptedException e) {
        executor.shutdownNow();
        logs.error("Execution of Checksims was interrupted!");
        throw new RuntimeException(e);
    } catch (RejectedExecutionException e) {
        executor.shutdownNow();
        logs.error("Could not schedule execution of all comparisons --- possibly too few resources available?");
        throw new RuntimeException(e);
    }
}