我一直是用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更好。 –