当前位置: 首页>>代码示例>>Java>>正文


Java Task类代码示例

本文整理汇总了Java中javafx.concurrent.Task的典型用法代码示例。如果您正苦于以下问题:Java Task类的具体用法?Java Task怎么用?Java Task使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Task类属于javafx.concurrent包,在下文中一共展示了Task类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: monitorTask

import javafx.concurrent.Task; //导入依赖的package包/类
/**
 * Monitors a given task, and updates the current status and progress accordingly.
 *
 * @param task the {@link Consumer<ProgressUpdater>} to monitor
 */
public void monitorTask(final Consumer<ProgressUpdater> task) {
    final Task<Void> progressTask = new Task<Void>() {
        @Override
        public Void call() throws InterruptedException {
            task.accept((progress, message) -> Platform.runLater(() -> {
                this.updateProgress(progress, PROGRESS_MAX);
                statusProperty.set(message);
            }));
            return null;
        }
    };

    progressProperty.unbind();
    progressProperty.bind(progressTask.progressProperty());

    new Thread(progressTask).start();
}
 
开发者ID:ProgrammingLife2017,项目名称:hygene,代码行数:23,代码来源:StatusBar.java

示例2: ViewerAboutDialog

import javafx.concurrent.Task; //导入依赖的package包/类
public ViewerAboutDialog(Stage stage) {
    ImageView image = ImageUtils.createImageView("/icons/spy128.png");
    this.setCenter(image);
    this.setOnMouseClicked(e -> stage.close());

    image.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                Desktop.getDesktop().browse(URI.create(homeUrl));
                return null;
            }
        };
        task.setOnFailed(e -> {
            Throwable exception = e.getSource().getException();
            if (exception != null) {
                Log.error(exception);
                ViewerAlert.exceptionAlert(exception);
            }
        });

        new Thread(task).start();
    });

}
 
开发者ID:Glavo,项目名称:ClassViewer,代码行数:26,代码来源:ViewerAboutDialog.java

示例3: shouldShowProgressBarWhileGivenTaskRun

import javafx.concurrent.Task; //导入依赖的package包/类
@Test
public void shouldShowProgressBarWhileGivenTaskRun() throws Exception {
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            Thread.sleep(1000);
            return null;
        }
    };

    statusBar.bindProgressTo(task);
    view.checkProgressBarIsNotVisible();

    new Thread(task).start();

    view.waitUntilProgressBarIsVisible();
    view.waitUntilProgressBarIsNotVisible();
}
 
开发者ID:AntonioGabrielAndrade,项目名称:LIRE-Lab,代码行数:19,代码来源:StatusBarAcceptanceTest.java

示例4: partialSync

import javafx.concurrent.Task; //导入依赖的package包/类
public void partialSync(){
    Task<Void> syncNewMails = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            while (true) {
                try {
                    Thread.sleep(5000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                GmailOperations.getNewInboxMails();
                GmailOperations.getNewSentMails();
                GmailOperations.getNewDraftMails();
                GmailOperations.getNewTrashMails();

            }
        }
    };

    Thread backgroundThread = new Thread(syncNewMails);
    backgroundThread.setDaemon(true);
    backgroundThread.start();

}
 
开发者ID:ashoknailwal,项目名称:desktop-gmail-client,代码行数:25,代码来源:SynchronizeMessages.java

示例5: backgroundSearch

import javafx.concurrent.Task; //导入依赖的package包/类
private void backgroundSearch(String query){
    if(query != null) {
        if (!GmailMessages.activeSearches.isEmpty()) {
            for (Service<Void> service : GmailMessages.activeSearches) {
                service.cancel();
            }
        }
        Service<Void> search = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                try {
                    GmailOperations.listSearchMessages(query);
                    GmailOperations.getSearchMessages(query);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        };
        GmailMessages.activeSearches.add(search);
        search.start();
    }
}
 
开发者ID:ashoknailwal,项目名称:desktop-gmail-client,代码行数:24,代码来源:MainUI3Controller.java

示例6: runAsyncTask

import javafx.concurrent.Task; //导入依赖的package包/类
public static <T> CompletableFuture<T> runAsyncTask(Callable<T> task) {
	Task<T> jfxTask = new Task<T>() {
		@Override
		protected T call() throws Exception {
			return task.call();
		}
	};

	CompletableFuture<T> ret = new CompletableFuture<T>();

	jfxTask.setOnSucceeded(event -> ret.complete(jfxTask.getValue()));
	jfxTask.setOnFailed(event -> ret.completeExceptionally(jfxTask.getException()));
	jfxTask.setOnCancelled(event -> ret.cancel(false));

	threadPool.execute(jfxTask);

	return ret;
}
 
开发者ID:sfPlayer1,项目名称:Matcher,代码行数:19,代码来源:Gui.java

示例7: loadThreadMes

import javafx.concurrent.Task; //导入依赖的package包/类
public void loadThreadMes(Label lbl) {
    Task tMes = new Task<Void>() {
        DecimalFormat df = new DecimalFormat("#.00€");

        @Override
        protected Void call() throws Exception {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    lbl.setText(df.format(mesatarja()) + "");
                }
            });
            return null;
        }
    };
    Thread mesT = new Thread(tMes);
    mesT.setDaemon(true);
    mesT.start();
}
 
开发者ID:urankajtazaj,项目名称:Automekanik,代码行数:20,代码来源:DritarjaKryesore.java

示例8: keyPress

import javafx.concurrent.Task; //导入依赖的package包/类
public static void keyPress(String key)
{
	if(playerEntity != null)
	{
		playerEntity.keyPress(key);
	}
	else
	{
		if(key.compareTo("R")==0)
		{
			Task<Void> mainLoop = new MainLoop();
			Thread mainLoopThread = new Thread(mainLoop);
			mainLoopThread.start();
		}
	}
}
 
开发者ID:GabrielCT,项目名称:Chase-R,代码行数:17,代码来源:InputHandler.java

示例9: start

import javafx.concurrent.Task; //导入依赖的package包/类
public void start(final Stage initStage) {        
    initStage.getIcons().add(new Image(APPLICATION_ICON));
    initStage.setResizable(false);
    initStage.setAlwaysOnTop(true);
    final Task<ObservableList<String>> friendTask = new Task<ObservableList<String>>() {
        @Override
        protected ObservableList<String> call() throws InterruptedException {
            ObservableList<String> titles = FXCollections.<String>observableArrayList();
            ObservableList<String> loadings = FXCollections.observableArrayList(
                    "Validate Version", "Running", "Initialize application", "Preparing ...", "Please Wait", "Done"
            );

            updateMessage("Checking Version");
            for (int i = 0; i < loadings.size(); i++) {
                Thread.sleep(400);
                updateProgress(i + 1, loadings.size());
                String nextFriend = loadings.get(i);
                titles.add(nextFriend);
                updateMessage("Chess Bot 1.0.0 ...  " + nextFriend);
            }

            Thread.sleep(400);

            return titles;
        }
    };

    showSplash(initStage, friendTask, () -> showMainStage());
    new Thread(friendTask).start();
}
 
开发者ID:mhusam,项目名称:ChessBot,代码行数:31,代码来源:ChessBotAppSplash.java

示例10: removeStore

import javafx.concurrent.Task; //导入依赖的package包/类
@FXML
private void removeStore() {
	final int selectedIndex = indexView.getSelectionModel().getSelectedIndex();

	if (selectedIndex == -1) {
		return;
	}

	createTask(new Task<Boolean>() {

		@Override
		protected Boolean call() throws Exception {
			cache.removeStore(indexView.getSelectionModel().getSelectedItem().getId());
			cache.reset();
			Platform.runLater(() ->indexes.remove(selectedIndex));
			return true;
		}
	}, true);
}
 
开发者ID:nshusa,项目名称:rsam-gui,代码行数:20,代码来源:StoreController.java

示例11: stopMonitoring

import javafx.concurrent.Task; //导入依赖的package包/类
/**
 * stop the monitoring
 */
public void stopMonitoring() {
  if (monitortask == null)
    return;
  // TODO use better symbol e.g. icon
  setWatchDogState("X", I18n.get(I18n.HALTED));
  setMenuItemDisable(I18n.OBD_START_MENU_ITEM, false);
  setMenuItemDisable(I18n.OBD_START_WITH_LOG_MENU_ITEM,
      simulatorPane != null);
  setMenuItemDisable(I18n.OBD_TEST_MENU_ITEM, false);
  setMenuItemDisable(I18n.OBD_HALT_MENU_ITEM, true);
  Task<Void> task = new Task<Void>() {
    @Override
    public Void call() {
      try {
        obdApp.stop();
      } catch (Exception e) {
        handleException(e);
      }
      return null;
    }
  };
  new Thread(task).start();
}
 
开发者ID:BITPlan,项目名称:can4eve,代码行数:27,代码来源:JavaFXDisplay.java

示例12: defragment

import javafx.concurrent.Task; //导入依赖的package包/类
@FXML
private void defragment() {
	if (!cache.isLoaded()) {
		Dialogue.showWarning("Load your cache first.").showAndWait();
		return;
	}

	createTask(new Task<Boolean>() {

		@Override
		protected Boolean call() throws Exception {
			if (!cache.defragment()) {
				Platform.runLater(() -> Dialogue.showWarning("Failed to defragment cache.").showAndWait());
			}
			return true;
		}
	}, true);
}
 
开发者ID:nshusa,项目名称:rsam-gui,代码行数:19,代码来源:StoreController.java

示例13: createTask

import javafx.concurrent.Task; //导入依赖的package包/类
@Override
protected Task<Void> createTask() {
	return new Task<Void>() {
		@Override
		protected Void call() throws SQLException, InterruptedException {
			try (Connection connection = DataSourceFactory.getConfiguredDataSource(properties, textPassword.getText()).getConnection()) {
				if (isCancelled()) return null;

				LOGGER.info("Successfully connected to the database.");
				linkifier = new Linkifier(connection, properties.getProperty("schema"));
				if (isCancelled()) return null;

				linkifier.estimatePk();
				if (isCancelled()) return null;

				linkifier.estimateFk();
				LOGGER.info("Estimated foreign key constraint count: " + linkifier.getFkCount());
			}

			return null;
		}
	};
}
 
开发者ID:janmotl,项目名称:linkifier,代码行数:24,代码来源:Events.java

示例14: doAsyncWork

import javafx.concurrent.Task; //导入依赖的package包/类
/**
 * 
 * Perform an async call
 * 
 * @param action
 * @param success
 * @param error
 */
public static <T extends Object> void doAsyncWork(Supplier<T> action, Consumer<T> success,
		Consumer<Throwable> error) {
	Task<T> tarefaCargaPg = new Task<T>() {
		@Override
		protected T call() throws Exception {
			return action.get();
		}

		@Override
		protected void succeeded() {
			success.accept(getValue());
		}

		@Override
		protected void failed() {
			error.accept(getException());
		}
	};
	Thread t = new Thread(tarefaCargaPg);
	t.setDaemon(true);
	t.start();
}
 
开发者ID:jesuino,项目名称:java-ml-projects,代码行数:31,代码来源:AppUtils.java

示例15: doBlockingAsyncWork

import javafx.concurrent.Task; //导入依赖的package包/类
public static <T extends Object> void doBlockingAsyncWork(Scene scene, Supplier<T> action, Consumer<T> success,
		Consumer<Throwable> error) {
	Parent originalRoot = scene.getRoot();
	Runnable blockScreen = () -> originalRoot.setDisable(true);
	Runnable unblockScreen = () -> originalRoot.setDisable(false);

	Task<T> task = new Task<T>() {
		@Override
		protected T call() throws Exception {
			blockScreen.run();
			return action.get();
		}

		@Override
		protected void succeeded() {
			unblockScreen.run();
			success.accept(getValue());
		}

		@Override
		protected void failed() {
			unblockScreen.run();
			error.accept(getException());
		}
	};
	Thread t = new Thread(task);
	t.setDaemon(true);
	t.start();
}
 
开发者ID:jesuino,项目名称:java-ml-projects,代码行数:30,代码来源:AppUtils.java


注:本文中的javafx.concurrent.Task类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。