Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Share Java-based multithreading file case     - Copy and paste in Linux terminal and Vim (Linux)

- How to upgrade to Oracle 11g Oracle 12c (Database)

- Linux system monitoring tool set cpu (Linux)

- JavaScript in null undefined summary (Linux)

- Linux system server network security management tips (Linux)

- Two strokes to improve development productivity Struts2 (Programming)

- Android studio multi-channel ultra-compact version of the package (Programming)

- Linux basic introductory tutorial ---- Software Installation under Linux (Linux)

- 64-bit Oracle Linux recompiled Hadoop-2.2.0 (Server)

- Java gets the current system time System.currentTimeMillis () (Programming)

- pureftpd basis: Install, configure, implement, anonymous logon (Linux)

- Upgrading to Debian 7.6 glibc 2.15 (Linux)

- Shell command line operation (Linux)

- History and Statistics tuptime use tools to view Linux server system boot time (Server)

- Python dictionary applications get built-in method (Programming)

- Distributed File System FastDFS deployment (Server)

- C ++ Supplements - Smart Pointers (Programming)

- To learn linux security (Linux)

- To install minimize RHEL / CentOS 7 (Linux)

- Ubuntu 15.04 Linux install anti-virus software Antiviral 0.2 (Linux)

 
         
  Share Java-based multithreading file case
     
  Add Date : 2017-04-04      
         
       
         
  Scene Description: A folder has many files and folders required to read all the files in the folder, and text printouts.

Case Study: Suppose there are 10 files in the folder, then we will iterate over all the documents printed output on it. But if there are 100, 1000, 10,000, or even more? At this time perhaps we should consider the use of multiple threads to solve this problem myself, read files in batches.

Java implementation ideas:

(1) List < File> readFiles (String filePath)

(2) subFiles (List < File> fileList, int startIndex, int endIndex)

(3) readFile (File file)

(4) FavThreadUtil (List < File> fileList) .run ()

Dry Code:

(A) FavFileUtil.java, the above step (1) (2) (3) read with file

package com.test.favthread.util;
  
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
  
public class FavFileUtil {
      
    public static List < File> getFileList (String filePath) {
        List < File> fileList = new ArrayList < File> ();
        File file = new File (filePath);
        if (! file.isDirectory ()) {
            System.out.println ( "file [" + file.getName () + "]:" + file.getAbsolutePath ());
            fileList.add (file);
        } Else {
            System.out.println ( "folder [" + file.getName () + "]:" + file.getAbsolutePath ());
            File [] files = file.listFiles ();
            for (int i = 0; i < files.length; i ++) {
                if (! files [i] .isDirectory ()) {
                    System.out.println ( "file [" + files [i] .getName () + "]:");
                    fileList.add (files [i]);
                } Else if (files [i] .isDirectory ()) {
                    getFileList (files [i] .getAbsolutePath ());
                }
            }
              
        }
        return fileList;
    }
      
      
    public static List < File> subFiles (List < File> list, int startIndex, int endIndex) {
        if (endIndex> list.size ()) {
            return list.subList (startIndex, list.size ());
        }
        return list.subList (startIndex, endIndex);
    }
      
      
    public static void readFile (File file) throws IOException {
        // Read the file
        FileInputStream is = new FileInputStream (file);
        // Get the character stream file
        InputStreamReader isr = new InputStreamReader (is);
        // Buffer
        BufferedReader br = new BufferedReader (isr);
        String line = null;
        while ((line = br.readLine ())! = null) {
            System.out.println ( ":::" + line);
        }
        br.close ();
    }
  
}

 (B) FavThreadUtil.java, the above step (4) by operating the file list

package com.test.favthread.util;
  
import java.io.File;
import java.io.IOException;
import java.util.List;
  
public class FavThreadUtil implements Runnable {
      
    private List < File> fileList;
      
    public FavThreadUtil (List < File> fileList) {
        this.fileList = fileList;
    }
  
    @Override
    public void run () {
        for (File file: fileList) {
            try {
                FavFileUtil.readFile (file);
            } Catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
  
}

 (C) Main.java, Java Main program run tests.


package com.test.favthread.main;
  
import java.io.File;
import java.util.List;
  
import com.test.favthread.util.FavFileUtil;
import com.test.favthread.util.FavThreadUtil;
  
public class Main {
  
    public static void main (String [] args) {
        List < File> fileList = FavFileUtil.getFileList ( "D: \\ log");
        List < File> aList = fileList.subList (0, 10);
        List < File> bList = fileList.subList (10, 20);
        FavThreadUtil afavThreatUtil = new FavThreadUtil (aList);
        FavThreadUtil bfavThreatUtil = new FavThreadUtil (bList);
        new Thread (afavThreatUtil, "Thread 1") start ().;
        new Thread (bfavThreatUtil, "Thread 2") start ().;
    }
  
}
     
         
       
         
  More:      
 
- Linux unpack the tar file to a different directory (Linux)
- Usage logs Python library (Programming)
- RedHat Redis Linux installation (Database)
- build Android environment on Ubuntu 12.04 (Server)
- HAProxy performance under high concurrency (Server)
- High-performance JavaScript reflows and repaints (Programming)
- SSH port forwarding application (Server)
- Objective-C basic program structure (Programming)
- Function Getting the Linux shell (Programming)
- to compile FFmpeg In Ubuntu (Linux)
- How to manage your to-do list with the Go For It on Ubuntu (Linux)
- MongoDB3.0.x version of the user authorization profile (stand-alone environment) (Database)
- iOS Sensor Development - add to APP phone password, fingerprint security authentication (Programming)
- OpenWRT environment to build (Linux)
- Linux System Getting Started Learning: modify environment variables in Linux PATH (Linux)
- CentOS 6.5_x64 install Oracle 11g R2 (Database)
- Oracle archive log summary (Database)
- Ubuntu 15.10 under Python + Apache + CGI fully configured (Server)
- Installation and Configuration JDK8 In CentOS 7 (Linux)
- Linux / Raspberry Pi using the following command-line based web browser (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.