我一直是用Java處理圖像的程序。然而,在這個圖像處理過程中,同時運行的同時(包括進度條等)的swing GUI會凍結,直到處理完成。我試過多線程程序來解決這個問題,讓處理在後臺運行,但它似乎不工作。如何防止GUI在多線程中互相干擾
下面是我一直在使用的一些代碼。
public class GUIThread implements Runnable {
public static final int CHOOSER_HUB = 0;
public static final int LAUNCH_IMAGE_PREVIEWER = 1;
public static final int DISABLE_PREVIEWER_BUTTONS = 2;
public static final int MEMORY_USAGE_WINDOW = 3;
private int guiNumber;
// ==========================================================================
// | START CODE |
// ==========================================================================
/**
* Runs begins the thread. This method is also for pre-run configuration,
* but so far there is none of that.
*
* @param GUINumber
* The GUI number, indicating which GUI to start.
*/
public void start(int GUINumber) {
DebugMessenger.out("Starting new thread for GUI");
guiNumber = GUINumber;
run();
}
/**
* Starts the thread, and runs a method determined by what guiNumber was set.
*/
@Override
public void run() {
DebugMessenger.out("Thread running");
if (guiNumber == GUIThread.CHOOSER_HUB)
createChooserHub();
if (guiNumber == GUIThread.LAUNCH_IMAGE_PREVIEWER)
createPreviewer();
if (guiNumber == GUIThread.DISABLE_PREVIEWER_BUTTONS)
disablePreviewerButtons();
if (guiNumber == GUIThread.MEMORY_USAGE_WINDOW)
createMemoryUsageWindow();
}
/* More code, including methods called by run() */
}
要啓動我的程序,我只需運行以下代碼。
public class Main {
/**
* Main method. Starts the threads and lets them roll.
* @param args
*/
public static void main(String[] args) {
// start threads
DebugMessenger.out("Starting Main");
GUIThread guiThread = new GUIThread();
guiThread.start(GUIThread.LAUNCH_IMAGE_PREVIEWER);
if(Config.DEBUG_OUTPUT_ENABLED) {
memBarThread = new GUIThread();
memBarThread.start(GUIThread.MEMORY_USAGE_WINDOW);
}
DebugMessenger.out("Main complete");
}
}
正如你所看到的,我做了關於儘可能簡單的多線程,但它似乎仍然是,當我運行程序和圖像預覽開始處理,內存使用率窗口凍結。我怎樣才能防止這一點?
工作就像一個魅力!將第一行更改爲'public class GUIThread extends Thread {',將'start()'方法重命名爲'startThread()',並用'start()'替換'run()'。 – user119567
@ user119567:我很樂意幫助你。 – fireandfuel
您可以使用Runnable啓動線程。實現Runnable比擴展Thread更好。 –