所以不是「睡眠」一個線程,如Thread.sleep();
,只允許進程在另一個線程上運行,並使新線程睡眠Thread.sleep();
而不是原始Thread
。這可能嗎?如何在不同的線程上運行程序的某些部分並將線程引用變量?
這裏是我的方法,我想在一個名爲processesThread
新Thread
運行:
private void Processes() throws IOException, InterruptedException {
// New Thread "processesThread" will start here.
Runtime rt = Runtime.getRuntime();
List<Process> processes = new ArrayList<Process>();
// "runnableTogether" will be the number that the user inputs in the GUI.
switch (runnableTogether) {
case 4:
processes.add(rt.exec("C:/Windows/System32/SoundRecorder.exe"));
case 3:
processes.add(rt.exec("C:/Windows/System32/taskmgr.exe"));
case 2:
processes.add(rt.exec("C:/Windows/System32/notepad.exe"));
case 1:
processes.add(rt.exec("C:/Windows/System32/calc.exe"));
Thread.sleep(5000);
destroyProcesses(processes);
break;
default:
System.exit(0);
break;
}
// New Thread "processesThread" will end here.
}
這可能嗎?如果是這樣,怎麼樣?
我已經研究開始新的Threads
,但我無法弄清楚如何讓它與我的程序一起工作。
編輯:我希望能使用類似這種方法的東西:
Thread processesThread = new Thread() {
public void run() {
// Code here.
}
};
processesThread.start();
任何想法?
不太確定你要在這裏做什麼,但我建議閱讀有關ExecutorService(http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors -and-threadpoolexecutor.html?ModPagespeed = noscript)(http://stackoverflow.com/questions/2104676/java-executor-best-practices)和一些官方文檔(http://docs.oracle.com/javase/6 /docs/api/java/util/concurrent/ExecutorService.html),也許這可以幫助你,但你需要澄清你想要做什麼:) – AlejandroVK
@AjjandroVK基本上,我不想要那個線程我的大部分程序都在運行,進入睡眠狀態。只是新的線程。 – knorberg
先看看提供的鏈接,他們會在您的場景中爲您提供幫助,您需要擁有可由管理員控制的線程池。對於Executor來說這是一個完美的場景。否則,你可以自己實現,創建一個線程管理器,可以根據你指定的任何標準來休眠/啓動/停止每個線程。 – AlejandroVK