0

I am trying to restrict the amount of clients on my server,so i just sending message from my server when it is full and print it in my client by ending process for him.But as i use multiple threads i cant stop my writeMessage thread

Tried AtomicBoolean,but i didnt work

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;
    private AtomicBoolean running = new AtomicBoolean(true);

    public Client(String address, int port) {
        this.address = address;
        this.port = port;
        net = new Net(address, port);
        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()) {
                if ("full".equals(str)) {
                    System.out.println("server is full");
                    running.set(false);
                    break;
                } else {
                    System.out.println(str);
                    str = net.receive();
                }
            }
            net.offService();
        }, "readMessage");
        readMessage.start();
    }

    private void writeMessage() {
        writeMessage = new Thread(() -> {
            while (running.get()) {
                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.set(false);
                    } else {
                        net.send("(" + time + ") " + name + " says : " + userWord + "\n");
                    }
                } catch (IOException e) {
                    LOGGER.error(e);
                }
            }
            net.offService();
        }, "writeMessage");
        writeMessage.start();
    }
}

I want to change running to "false",so the writeMessage thread wont work if it gets message "full" from the server

  • Perhaps consider setting it to volatile https://stackoverflow.com/questions/13582395/sharing-a-variable-between-multiple-different-threads – JCompetence May 27 '19 at 07:19
  • I have tried,it is not working – sanbayev98 May 27 '19 at 07:35
  • Can you add a System.out.println("writeMessage"); and System.out.println("readMessage"); in your code. I think you will see something like this: writeMessage, writeMessage, readMessage .... Why do you not execute a write and after that a read in the same thread? – Thomas Krieger May 27 '19 at 08:44
  • Unfortunately,it is not helping.Checked my running variable in writeMessage and it isnt changing – sanbayev98 May 27 '19 at 12:47
  • Simplify your program so that we can run it and see what happens. Use BlockingQueue instead of Net for interthread communication. – Alexei Kaigorodov May 27 '19 at 18:21

0 Answers0