progressbar in a thread does not update its UI until the work was done in the main thread

前端 未结 3 1182
北恋
北恋 2020-12-21 18:08

i am cutting a big file into blocks, and want to display the rate of the progress. when i click startCut Button, here is the code to execute:

FileInputStream         


        
3条回答
  •  情话喂你
    2020-12-21 18:54

    You've got two problems here:

    • You're doing long-running work in the Swing dispatcher thread, which means you're stopping it from processing events. (Try moving windows around etc - it will fail.)
    • You're updating the UI from the wrong thread at point A. It sounds like you're getting away with this at the moment, but it's still a bug.

    You should use SwingWorker or SwingUtilities to address both of these issues. Basically, you mustn't access the UI from a non-UI thread, and you mustn't do long-running work on a UI thread. See the Swing concurrency tutorial for more information.

提交回复
热议问题