當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。