Java – the ability to get progress on future objects

Refer to Java util. Concurrent package and future interface. I noticed (unless I was wrong) that the function of starting long tasks and querying progress can only be used with the swingworker implementation class

This raises the following questions:

Is there a way to start long tasks in the background and allow other threads to check progress in non GUI, non swing applications (imaging console applications)? In my opinion, there is no reason to limit this capability to swing / GUI applications Otherwise, the only available option I see is through executorservice:: submit, which returns a future object However, the future interface does not allow you to monitor progress

Solution

Obviously, future objects will only block and receive results

The runnable or callable object you submit must know how to provide this progress (percentage completion, number of attempts, status (enumeration?) And publish it as an API call to the object itself or in some lookup resources (in memory mapping or database if necessary) For simplicity, I tend to like the object itself, especially because you most likely need a handle (ID) to find the object or a reference to the object itself

This does mean that you have three thread operations 1 is the actual work, 1 is blocked while waiting for the result, and 1 is the monitoring thread The last one can be shared according to your requirements

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>