List of usage examples for java.lang Runnable run
public abstract void run();
Runnable
is used to create a thread, starting the thread causes the object's run
method to be called in that separately executing thread. From source file:demos.Main.java
private static int run(CommandLine cmdLine) throws Exception { Runnable demo; if (cmdLine.hasOption("h")) { demo = Main::printUsage;//from ww w . java 2s. co m } else if (cmdLine.hasOption("d")) { demo = new NodeDiscovery(); } else if (cmdLine.hasOption("t")) { demo = new TopologyChange(); } else if (cmdLine.hasOption("s")) { demo = new SynchronousInsert(); } else if (cmdLine.hasOption("a")) { demo = new AsynchronousInsert(); } else if (cmdLine.hasOption("b")) { demo = new BatchInsert(); } else if (cmdLine.hasOption("r")) { demo = new SynchronousRead(); } else if (cmdLine.hasOption("ar")) { demo = new AsynchronousRead(); } else { demo = Main::printUsage; } try { demo.run(); } catch (Exception e) { return 1; } return 0; }
From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java
/** * Generic wait function./*from ww w .ja v a2s . com*/ */ public static <T> T waitForState(Supplier<T> supplier, Predicate<T> predicate, long waitIterationSleep, long waitIterationCount, Runnable cleanup) throws Throwable { for (int i = 0; i < waitIterationCount; i++) { T t = supplier.get(); if (predicate.test(t)) { return t; } Thread.sleep(waitIterationSleep); } if (cleanup != null) { cleanup.run(); } throw new TimeoutException("timeout waiting for state transition."); }
From source file:com.microsoft.alm.plugin.idea.common.utils.IdeaHelper.java
public static void runOnUIThread(final Runnable runnable, final boolean wait, final ModalityState modalityState) { if (ApplicationManager.getApplication() != null && !ApplicationManager.getApplication().isDispatchThread()) { if (wait) { ApplicationManager.getApplication().invokeAndWait(runnable, modalityState); } else {/*w w w . j ava2 s.co m*/ ApplicationManager.getApplication().invokeLater(runnable, modalityState); } } else { // If we are already on the dispatch thread then we can run it here // If we don't have an application then we are testing, just run the runnable here runnable.run(); } }
From source file:com.ikanow.aleph2.analytics.spark.utils.SparkTechnologyUtils.java
/** Optional utility to respect the test spec's timeout * @param maybe_test_spec//from ww w . j av a 2 s. com * @param on_timeout - mainly for testing */ public static void registerTestTimeout(final Optional<ProcessingTestSpecBean> maybe_test_spec, Runnable on_timeout) { maybe_test_spec.map(test_spec -> test_spec.max_run_time_secs()).ifPresent(max_run_time -> { CompletableFuture.runAsync(Lambdas.wrap_runnable_u(() -> { Thread.sleep(1500L * max_run_time); // (seconds, *1.5 for safety) System.out.println("Test timeout - exiting"); on_timeout.run(); })); }); }
From source file:com.nadmm.airports.utils.NetworkUtils.java
public static void checkNetworkAndDownload(Context context, final Runnable runnable) { if (!NetworkUtils.isNetworkAvailable(context)) { UiUtils.showToast(context, "Please check your internet connection"); }/*from w w w . j a va 2 s . co m*/ if (NetworkUtils.isConnectedToMeteredNetwork(context)) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("You are conneteced to a metered network such as mobile data" + " or tethered to mobile data.\nContinue download?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { runnable.run(); } }).setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); AlertDialog alert = builder.create(); alert.show(); } else { runnable.run(); } }
From source file:com.vmware.photon.controller.common.xenon.ServiceHostUtils.java
/** * Generic wait function./* w ww . j a v a 2s. com*/ */ public static <T> T waitForState(Supplier<T> supplier, Predicate<T> predicate, long waitIterationSleepMillis, long waitIterationCount, Runnable cleanup, String timeoutMessage) throws Throwable { for (int i = 0; i < waitIterationCount; i++) { T t = supplier.get(); if (predicate.test(t)) { return t; } Thread.sleep(waitIterationSleepMillis); } if (cleanup != null) { cleanup.run(); } logger.warn(timeoutMessage); throw new TimeoutException(timeoutMessage); }
From source file:net.gtaun.wl.race.dialog.NewRacingDialog.java
public static WlListDialog create(Player player, EventManager eventManager, AbstractDialog parent, RaceServiceImpl service, Track track) { PlayerStringSet stringSet = service.getLocalizedStringSet().getStringSet(player); RacingManagerImpl racingManager = service.getRacingManager(); String racingMode = stringSet .get((track.getStatus() == TrackStatus.EDITING) ? "Dialog.NewRacingDialog.RacingTestCaption" : "Dialog.NewRacingDialog.RacingNormalCaption"); String[] racingName = new String[1]; racingName[0] = RacingUtils.getDefaultName(player, track); RacingSetting setting = new RacingSetting(track); return WlListDialog.create(player, eventManager).parentDialog(parent) .caption(() -> stringSet.format("Dialog.NewRacingDialog.Caption", racingMode)) .item(() -> stringSet.format("Dialog.NewRacingDialog.Name", racingName[0]), () -> { if (track.getStatus() == TrackStatus.EDITING) return false; return true; }, (i) -> {/* w w w . j ava2 s . co m*/ player.playSound(1083); String caption = stringSet.format("Dialog.NewRacingEditNameDialog.Caption", racingMode); String message = stringSet.get("Dialog.NewRacingEditNameDialog.Text"); WlInputDialog.create(player, eventManager).parentDialog(i.getCurrentDialog()).caption(caption) .message(message).onClickOk((d, text) -> { String name = StringUtils.trimToEmpty(text); if (name.length() < 3 || name.length() > 40) { ((WlInputDialog) d).setAppendMessage( stringSet.get("Dialog.NewRacingEditNameDialog.LimitAppendMessage")); d.show(); return; } racingName[0] = name; }).build().show(); }) .item(() -> stringSet.format("Dialog.NewRacingDialog.Track", track.getName(), track.getCheckpoints().size(), track.getLength() / 1000.0f), (i) -> { player.playSound(1083); TrackDialog.create(player, eventManager, i.getCurrentDialog(), service, track).show(); }) .item(() -> { String trackType = stringSet.get("Track.Type.Normal"); if (track.getType() == TrackType.CIRCUIT) trackType = stringSet.format("Dialog.NewRacingDialog.CircultFormat", track.getCircultLaps()); return stringSet.format("Dialog.NewRacingDialog.TrackType", trackType); }, (i) -> { player.playSound(1083); i.getCurrentDialog().show(); }) .item(ListDialogItemRadio.create().itemText(stringSet.get("Dialog.NewRacingDialog.RacingType")) .selectedIndex(() -> setting.getRacingType().ordinal()) .item(stringSet.get("Racing.Type.Normal"), Color.CORNFLOWERBLUE, (dialogItem) -> setting.setRacingType(RacingType.NORMAL)) .item(stringSet.get("Racing.Type.Knockout"), Color.MAGENTA, (dialogItem) -> setting.setRacingType(RacingType.KNOCKOUT)) .onSelect((i) -> { player.playSound(1083); i.getCurrentDialog().show(); }).build()) .item(() -> { String format = stringSet.get("Dialog.NewRacingDialog.DepartureInterval"); int interval = setting.getDepartureInterval(); if (interval == 0) return String.format(format, stringSet.get("Common.None")); return String.format(format, stringSet.format("Time.Format.S", interval)); }, (i) -> { player.playSound(1083); RacingDepartureSettingDialog .create(player, eventManager, i.getCurrentDialog(), service, setting).show(); }) .item(ListDialogItemRadio.create().selectedIndex(() -> setting.getDeathRule().ordinal()) .itemText(stringSet.get("Dialog.NewRacingDialog.DeathRule")) .item(stringSet.get("Racing.DeathRule.WaitAndReturn"), Color.AQUA, (dialogItem) -> setting.setDeathRule(DeathRule.WAIT_AND_RETURN)) .item(stringSet.get("Racing.DeathRule.Knockout"), Color.FUCHSIA, (dialogItem) -> setting.setDeathRule(DeathRule.KNOCKOUT)) .onSelect((i) -> { player.playSound(1083); i.getCurrentDialog().show(); }).build()) .item(ListDialogItemCheck.create().itemText(stringSet.get("Dialog.NewRacingDialog.Limit")) .item(stringSet.get("Racing.Limit.AutoRepair"), Color.LIME, () -> setting.getLimit().isAllowAutoRepair()) .item(stringSet.get("Racing.Limit.InfiniteNitrous"), Color.RED, () -> setting.getLimit().isAllowInfiniteNitrous()) .item(stringSet.get("Racing.Limit.AutoFlip"), Color.BLUE, () -> setting.getLimit().isAllowAutoFlip()) .item(stringSet.get("Racing.Limit.ChangeVehicle"), Color.GOLD, () -> setting.getLimit().isAllowChangeVehicle()) .onSelect((i) -> { player.playSound(1083); RacingLimitDialog.create(player, eventManager, i.getCurrentDialog(), service, track, setting.getLimit()).show(); }).build()) .item(() -> { int min = track.getSetting().getMinPlayers(); int max = setting.getMaxPlayers(); if (min != 0 && max != 0) return stringSet.format("Dialog.NewRacingDialog.PlayersLimitFormat", min, max); return stringSet.get("Dialog.NewRacingDialog.PlayersLimit") + " " + stringSet.get("Dialog.NewRacingDialog.PlayersLimitNone"); }, (i) -> { player.playSound(1083); i.getCurrentDialog().show(); }) .item(() -> { String itemText = stringSet.get("Dialog.NewRacingDialog.Password"); if (StringUtils.isBlank(setting.getPassword())) return itemText + stringSet.get("Common.None"); return itemText + " " + setting.getPassword(); }, (i) -> { player.playSound(1083); i.getCurrentDialog().show(); }) .item(stringSet.get("Dialog.NewRacingDialog.Create"), (i) -> { Runnable startNewRacing = () -> { Racing racing = racingManager.createRacing(track, player, racingName[0]); racing.teleToStartingPoint(player); racing.setSetting(setting); }; player.playSound(1083); if (track.getCheckpoints().isEmpty()) return; if (racingManager.isPlayerInRacing(player)) { Racing racing = racingManager.getPlayerRacing(player); NewRacingConfirmDialog.create(player, eventManager, i.getCurrentDialog(), service, racing, () -> startNewRacing.run()); } else startNewRacing.run(); }) .build(); }
From source file:edu.cmu.cs.lti.util.general.BasicConvenience.java
public static void runAndReportMemoryUsage(String label, Runnable r) { System.gc();/*from ww w . jav a 2 s . c o m*/ try { Thread.sleep(10000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.gc(); long total = Runtime.getRuntime().totalMemory(); long free = Runtime.getRuntime().freeMemory(); System.out.println("STARTING " + label); System.out.println("Total (JVM): " + total / MB + " MB"); System.out.println("Free (before): " + free / MB + " MB"); long used = total - free; long usedAtStart = used; System.out.println("Used (before): " + used / MB + " MB"); r.run(); System.out.println("DONE " + label); System.gc(); try { Thread.sleep(10000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.gc(); total = Runtime.getRuntime().totalMemory(); free = Runtime.getRuntime().freeMemory(); long nowUsed = total - free; System.out.println("Free (after): " + free / MB + " MB"); System.out.println("Used (after): " + nowUsed / MB + " MB"); System.out.println("Difference: " + (nowUsed - usedAtStart) / MB + " MB"); }
From source file:nu.yona.server.util.TransactionHelper.java
@Transactional(value = TxType.REQUIRES_NEW) public void executeInNewTransaction(Runnable runnable) { runnable.run(); }
From source file:com.mtt.myapp.infra.schedule.InternalAsyncTaskService.java
@Async public void runAsync(Runnable runnable) { runnable.run(); }