List of usage examples for java.lang Thread Thread
public Thread(String name)
From source file:Main.java
public static String runScript(String script) { String sRet;/* w w w . j a v a 2s .c om*/ try { final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:Main.java
public static void showMessage(final Context act, final String msg, final int len) { new Thread(new Runnable() { public void run() { handler.post(new Runnable() { @Override/*from www.java 2 s .c o m*/ public void run() { if (toast != null) { // toast.cancel(); toast.setText(msg); // toast.setDuration(len); } else { toast = Toast.makeText(act, msg, len); toast.setGravity(Gravity.BOTTOM, 0, 100); } toast.show(); } }); } }).start(); }
From source file:Main.java
/** * Starts a new thread and then adds the string and thread to the thread * pool/* w w w .j ava2 s . c o m*/ * * @param name * the string to be added * @param threadRun * the thread to be executed and added */ public static void spinThreadForPool(String name, Runnable threadRun) { Thread spun = new Thread(threadRun); spun.start(); threadPool.put(name, spun); }
From source file:puma.central.pdp.CentralPUMAPDP.java
public static void main(String[] args) { // initialize log4j BasicConfigurator.configure();//from w w w .j ava 2 s. co m CommandLineParser parser = new BasicParser(); Options options = new Options(); options.addOption("ph", "policy-home", true, "The folder where to find the policy file given with the given policy id. " + "For default operation, this folder should contain the central PUMA policy (called " + CENTRAL_PUMA_POLICY_FILENAME + ")"); options.addOption("pid", "policy-id", true, "The id of the policy to be evaluated on decision requests. Default value: " + GLOBAL_PUMA_POLICY_ID + ")"); options.addOption("s", "log-disabled", true, "Verbose mode (true/false)"); String policyHome = ""; String policyId = ""; // read command line try { CommandLine line = parser.parse(options, args); if (line.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Simple PDP Test", options); return; } if (line.hasOption("policy-home")) { policyHome = line.getOptionValue("policy-home"); } else { logger.log(Level.WARNING, "Incorrect arguments given."); return; } if (line.hasOption("log-disabled") && Boolean.parseBoolean(line.getOptionValue("log-disabled"))) { logger.log(Level.INFO, "Now switching to silent mode"); LogManager.getLogManager().getLogger("").setLevel(Level.WARNING); //LogManager.getLogManager().reset(); } if (line.hasOption("policy-id")) { policyId = line.getOptionValue("policy-id"); } else { logger.log(Level.INFO, "Using default policy id: " + GLOBAL_PUMA_POLICY_ID); policyId = GLOBAL_PUMA_POLICY_ID; } } catch (ParseException e) { logger.log(Level.WARNING, "Incorrect arguments given.", e); return; } // // STARTUP THE RMI SERVER // // if (System.getSecurityManager() == null) { // System.setSecurityManager(new SecurityManager()); // } final CentralPUMAPDP pdp; try { pdp = new CentralPUMAPDP(policyHome, policyId); } catch (IOException e) { logger.log(Level.SEVERE, "FAILED to set up the CentralPUMAPDP. Quitting.", e); return; } try { Registry registry; try { registry = LocateRegistry.createRegistry(RMI_REGISITRY_PORT); logger.info("Created new RMI registry"); } catch (RemoteException e) { // MDC: I hope this means the registry already existed. registry = LocateRegistry.getRegistry(RMI_REGISITRY_PORT); logger.info("Reusing existing RMI registry"); } CentralPUMAPDPRemote stub = (CentralPUMAPDPRemote) UnicastRemoteObject.exportObject(pdp, 0); registry.bind(CENTRAL_PUMA_PDP_RMI_NAME, stub); logger.info( "Central PUMA PDP up and running (available using RMI with name \"central-puma-pdp\" on RMI registry port " + RMI_REGISITRY_PORT + ")"); Thread.sleep(100); // MDC: vroeger eindigde de Thread om n of andere reden, dit lijkt te werken... } catch (Exception e) { logger.log(Level.SEVERE, "FAILED to set up PDP as RMI server", e); } // // STARTUP THE THRIFT PEP SERVER // //logger.log(Level.INFO, "Not setting up the Thrift server"); // set up server // PEPServer handler = new PEPServer(new CentralPUMAPEP(pdp)); // RemotePEPService.Processor<PEPServer> processor = new RemotePEPService.Processor<PEPServer>(handler); // TServerTransport serverTransport; // try { // serverTransport = new TServerSocket(THRIFT_PEP_PORT); // } catch (TTransportException e) { // e.printStackTrace(); // return; // } // TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor)); // System.out.println("Setting up the Thrift PEP server on port " + THRIFT_PEP_PORT); // server.serve(); // // STARTUP THE THRIFT PEP SERVER // //logger.log(Level.INFO, "Not setting up the Thrift server"); // set up server // do this in another thread not to block the main thread new Thread(new Runnable() { @Override public void run() { RemotePDPService.Processor<CentralPUMAPDP> pdpProcessor = new RemotePDPService.Processor<CentralPUMAPDP>( pdp); TServerTransport pdpServerTransport; try { pdpServerTransport = new TServerSocket(THRIFT_PDP_PORT); } catch (TTransportException e) { e.printStackTrace(); return; } TServer pdpServer = new TThreadPoolServer( new TThreadPoolServer.Args(pdpServerTransport).processor(pdpProcessor)); logger.info("Setting up the Thrift PDP server on port " + THRIFT_PDP_PORT); pdpServer.serve(); } }).start(); }
From source file:Main.java
/***/ public static Thread[] createThreads(Runnable... runnables) { Thread[] ret = new Thread[runnables.length]; for (int i = 0; i < runnables.length; i++) { ret[i] = new Thread(runnables[i]); }/*from w w w . jav a 2 s .c o m*/ return ret; }
From source file:Main.java
private static void runTest() throws InterruptedException { Collection<String> syncList = Collections.synchronizedList(new ArrayList<String>(1)); List<String> list = Arrays.asList("A", "B", "C", "D"); List<Thread> threads = new ArrayList<Thread>(); for (final String s : list) { Thread thread = new Thread(new Runnable() { @Override/*from w w w .j a va 2s . c om*/ public void run() { syncList.add(s); } }); threads.add(thread); thread.start(); } for (Thread thread : threads) { thread.join(); } System.out.println(syncList); }
From source file:Main.java
/** * Starts the given {@link Runnable} tasks as normal threads * @param tasks/*from www. j a va 2 s .co m*/ */ public static void start(Runnable... tasks) { for (Runnable task : tasks) { new Thread(task).start(); } }
From source file:Main.java
public static void runInThread(Runnable task) { new Thread(task).start(); }
From source file:Main.java
public static void execute(Runnable runnable) { new Thread(runnable).start(); }
From source file:Main.java
private static void pipe(final InputStream src, final PrintStream dest) { new Thread(new Runnable() { @Override// w w w . j ava 2s. c o m public void run() { try { byte[] buffer = new byte[1024]; for (int n = 0; n != -1; n = src.read(buffer)) { dest.write(buffer, 0, n); } } catch (IOException e) { } } }).start(); }