关闭

Java Network Programming with Callback model

标签: javamultithreadi
271人阅读 评论(0) 收藏 举报
分类:

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…
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11370次
    • 积分:361
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类