Organize message from Thread

Asked

Viewed 156 times

0

good night.

I need your help in something kind of simple, I’m doing a chat via RMI, the implementation of the program is already working less as I want, but I’m breaking my head in a simple print. I need to arrange the exit so it stays the following way:

Chat: type a message

so-and-so says: typed message

My program is a thread to read messages from the server, and it is precisely these messages that I am unable to organize.

currently the messages are coming out as follows:

Chat: Enter a message

Chat: so-and-so says: message typed

I mean, I’d like to follow the sequence

Message

Meshing

I know it’s kind of dumb that doubt, but I’m really not getting to do this kkkk follows my code:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Scanner;

public class ChatCliente{

private static Scanner scanner;

public static void main(String[] args) {
    try {

        ChatIF chat = (ChatIF) Naming.lookup("rmi://localhost:1098/Chat"); //Buscando a Interface no Servidor

        String nome;
        String msg = "";
        scanner = new Scanner(System.in);
        int cont = chat.getmensagens().size();

        //Recebendo o nome do Usuario
        System.out.println("---------------------------------------------");
        System.out.println("       SEJA BEM VINDO AO CHAT                ");
        System.out.println("---------------------------------------------");
        System.out.print("[Chat] Por Favor, Digite seu nome: ");
        nome = scanner.nextLine();

        //Thread responsavel por ler as mensagens do servidor
        Thread thread = new Thread(new Runnable() {
            int cont = chat.getmensagens().size();

            @Override
            public void run() {
                try {
                    while(true){
                        if (chat.getmensagens().size() > cont){


   System.out.println(chat.getmensagens().get(chat.getmensagens().size()-1));
                            cont++;
                        }
                    }
                }catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        System.out.println("---------------------------------------------");
        System.out.println("Ola, " + nome + "! Voce agora esta conectado!");
        System.out.println("---------------------------------------------");

        thread.start();

        //Envia as mensagens  para o servidor
        while(true){
            System.out.print("[Chat]"+": ");
            msg = scanner.nextLine();           
            chat.setMensagem(nome+" diz"+": "+msg);
        }


    } catch (Exception e) {
        System.out.println("Ocorreu um erro: " + e);
    }
}
}

1 answer

0

The problem is that the two threads are printing on the same console.

At this point you print the chat string without line break.

while(true){
    System.out.print("[Chat]"+": "); // Aqui você imprime o "[Chat]:" sem quebra de linhas
    msg = scanner.nextLine();           
    chat.setMensagem(nome+" diz"+": "+msg);
}

At that other point, you print the message in the same console as the previous message.

System.out.println(chat.getmensagens().get(chat.getmensagens().size()-1));

That’s why you’re seeing "[Chat]: message". The threads are mixed. To get organized you need to put threads in different classes and create a main for each one. Then you need to run the two main. This way you will have two consoles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.