Sort a large collection while showing progress

后端 未结 7 795
广开言路
广开言路 2021-01-02 22:58

What is the best way to sort a collection while updating a progress bar? Currently I have code like this:

for (int i = 0; i < items.size(); i++)
{
    pro         


        
相关标签:
7条回答
  • 2021-01-02 23:49

    One simple approach on the progress bar is this.

    You can fix the number of calls to update the progress regardless of the item size by using mod. For example,

    public void run(int total) {
        int updateInterval = total / 10;
        System.out.println("interval = " + updateInterval);
        for(int i = 0; i < total; i++) {
            if(i % updateInterval == 0) {
                printProgress((float)i / total * 100f);
            }
            // do task here
        }
    }
    
    private void printProgress(float value) {
        System.out.println(value + "%");
    }
    

    This will update the progress bar 10 times (or 9? check the boundary conditions) whether the size is 10 or 10 million.

    This is just an example, adjust the values accordingly.

    0 讨论(0)
提交回复
热议问题