使用AtomicInteger进行客户计数

时间:2019-05-25 16:43:24

标签: java sockets

我试图计算连接到服务器的客户端数量,但是在增加服务器数量时遇到了一些问题。使用AtomicInteger且它仅增加一次。因此,请检查下面的代码并提供一些建议以解决问题。< / p>

public class Client {

    private static final Logger LOGGER = LogManager.getLogger();
    private BufferedReader consoleReader;
    private String name;
    private String address;
    private int port;
    private Thread writeMessage, readMessage;
    private ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
    private LocalTime dTime = zonedDateTime.toLocalTime();
    private Net net;

    public Client(String address, int port) {
        this.address = address;
        this.port = port;
        net = new Net(address, port);
        ClientAmountGenerator.increment();
        System.out.println(ClientAmountGenerator.getCounter());
        consoleReader = new BufferedReader(new InputStreamReader(System.in));
        printName();
        readMessage();
        writeMessage();
    }

    private void printName() {
        System.out.println("Print your name:");
        try {
            name = consoleReader.readLine();
            while (NameValidator.nameIsValid(name)) {
                System.out.println("Name should contain more than 1 letter");
                name = consoleReader.readLine();
            }
            net.send(name + "\n");
        } catch (IOException e) {
            LOGGER.error(e);
        }
    }

    private void readMessage() {
        readMessage = new Thread(() -> {
            String str = net.receive();
            while (net.isConnected()) {
                System.out.println(str);
                str = net.receive();
            }
            net.offService();
        }, "readMessage");
        readMessage.start();
    }

    private void writeMessage() {
        writeMessage = new Thread(() -> {
            boolean running = true;
            while (running) {
                String userWord;
                try {
                    String time = dTime.getHour() + ":" + dTime.getMinute() + ":" + dTime.getSecond();
                    userWord = consoleReader.readLine();
                    if (userWord.equals("quit")) {
                        net.send(userWord + "\n");
                        ClientAmountGenerator.decrement();
                        running = false;
                    } else {
                        net.send("(" + time + ") " + name + " says : " + userWord + "\n");
                    }
                } catch (IOException e) {
                    LOGGER.error(e);
                }
            }
            net.offService();
        }, "writeMessage");
        writeMessage.start();
    }
}

public class ClientAmountGenerator {
    private static AtomicInteger counter = new AtomicInteger(0);

    public static void increment() {
        counter.incrementAndGet();
    }

    public static void decrement() {
        counter.decrementAndGet();
    }

    public static int getCounter(){
        return counter.get();
    }
}

public class ClientMain {

    private static String Ip = "localhost";
    private static int port = 9011;

    public static void main(String[] args) {
        new Client(Ip, port);
    }

}

如何修复它,并在每次运行客户端时增加它,并在它等于“退出”时减少它,谢谢

0 个答案:

没有答案