Java Network Programming with Callback model

Below codes use threads to merge files with callback model.

  1. Merge process : CallbackMain
package org.mantis.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CallbackMain {
    private static String FILEPATH = "C:/Users/jb48021/Workspace/OscarFrontEnd/TestApp/Thrift/properties/logFilesReceived1.txt";
    private static BufferedWriter FILEOUTPUT;
    private static int THREADSNO;
    private static int COUNTER = 0;
    private static boolean[] THREADFLAGS;

    static {
        try {
            FILEOUTPUT = new BufferedWriter(new FileWriter(FILEPATH));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public CallbackMain() {
    }

    public static void WriteLog(List<String> listParam) throws IOException,
            InterruptedException {
        synchronized (FILEOUTPUT) {
            while (!listParam.isEmpty()) {
                FILEOUTPUT.write(listParam.remove(0));
                FILEOUTPUT.newLine();
                COUNTER++;
            }
            FILEOUTPUT.flush();
        }
        // System.out.println("Main COUNTER : " + COUNTER);
    }

    public static void updateFlag(boolean flag, int id) {
        synchronized (THREADFLAGS) {
            THREADFLAGS[id] = flag;
        }
    }

    public String charToString(char[] buffer) {
        if (buffer == null)
            return "";

        StringBuffer string = new StringBuffer(66);
        for (char c : buffer) {
            // System.out.print(c);
            string.append(c);
        }
        return string.toString();
    }

    public static String getFILEPATH() {
        return FILEPATH;
    }

    public static void setFILEPATH(String fILEPATH) {
        FILEPATH = fILEPATH;
    }

    public static BufferedWriter getFILEOUTPUT() {
        return FILEOUTPUT;
    }

    public static void setFILEOUTPUT(BufferedWriter fILEOUTPUT) {
        FILEOUTPUT = fILEOUTPUT;
    }

    public static int getCOUNTER() {
        return COUNTER;
    }

    public static void setCOUNTER(int cOUNTER) {
        COUNTER = cOUNTER;
    }

    public static int getTHREADSNO() {
        return THREADSNO;
    }

    public static void setTHREADSNO(int tHREADSNO) {
        THREADSNO = tHREADSNO;
        THREADFLAGS = new boolean[THREADSNO];
        while(tHREADSNO-- > 0){
            THREADFLAGS[tHREADSNO] = false;
        }
    }

    public static boolean[] getTHREADFLAGS() {
        return THREADFLAGS;
    }
}
  1. Files reader thread : CallbackThread
package org.mantis.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CallbackThread implements Runnable {
    private String filePath;
    private List<String> buffer1 = new ArrayList<String>();
    private List<String> buffer2 = new ArrayList<String>();
    private BufferedReader fileInput;
    private int counter = 0;
    private int threadID = 0;
    private String inputBuffer;
    private boolean endFlag = false;
    private static int MAXLINE = 10;

    public CallbackThread() {
    }

    public CallbackThread(int id) {
        this.threadID = id;
        filePath = "C:/Users/jb48021/Workspace/OscarFrontEnd/TestApp/Thrift/properties/logFiles"
                + id + "Received.txt";
        try {
            fileInput = new BufferedReader(new FileReader(new File(filePath)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void putListFromFile(int i) throws IOException, InterruptedException {
        while (i < MAXLINE) {
            if ((inputBuffer = fileInput.readLine()) != null) {
                buffer1.add(i++, inputBuffer);
            } else {
                endFlag = true;
                break;
            }
        }
        counter += i;
       CallbackMain.WriteLog(buffer1);
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        while (!endFlag) {
            try {
                putListFromFile(0);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            CallbackMain.updateFlag(true, threadID);
            fileInput.close();
            boolean[] flag = CallbackMain.getTHREADFLAGS();
            int i = CallbackMain.getTHREADSNO();
            if (i-- > 0)
                while (flag[i--])
                    if(i < 0) {
                        CallbackMain.getFILEOUTPUT().close();
                        System.out.println(Thread.currentThread().getName() + " closed main outPut stream!");
                        break;
                    }
        } catch (IOException e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();

        System.out.println("Thread " + Thread.currentThread().getName()
                + " comes to end, processed " + counter + " records, used "
                + (endTime - startTime) + " millis...");
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public int getThreadID() {
        return threadID;
    }

    public void setThreadID(int threadID) {
        this.threadID = threadID;
    }
}
  1. UnitTest
package org.mantis.test;

import java.io.IOException;

public class UnitTest {

    public static void callbackInterface() {
        Thread t = null;
        CallbackThread cThread = null;
        int MAXTHREADS = 3;
        CallbackMain.setTHREADSNO(MAXTHREADS);

        int id;
        for (id = 0; id < MAXTHREADS;) {
            cThread = new CallbackThread(id);
            t = new Thread(cThread);
            t.setName("thread" + id++);
            t.start();
        }

    }
    public static void main(String[] args) { 
         UnitTest.callbackInterface(); 
    }
}
  1. result:
    Thread thread1 comes to end, processed 51 records, used 7 millis…
    Thread thread2 comes to end, processed 3403 records, used 84 millis…
    thread0 closed main outPut stream!
    Thread thread0 comes to end, processed 12546 records, used 115 millis…
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值