Java Network Programming with Callback model

原创 2015年07月10日 09:47:49

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…

相关文章推荐

Java Network Programming with wait() & notifyAll()

Below codes use Object.wait() & Object.notifyAll() to construct the Producer & Consumer model. Ther...

【Golang】【 Network programming with Go】 Templates(模版)

翻译至http://jan.newmarch.name/go/template/chapter-template.html   第九章 很多编程语言都有字符串之间转换的机制,而GO语言则是...

Java Network Programming 笔记(1)

Java Network Programming 笔记 n5 一 网络基本概念 Chapter2 Basic Network Concepts 2.1 Networks keywords...

java Network programming using socket(1)

Java supports stream-based communication and packet-based communication,and the first is universal. ...

java network programming读书笔记(3) URL类和URI类

URL 类的java文档大概分为三部分:1 构造URL 通过设置URL的scheme, authroity, path 等2 当URL构造好后可以用各种getter 来获得它的各部分3 从URL中直接...
  • oney139
  • oney139
  • 2011年01月03日 10:58
  • 712
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java Network Programming with Callback model
举报原因:
原因补充:

(最多只允许输入30个字)