List of usage examples for java.lang Thread start
public synchronized void start()
From source file:jetbrains.exodus.util.ForkSupportIO.java
private static Thread createSpinner(final InputStream input, final PrintStream output, final int bufferSize, final String title) { final Thread result = new Thread(new Runnable() { @Override/*w ww. j a v a 2 s . c o m*/ public void run() { try { final byte[] buf = new byte[bufferSize]; int i; while ((i = input.read(buf, 0, bufferSize)) != -1) { output.write(buf, 0, i); } } catch (IOException ioe) { if (log.isWarnEnabled()) { log.warn("IO error in child process for reader: " + input); } } } }, title); result.start(); return result; }
From source file:com.facebook.stetho.Stetho.java
/** * Start the listening service, providing a custom initializer as per * {@link #newInitializerBuilder}.//from w w w. j av a 2 s.c o m * * @see #initializeWithDefaults(Context) */ public static void initialize(final Initializer initializer) { // Hook activity tracking so that after Stetho is attached we can figure out what // activities are present. boolean isTrackingActivities = ActivityTracker.get() .beginTrackingIfPossible((Application) initializer.mContext.getApplicationContext()); if (!isTrackingActivities) { LogUtil.w("Automatic activity tracking not available on this API level, caller must invoke " + "ActivityTracker methods manually!"); } Thread listener = new Thread(LISTENER_THREAD_NAME) { @Override public void run() { LocalSocketHttpServer server = new LocalSocketHttpServer(initializer); try { server.run(); } catch (IOException e) { LogUtil.e(e, "Could not start Stetho"); } } }; listener.start(); }
From source file:com.apptentive.android.sdk.module.engagement.interaction.InteractionManager.java
public static void asyncFetchAndStoreInteractions(final Context context) { if (!isPollForInteractions(context)) { Log.v("Interaction polling is disabled."); return;//from w w w . j a va 2 s . c o m } if (hasCacheExpired(context)) { Log.d("Interaction cache has expired. Fetching new interactions."); Thread thread = new Thread() { public void run() { fetchAndStoreInteractions(context); } }; Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable throwable) { Log.w("UncaughtException in InteractionManager.", throwable); MetricModule.sendError(context.getApplicationContext(), throwable, null, null); } }; thread.setUncaughtExceptionHandler(handler); thread.setName("Apptentive-FetchInteractions"); thread.start(); } else { Log.d("Interaction cache has not expired. Using existing interactions."); } }
From source file:it.cdpaf.helper.DrawableManager.java
public static void fetchDrawableOnThread(final Product product, final ImageView imageView, final Context ctx) { final String urlString = Const.IMAGE_URL + product.getPercorsoImmagine(); if (drawableMap.containsKey(urlString)) { Log.d(ctx.getClass().getSimpleName(), "DRAWABLE MANAGER FDOT:" + "RIUSO, Size:" + drawableMap.size()); imageView.setImageDrawable(drawableMap.get(urlString)); return;//from w w w. jav a 2s . com //product.setImmagine(drawableMap.get(urlString)); } final Handler handler = new Handler() { @Override public void handleMessage(Message message) { Drawable dr = (Drawable) message.obj; imageView.setImageDrawable(dr); product.setImmagine(dr); } }; Thread thread = new Thread() { @Override public void run() { //TODO : set imageView to a "pending" image Drawable d = ctx.getResources().getDrawable(R.drawable.blank); Message messagea = handler.obtainMessage(1, d); handler.sendMessage(messagea); Drawable drawable = fetchDrawable(urlString, ctx); Message messageb = handler.obtainMessage(1, drawable); handler.sendMessage(messageb); } }; Log.i("DRAWABLE MANAGER:", "LANCIO FDOT, Size:" + drawableMap.size()); thread.start(); }
From source file:com.iitb.cse.Utils.java
public static boolean startExperiment(int expId, String timeout, String logBgTraffic) { File file = null;//from w ww.j a v a 2s .co m File file1 = null; if (Constants.experimentDetailsDirectory.endsWith("/")) { file = new File(Constants.experimentDetailsDirectory + Constants.currentSession.getCurrentExperimentId() + "/" + Constants.configFile); file1 = new File( Constants.experimentDetailsDirectory + Constants.currentSession.getCurrentExperimentId()); } else { file = new File(Constants.experimentDetailsDirectory + "/" + Constants.currentSession.getCurrentExperimentId() + "/" + Constants.configFile); file1 = new File( Constants.experimentDetailsDirectory + "/" + Constants.currentSession.getCurrentExperimentId()); } if (file.exists()) { Charset charset = Charset.forName("UTF-8"); String line = null; String[] data = new String[1000];// int index = 0; data[index] = ""; try { BufferedReader reader = Files.newBufferedReader(file.toPath(), charset); Calendar cal = Calendar.getInstance(); while ((line = reader.readLine()) != null) { System.out.println("\nLENGTH : " + line.length()); if (line.isEmpty() || line.trim().equals("")) { System.out.println("\nCASE1"); continue; } else if (line.trim().equals("*****\n")) { System.out.println("\nCASE2"); data[index] = expId + "\n" + data[index]; index++; data[index] = ""; continue; } else if (line.trim().equals("*****")) { System.out.println("\nCASE3"); data[index] = expId + "\n" + data[index]; index++; data[index] = ""; continue; } String[] lineVariables = line.split(" "); // int offset = Integer.parseInt(lineVariables[1]); // cal.add(Calendar.SECOND, offset); //**************************************************** double time = Double.parseDouble(lineVariables[1]); int sec = (int) time; double rem = time % 1; int remainder = (int) (rem * 1000); // Calendar cal = Calendar.getInstance(); // System.out.println("\nSec : " + sec + "\nMiSec : " + remainder + "\nTime : " + cal.getTime()); int flag = 0; if (remainder < 100) { flag = 1; remainder = remainder + 100; cal.add(Calendar.SECOND, sec); cal.add(Calendar.MILLISECOND, remainder); cal.add(Calendar.MILLISECOND, -100); } else { cal.add(Calendar.SECOND, sec); cal.add(Calendar.MILLISECOND, remainder); } //**************************************************** if (lineVariables.length == 5) { // System.out.println("\nINSIDE"); data[index] += generateLine(cal, lineVariables[2], lineVariables[0], lineVariables[3], lineVariables[4]); } else { // System.out.println("\nOUTSIDE"); data[index] += generateLine(cal, lineVariables[2], lineVariables[0], lineVariables[3]); } if (flag == 1) { cal.add(Calendar.SECOND, -1 * sec); cal.add(Calendar.MILLISECOND, -1 * remainder); cal.add(Calendar.MILLISECOND, 100); } else { cal.add(Calendar.SECOND, -1 * sec); cal.add(Calendar.MILLISECOND, -1 * remainder); } } data[index] = expId + "\n" + data[index]; } catch (IOException ex) { System.out.println(ex.toString()); return false; } int controlFileIndex = 0; for (DeviceInfo d : Constants.currentSession.getFilteredClients()) { if (controlFileIndex >= Constants.currentSession.getFilteredClients().size()) { break; } else if (data[controlFileIndex] != null) { String jsonString = Utils.getControlFileJson(data[controlFileIndex], timeout, logBgTraffic); System.out.println("\njsonString : " + jsonString); System.out.println("\nControl FIle : " + data[controlFileIndex]); /* Locally keep the corresponding control file to each client*/ PrintWriter writer; try { writer = new PrintWriter(file1 + "/" + d.macAddress + "_confFile"); writer.write(data[controlFileIndex]); writer.flush(); writer.close(); } catch (FileNotFoundException ex) { System.out.println("\nException : " + ex.toString()); } //writer.close(); System.out.println("\nDevice Info : IP " + d.ip + " Port " + d.port + " Mac " + d.macAddress); Thread sendData = new Thread(new SendData(expId, d, 0, jsonString, data[controlFileIndex])); sendData.start(); } else { break; } controlFileIndex++; } } else { System.out.println("\nConfig FIle not found in location : " + Constants.experimentDetailsDirectory + Constants.currentSession.getCurrentExperimentId()); } return true; }
From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java
public static void openInSystemViewer(@Nonnull final DialogProvider dialogProvider, @Nullable final VirtualFile theFile) { final File file = vfile2iofile(theFile); if (file == null) { LOGGER.error("Can't find file to open, null provided"); dialogProvider.msgError("Can't find file to open"); } else {//from w ww . ja v a2s .c o m final Runnable startEdit = new Runnable() { @Override public void run() { boolean ok = false; if (Desktop.isDesktopSupported()) { final Desktop dsk = Desktop.getDesktop(); if (dsk.isSupported(Desktop.Action.OPEN)) { try { dsk.open(file); ok = true; } catch (Throwable ex) { LOGGER.error("Can't open file in system viewer : " + file, ex);//NOI18N } } } if (!ok) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { dialogProvider.msgError("Can't open file in system viewer! See the log!");//NOI18N Toolkit.getDefaultToolkit().beep(); } }); } } }; final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread t, final Throwable e) { LOGGER.error("Detected uncaught exception in openInSystemViewer() for file " + file, e); } }); thr.setDaemon(true); thr.start(); } }
From source file:com.linkedin.helix.TestHelper.java
public static StartCMResult startDummyProcess(final String zkAddr, final String clusterName, final String instanceName) throws Exception { StartCMResult result = new StartCMResult(); HelixManager manager = null;// ww w. j a va 2 s. c om manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName, InstanceType.PARTICIPANT, zkAddr); result._manager = manager; Thread thread = new Thread(new DummyProcessThread(manager, instanceName)); result._thread = thread; thread.start(); return result; }
From source file:com.tascape.qa.th.Utils.java
public static void waitForProcess(final Process process, final long timeout) throws InterruptedException { Thread t = new Thread() { @Override/*from w ww. j av a 2 s . c om*/ public void run() { try { Thread.sleep(timeout); } catch (InterruptedException ex) { LOG.warn(ex.getMessage()); } finally { if (process != null) { process.destroy(); } } } }; t.setDaemon(true); t.start(); if (process != null) { int exitValue = process.waitFor(); LOG.trace("process {} exits with {}", process, exitValue); } }
From source file:com.meltmedia.dropwizard.etcd.json.EtcdWatchServiceIT.java
public static Thread startNoiseThread(EtcdDirectoryDao<NoiseDocument> dao, int count) { Random random = new Random(); Thread events = new Thread(() -> { for (int i = 0; i < count; i++) { switch (random.nextInt(4)) { case 0: case 1: case 2: dao.put("noise_" + String.valueOf(i), new NoiseDocument().withNoise(String.valueOf(i))); break; case 3: dao.putDir("/noise_" + String.valueOf(i)); break; }/*from www . ja v a 2 s . c o m*/ } }); events.start(); return events; }
From source file:Main.java
public static String runScript(String script) { String sRet;/*from w ww. j a v a2 s . c o m*/ 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; }