concurrency algorithm for file directory traversal

Source: Internet
Author: User

Problem: Calculate the size of the file under the specified directory.

This is a very simple question, just do a recursive on the line, the order algorithm:

     Public LongGetFileSize (Finalfile file) {        if(File.isfile ()) {returnfile.length (); }        Else{            LongTotal = 0; File files[]=File.listfiles (); if(Files = =NULL)return0;  for(File f:files) { Total+=GetFileSize (f); }            returnTotal ; }    }

Very simple, a recursive implementation, so now we think about the concurrency algorithm

Concurrency ideas: Each time a recursive operation, each time a thread to calculate the current directory file size, and then perform a recursive operation

Concurrency code:

Private LongGETFILESIZEBF (FinalExecutorservice Service,FinalFile file)throwsinterruptedexception, executionexception, timeoutexception{if(File.isfile ())returnfile.length (); Else{            FinalFile files[] =File.listfiles (); if(Files = =NULL)                return0; Else{                LongTotal = 0; Finallist<future<long>> tasks =NewArraylist<future<long>>();  for(FinalFile F:Files) {Tasks.add (Service.submit (NewCallable<long>() {@Override PublicLong Call ()throwsException {//TODO auto-generated Method stub
Recursively, the file size of the directory is returned
returnGETFILESIZEBF (service, F); } })); } for(FinalFuture<long>fl:tasks) { Total+=Fl.get (); } returnTotal ; } } }

There seems to be no problem, we come to the actual test;

We see, call get from the future to fetch data from the time, and no set timeout, the actual operation found that when the folder directory structure is simple, the directory tree is relatively shallow when it can run out, but the structure of the directory tree complex, ran for a long time or run out of results.

Analysis: Each thread will open a new thread to calculate the size of the sub-directory, this time, the thread will go to wait, waiting for the return of the child thread, this time there will be a situation, if the structure of the directory tree is complex, then many threads will enter the waiting state, waiting for the return value of the child thread, However, these parent threads still occupy the thread pool, but the child threads do not request a thread pool to execute, and this time they enter a deadlock .

Such as:

Optimization strategy: 1. Since the poolsize of the thread pool is not enough, then we will increase the size of poolsize, well, now we are facing a directory structure is not so complex, by simply adding poolsize can also be done, but very complex words will not be able to.

2. The reason we are creating

Concurrency algorithm for file directory traversal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.