시나닷컴 면접문제-다선정 합병 파일

오늘 나는 시나닷컴에서 온 면접 메일을 받았는데, 그 안에 면접 문제가 하나 있었다.바로 여러 라인이 하나의 파일을 합쳐서 내가 꼬박 이틀 동안 이 문제를 해결하고 모두에게 공유하고 코드를 보도록 하는 것이다.
주 프로그램
package com.huang;

import org.apache.log4j.Logger;

import java.util.concurrent.*;

/**
 *    ,             ,           
 * Created by root on 16-9-1.
 */
public class Main {
    private static final Logger MAIN_LOGGER=Logger.getLogger(Main.class);
    private static final Logger CON_LOGGER=Logger.getLogger(Consumer.class);
    private static final Logger PRO_LOGGER=Logger.getLogger(Producer.class);
    private static final  ExecutorService POOL;
    //              
    private static final BlockingQueue BLOCKING_QUEUE = new LinkedBlockingQueue();

    static{
        //  CPU  
        int cpuNum=Runtime.getRuntime().availableProcessors();
        //   IO      ,   CPU      N+1
        POOL= Executors.newFixedThreadPool(2*cpuNum+1);
    }

    public static void main(String args[]){
        String in[]={"/tmp/a","/tmp/b","/tmp/c"};
        String out="/tmp/d";
        for(int i=0;inew Producer(BLOCKING_QUEUE,in[i],PRO_LOGGER));
        }
        POOL.shutdown();
        new Thread(new Consumer(BLOCKING_QUEUE,CON_LOGGER,out,in.length)).start();
    }

}

프로듀서
package com.huang;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;


/**
 *    
 * Created by root on 16-8-31.
 */
public class Producer implements Runnable{
    private BlockingQueue blockingQueue;
    private String filePath;
    private Logger logger;

    public Producer(BlockingQueue blockingQueue,String filePath,Logger logger) {
        this.blockingQueue=blockingQueue;
        this.filePath=filePath;
        this.logger=logger;
    }
    @Override
    public void run() {
        try {
            Byte[] data=FileOperate.toArray(FileOperate.getFileByte(filePath));
            blockingQueue.add(data);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        }
    }
}

컨슈머
package com.huang;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;

/**
 *   
 * Created by root on 16-8-31.
 */
public class Consumer implements Runnable{
    private final BlockingQueue blockingQueue;
    private Logger logger;
    private String outFile;
    private int num;
    public Consumer(BlockingQueue blockingQueue,Logger logger,String outFile,int num){
        this.blockingQueue=blockingQueue;
        this.logger=logger;
        this.outFile=outFile;
        this.num=num;
    }
    @Override
    public void run() {
        while(true){
            try {
                Byte[] bytes=blockingQueue.take();
                FileOperate.writeByte(outFile,bytes);
                if(--num==0)
                    break;
            } catch (InterruptedException e) {
                logger.error(e.getMessage(),e);
            } catch (IOException e) {
                logger.error(e.getMessage(),e);
            }
        }
    }
}

파일 작업 도구 클래스
package com.huang;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by root on 16-8-31.
 */
public class FileOperate {
    /**
     *   NIO        
     * @param filePath
     * @param bytes
     * @throws IOException
     */
    public static void  writeByte(String filePath,Byte[] bytes) throws IOException {
        Lock lock=new ReentrantLock();
        File outFile=new File(filePath);
        if(!outFile.exists())
            outFile.createNewFile();
        try(FileOutputStream fileOutputStream=new FileOutputStream(outFile,true); FileChannel fileChannel=fileOutputStream.getChannel();){
            ByteBuffer bf=ByteBuffer.wrap(getBytes(bytes));
            fileChannel.write(bf);
        }
    }

    /**
     *  NIO        
     * @param filePath
     * @return
     * @throws IOException
     */
    public static List getFileByte(String filePath) throws IOException {
        File inFile=new File(filePath);
        List bytes=null;
        try (FileInputStream fileInputStream= new FileInputStream(filePath); FileChannel fc=fileInputStream.getChannel();){
            bytes=new ArrayList<>();
            ByteBuffer bf=ByteBuffer.allocate(1024);
            while(fc.read(bf)!=-1) {
                bf.flip();
                while(bf.hasRemaining())
                    bytes.add(bf.get());
                bf.clear();
            }
        } catch (FileNotFoundException e) {

            throw new RuntimeException(filePath+"          ");
        }

        return bytes;
    }
    public static Byte[] toArray(List list){
        Byte[] result=new Byte[list.size()];
        for(int i=0;ireturn result;
    }
    public static byte[] getBytes(Byte[] bytes){
        byte[] result=new byte[bytes.length];
        for(int i=0;ireturn result;
    }
}

좋은 웹페이지 즐겨찾기