I have a code of a small Java program which generates MD5 checksums. It can work only with one file at a time. When given a file name it opens a modal dialog where it shows the progress bar and the overall status. At "100%" this dialog closes, the program displays generated MD5 and then is ready to take another file.
我有一个生成MD5校验和的小型Java程序代码。它一次只能使用一个文件。给定文件名时,它会打开一个模态对话框,其中显示进度条和整体状态。在“100%”此对话框关闭时,程序显示生成的MD5,然后准备采取另一个文件。
The function which generates MD5 is located in the class which implements Runnable
and this class is then passed to Thread
instance. Then this Thread
object is used as thread.start(); thread.join()
. The program works with only one file at a time. It works perfectly well when I use the function without threads.
生成MD5的函数位于实现Runnable的类中,然后将该类传递给Thread实例。然后这个Thread对象用作thread.start();的Thread.join()。该程序一次只能使用一个文件。当我使用没有线程的函数时,它非常有效。
My question is: what's the purpose of placing it in a separate thread and joining it?
我的问题是:将它放在一个单独的线程并加入它的目的是什么?
4 个解决方案
#1
0
As I have already mentioned in this answer, if you keep time-consuming tasks in the main GUI process of your program you should be aware that it will not respond to any user interaction as long as the task is running.
正如我在本回答中已经提到的,如果您在程序的主GUI过程中保留耗时的任务,您应该知道只要任务正在运行,它就不会响应任何用户交互。
In this page you can find some good practices regarding asynchronous operations in Java applications (it is focused on Swing, but can be applied broadly for other Java coding strategies). I would like to highlight one of the topics:
在此页面中,您可以找到有关Java应用程序中异步操作的一些良好实践(它主要关注Swing,但可以广泛应用于其他Java编码策略)。我想强调其中一个主题:
Rule #2: do not run time-consuming operations on the event thread.
规则#2:不要在事件线程上运行耗时的操作。
I'll keep repeating this: Swing uses a single thread for all GUI events. If your event handler is uploading a multi-megabyte file across the net, those events will be delayed until you're done. There is a balancing act involved with this rule: some operations (such as getting the size of a file) are so fast that they won't interrupt the user. However, that's not a reason to be cavalier, because some day your user will be getting the size of a file that resides on a fileserver that has an intermittent network connection.
我将继续重复这一点:Swing对所有GUI事件使用单个线程。如果您的事件处理程序正在通过网络上传一个多兆字节的文件,那么这些事件将被延迟,直到您完成为止。这个规则涉及一个平衡行为:某些操作(例如获取文件的大小)非常快,以至于它们不会中断用户。但是,这并不是一个骑士的理由,因为有一天你的用户将获得驻留在具有间歇性网络连接的文件服务器上的文件的大小。
As you have mentioned, the dialog is kept opened while the MD5 is being generated. That should be the reason why the developer used a separate thread -- even knowing the program can only handle one file at a time, by keeping the computation isolated in its own thread will avoid the main GUI from being frozen while the MD5 is computed.
如您所述,在生成MD5时,对话框保持打开状态。这应该是开发人员使用单独线程的原因 - 即使知道程序一次只能处理一个文件,通过将计算隔离在自己的线程中将避免在计算MD5时冻结主GUI。
However, Thread.join()
allows one thread to wait for the completion of another. That said, the developer may have used this strategy to prevent additional user interaction until the MD5 calculation is complete. Or, what is also possible, to wait for the MD5 calculation to finish before the main program (or GUI) finishes (closes).
但是,Thread.join()允许一个线程等待另一个线程的完成。也就是说,开发人员可能已经使用此策略来防止额外的用户交互,直到MD5计算完成。或者,也可以在主程序(或GUI)完成(关闭)之前等待MD5计算完成。
#2
1
Then this Thread object is used as thread.start(); thread.join().
然后这个Thread对象用作thread.start();的Thread.join()。
Is it really doing something like:
它真的做的事情是这样的:
Thread thread = new Thread(new MD5ProcessorRunnable(...));
thread.start();
thread.join();
...
This is a very strange pattern if so. There is no reason why the code couldn't call the MD5 processor directly in the current thread.
如果是这样,这是一个非常奇怪的模式。没有理由为什么代码不能直接在当前线程中调用MD5处理器。
I suspect that it is doing something equivalent to:
我怀疑它正在做的事情相当于:
MD5ProcessorRunnable processor = new MD5ProcessorRunnable(...);
Thread progressBarThread = new Thread(new ProgressBar(processor));
progressBarThread.start();
Thread md5Thread = new Thread(processor);
md5Thread.start();
md5Thread.join();
progressBarThread.join();
...
This starts a thread which updates the progress bar and only returns GUI control to the user once both threads complete.
这将启动一个更新进度条的线程,并在两个线程完成后仅向用户返回GUI控件。
#3
0
Thread.join()
is defined as the following:
Thread.join()定义如下:
Waits for this thread to die.
等待这个线程死亡。
Thread joins are usually used to make the main thread wait for other threads to finish their processes. Of course this is assuming the join was called from the main thread.
线程连接通常用于使主线程等待其他线程完成其进程。当然这是假设从主线程调用连接。
I am guessing the code blocks user interaction until the MD5 calculation is complete.
我猜测代码会阻止用户交互,直到MD5计算完成。
#4
0
Without seeing the code, my guess is one thread calculates the checksum and the other handles the progress bar, updating its value when necessary, but you need to add the code to your post to let us help you.
在没有看到代码的情况下,我的猜测是一个线程计算校验和,另一个线程处理进度条,必要时更新其值,但是您需要将代码添加到帖子中以便我们帮助您。
The join method allows one thread to wait for the completion of another, so it's the way to finish the execution of the application ensuring both threads end
join方法允许一个线程等待另一个线程的完成,因此它是完成应用程序执行的方法,确保两个线程结束
#1
0
As I have already mentioned in this answer, if you keep time-consuming tasks in the main GUI process of your program you should be aware that it will not respond to any user interaction as long as the task is running.
正如我在本回答中已经提到的,如果您在程序的主GUI过程中保留耗时的任务,您应该知道只要任务正在运行,它就不会响应任何用户交互。
In this page you can find some good practices regarding asynchronous operations in Java applications (it is focused on Swing, but can be applied broadly for other Java coding strategies). I would like to highlight one of the topics:
在此页面中,您可以找到有关Java应用程序中异步操作的一些良好实践(它主要关注Swing,但可以广泛应用于其他Java编码策略)。我想强调其中一个主题:
Rule #2: do not run time-consuming operations on the event thread.
规则#2:不要在事件线程上运行耗时的操作。
I'll keep repeating this: Swing uses a single thread for all GUI events. If your event handler is uploading a multi-megabyte file across the net, those events will be delayed until you're done. There is a balancing act involved with this rule: some operations (such as getting the size of a file) are so fast that they won't interrupt the user. However, that's not a reason to be cavalier, because some day your user will be getting the size of a file that resides on a fileserver that has an intermittent network connection.
我将继续重复这一点:Swing对所有GUI事件使用单个线程。如果您的事件处理程序正在通过网络上传一个多兆字节的文件,那么这些事件将被延迟,直到您完成为止。这个规则涉及一个平衡行为:某些操作(例如获取文件的大小)非常快,以至于它们不会中断用户。但是,这并不是一个骑士的理由,因为有一天你的用户将获得驻留在具有间歇性网络连接的文件服务器上的文件的大小。
As you have mentioned, the dialog is kept opened while the MD5 is being generated. That should be the reason why the developer used a separate thread -- even knowing the program can only handle one file at a time, by keeping the computation isolated in its own thread will avoid the main GUI from being frozen while the MD5 is computed.
如您所述,在生成MD5时,对话框保持打开状态。这应该是开发人员使用单独线程的原因 - 即使知道程序一次只能处理一个文件,通过将计算隔离在自己的线程中将避免在计算MD5时冻结主GUI。
However, Thread.join()
allows one thread to wait for the completion of another. That said, the developer may have used this strategy to prevent additional user interaction until the MD5 calculation is complete. Or, what is also possible, to wait for the MD5 calculation to finish before the main program (or GUI) finishes (closes).
但是,Thread.join()允许一个线程等待另一个线程的完成。也就是说,开发人员可能已经使用此策略来防止额外的用户交互,直到MD5计算完成。或者,也可以在主程序(或GUI)完成(关闭)之前等待MD5计算完成。
#2
1
Then this Thread object is used as thread.start(); thread.join().
然后这个Thread对象用作thread.start();的Thread.join()。
Is it really doing something like:
它真的做的事情是这样的:
Thread thread = new Thread(new MD5ProcessorRunnable(...));
thread.start();
thread.join();
...
This is a very strange pattern if so. There is no reason why the code couldn't call the MD5 processor directly in the current thread.
如果是这样,这是一个非常奇怪的模式。没有理由为什么代码不能直接在当前线程中调用MD5处理器。
I suspect that it is doing something equivalent to:
我怀疑它正在做的事情相当于:
MD5ProcessorRunnable processor = new MD5ProcessorRunnable(...);
Thread progressBarThread = new Thread(new ProgressBar(processor));
progressBarThread.start();
Thread md5Thread = new Thread(processor);
md5Thread.start();
md5Thread.join();
progressBarThread.join();
...
This starts a thread which updates the progress bar and only returns GUI control to the user once both threads complete.
这将启动一个更新进度条的线程,并在两个线程完成后仅向用户返回GUI控件。
#3
0
Thread.join()
is defined as the following:
Thread.join()定义如下:
Waits for this thread to die.
等待这个线程死亡。
Thread joins are usually used to make the main thread wait for other threads to finish their processes. Of course this is assuming the join was called from the main thread.
线程连接通常用于使主线程等待其他线程完成其进程。当然这是假设从主线程调用连接。
I am guessing the code blocks user interaction until the MD5 calculation is complete.
我猜测代码会阻止用户交互,直到MD5计算完成。
#4
0
Without seeing the code, my guess is one thread calculates the checksum and the other handles the progress bar, updating its value when necessary, but you need to add the code to your post to let us help you.
在没有看到代码的情况下,我的猜测是一个线程计算校验和,另一个线程处理进度条,必要时更新其值,但是您需要将代码添加到帖子中以便我们帮助您。
The join method allows one thread to wait for the completion of another, so it's the way to finish the execution of the application ensuring both threads end
join方法允许一个线程等待另一个线程的完成,因此它是完成应用程序执行的方法,确保两个线程结束