Java 一个线程过早停止,而与CyclicBarrier无关

Java 一个线程过早停止,而与CyclicBarrier无关,java,multithreading,socketchannel,cyclicbarrier,Java,Multithreading,Socketchannel,Cyclicbarrier,我知道下面的代码可能看起来很粗俗,但我对这些东西不熟悉,只是为了让它正常工作,我尝试了一切 问题:尽管我使用的是CyclicBarrier(可能是错误的方式),但其中一个线程(看起来总是相同的)停止得太快,并打印出了它的向量,在11条“传入连接”消息中,有1条不存在。我的循环的最后一次迭代可能有严重的问题,但我似乎找不到确切的原因。。现在程序只是循环等待处理最后一个连接。 公共类VectorClockClient实现可运行{ /* *属性 */ /* *客户号是存储区,以提供快速服务 *例如,

我知道下面的代码可能看起来很粗俗,但我对这些东西不熟悉,只是为了让它正常工作,我尝试了一切

问题:尽管我使用的是CyclicBarrier(可能是错误的方式),但其中一个线程(看起来总是相同的)停止得太快,并打印出了它的向量,在11条“传入连接”消息中,有1条不存在。我的循环的最后一次迭代可能有严重的问题,但我似乎找不到确切的原因。。现在程序只是循环等待处理最后一个连接。

公共类VectorClockClient实现可运行{
/*
*属性
*/
/*
*客户号是存储区,以提供快速服务
*例如,当线程自己的
*时钟只需要增加。
*/
私人int客户号;
私有文件configFile,inputFile;
int[]矢量时钟;
/*
*建造师
*@param
*-文件配置
*-整数行
*-文件输入
*-int客户端
*/
公共VectorClockClient(文件配置、int行、文件输入、int客户端){
/*
*确保文件句柄不为null,并且
*行号是有效的。
*/
如果(配置!=null&&line>=0&&input!=null){
configFile=config;
输入文件=输入;
clientNumber=行;
/*
*将数组大小设置为在
*配置文件并用零值初始化。
*/
vectorClock=新的整数[客户端];
对于(int i=0;i=0){
//System.out.println(“[”+(clientNumber+1)
//+“]向“+结果+”发送消息;
试一试{
SocketChannel client=SocketChannel.open();
client.configureBlocking(true);
client.connect(
新的InetSocketAddress(“localhost”,
解析端口(结果));
//ByteBuffer buf=ByteBuffer.allocateDirect(32);
//buf.put((字节)0xFF);
//buf.flip();
//向量时钟[clientNumber]+=1;
//int numbyteswrited=client.write(buf);
字符串obj=Integer.toString(clientNumber+1);
ObjectOutputStream oos=新建
ObjectOutputStream(
client.socket().getOutputStream());
oos.writeObject(obj);
oos.close();
}
捕获(例外e){
public class VectorClockClient implements Runnable {
/*
 * Attributes
 */

/*
 * The client number is store to provide fast
 * array access when, for example, a thread's own
 * clock simply needs to be incremented.
 */
private int clientNumber;
private File configFile, inputFile;
int[] vectorClock;

/*
 * Constructor
 * @param
 * - File config
 * - int line
 * - File input
 * - int clients
 */
public VectorClockClient(File config, int line, File input, int clients) {
    /*
     * Make sure that File handles aren't null and that
     * the line number is valid.
     */
    if (config != null && line >= 0 && input != null) {
        configFile = config;
        inputFile = input;
        clientNumber = line;
        /*
         * Set the array size to the number of lines found in the
         * config file and initialize with zero values.
         */
        vectorClock = new int[clients];
        for (int i = 0; i < vectorClock.length; i++) {
            vectorClock[i] = 0;
        }
    }
}

private int parsePort() {
    int returnable = 0;
    try {
        FileInputStream fstream = new FileInputStream(configFile.getName());
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine = "";
        for (int i = 0; i < clientNumber + 1; i++) {
            strLine = br.readLine();
        }
        String[] tokens = strLine.split(" ");
        returnable = Integer.parseInt(tokens[1]);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("[" + clientNumber + "] returned with " + returnable + ".");
    return returnable;
}

private int parsePort(int client) {
    int returnable = 0;
    try {
        FileInputStream fstream = new FileInputStream(configFile.getName());
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine = "";
        for (int i = 0; i < client; i++) {
            strLine = br.readLine();
        }
        String[] tokens = strLine.split(" ");
        returnable = Integer.parseInt(tokens[1]);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return returnable;
}

private int parseAction(String s) {
    int returnable = -1;
    try {
        FileInputStream fstream = new FileInputStream(configFile.getName());
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String[] tokens = s.split(" ");
        if (!(Integer.parseInt(tokens[0]) == this.clientNumber + 1)) {
            return -1;
        }
        else {
            if (tokens[1].equals("L")) {
                vectorClock[clientNumber] += Integer.parseInt(tokens[2]);
            }
            else {
                returnable = Integer.parseInt(tokens[2]);
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return returnable;
}

/*
 * Do the actual work.
 */
public void run() {
    try {
        InitClients.barrier.await();
    }
    catch (Exception e) {
        System.out.println(e);
    }
    int port = parsePort();
    String hostname = "localhost";
    String strLine;
    ServerSocketChannel ssc;
    SocketChannel sc;
    FileInputStream fstream;
    DataInputStream in;
    BufferedReader br;
    boolean eof = false;
    try {
        ssc = ServerSocketChannel.open();
        ssc.socket().bind(new InetSocketAddress(hostname, port));
        ssc.configureBlocking(false);
        fstream = new FileInputStream("input_vector.txt");
        in = new DataInputStream(fstream);
        br = new BufferedReader(new InputStreamReader(in));

        try {
            InitClients.barrier.await();
        }
        catch (Exception e) {
            System.out.println(e);
        }

        while (true && (eof == false)) {
            sc = ssc.accept();

            if (sc == null) {
                if ((strLine = br.readLine()) != null) {
                    int result = parseAction(strLine);
                    if (result >= 0) {
                        //System.out.println("[" + (clientNumber + 1)
                        //+ "] Send a message to " + result + ".");
                        try {
                            SocketChannel client = SocketChannel.open();
                            client.configureBlocking(true);
                            client.connect(
                                    new InetSocketAddress("localhost",
                                    parsePort(result)));
                            //ByteBuffer buf = ByteBuffer.allocateDirect(32);
                            //buf.put((byte)0xFF);
                            //buf.flip();
                            //vectorClock[clientNumber] += 1;
                            //int numBytesWritten = client.write(buf);
                            String obj = Integer.toString(clientNumber+1);
                            ObjectOutputStream oos = new 
                                    ObjectOutputStream(
                                    client.socket().getOutputStream());
                            oos.writeObject(obj);
                            oos.close();
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                else {
                    eof = true;
                }
            }
            else {
                ObjectInputStream ois = new 
                        ObjectInputStream(sc.socket().getInputStream());
                String clientNumberString = (String)ois.readObject();
                System.out.println("At {Client[" + (clientNumber + 1)
                        + "]}Incoming connection from: "
                        + sc.socket().getRemoteSocketAddress()
                        + " from {Client[" + clientNumberString + "]}");
                sc.close();
            }
            try {
                InitClients.barrier.await();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    printVector();
}

private void printVector() {
    System.out.print("{Client[" + (clientNumber + 1) + "]}{");
    for (int i = 0; i < vectorClock.length; i++) {
        System.out.print(vectorClock[i] + "\t");
    }
    System.out.println("}");
}