Problem sending arrays from one method to another using Thread in Java

Asked

Viewed 66 times

1

I’m doing a program that simulates sending a message from the physical layer to another physical layer of the OSI (ISO reference) model of networks. My code has a method that sends "frames" separately. These frames are divided by spaces, and are integer arrays that have ASCII codes from a set of letters. However, when sending these "frames" to the method using a Thread, they are sent differently to each execution. Example: "ab Cde" sending, is received "ab", or "Cde", sometimes "cdeab" and rarely "ab Cde". What Thread is doing with sending the method?

Note: I decided to make the project available on Github. https://github.com/Hugorc10/Camada_Fisica_Redes.git

private void enviarQuadros(int[] quadroEnquadrado) {
        System.out.print("\nEnviar Quadros\n");
        switch (this.tipoDeEnquadramento) {
            case 0:
                int x = 0;
                int[] quadro;
                int index = 0;
                while (x < quadroEnquadrado.length) {
                    int cont = 0;
                    index = quadroEnquadrado[x];
                    quadro = new int[index];
                    quadro[cont] = index;
                    cont++;
                    x++;

                    for (int y = 0; y < index - 1; y++) {
                        quadro[cont] = quadroEnquadrado[x];
                        cont++;
                        x++;
                    }

                    int[] finalQuadro = quadro;
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            camadaFisicaTransmissora(finalQuadro);
                        }
                    });

                    thread.start();
                }
                break;
        }
    }

    private void camadaFisicaTransmissora(int[] quadro) {
        System.out.print("\nCamada Fisica Transmissora\n");
        camadaFisica.clear = false; // Impede de limpar a tela
        int[] fluxoBrutoDeBits = new int[0];

        if (binarioRadioButton.isSelected()) {
            try {
                camadaFisica.revalidate();
                camadaFisica.repaint();

                limparTela();
                camadaFisica.clear = false;

                fluxoBrutoDeBits = camadaFisicaTransmissoraCodificacaoBinaria(quadro);

                camadaFisica.bits = new String[fluxoBrutoDeBits.length];
                for (int i = 0; i < fluxoBrutoDeBits.length; i++)
                    camadaFisica.bits[i] = Integer.toBinaryString(fluxoBrutoDeBits[i]);

                camadaFisica.setEncodingTechnique(camadaFisica.BINARIO);

                for (String s : camadaFisica.bits)
                    bitsReceptor.append(s);

                System.out.print("Imprimindo bits receptor: " + Arrays.toString(camadaFisica.bits) + "\n");
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(this, "Entrada Invalida", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(this, "Excecao Desconhecida Ocorreu", "Exception", JOptionPane.ERROR_MESSAGE);
            }
        }

        meioDeComunicacao(fluxoBrutoDeBits);
    }

    public static int[] camadaFisicaTransmissoraCodificacaoBinaria(int[] quadro) {
        System.out.print("\nCamada Fisica Transmissora Codificacao Binaria\n");

        // Variavel que ira receber o comprimento (length) do array "quadro" dividido por quatro
        int n = quadro.length / 4;

        // Verifica  se o comprimento do quadro divido por quatro tem resto diferente de 0
        if (quadro.length % 4 != 0)
            n++;

        int[] bits = new int[n]; // Array que ira conter os inteiros com os bits armazenados no array "quadro"

        int index = 0;

        // Realiza loop ate o i ser menor que o comprimento (length) do vetor bits
        for (int i = 0; i < bits.length; i++) {
            bits[i] = quadro[index];
            index++;

            for (int y = 0; y < 3; y++) {
                if (index < quadro.length) {
                    bits[i] = bits[i] << 8; // Desloca 8 bits a esquerda
                    bits[i] = bits[i] | quadro[index]; // Compara os bits do array 'quadro' para o array 'bits'
                    index++;
                } // fim do if
            } // fim do for
        } // Fim do for

        System.out.print("Imprimindo bits: " + Arrays.toString(bits) + "\n");

        return bits;
    } // Fim do metodo camadaFisicaTransmissoraCodificacaoBinaria

This is a screenshot of the program I’m implementing with the received message.

Esta é uma imagem do programa que estou implementando com a mensagem recebida.

  • 1

    It would help if you could provide a [mcve] because then it would be possible to test your code and see the problem happening.

  • I decided to make the project available on Github. HTTPS: https://github.com/Hugorc10/RedesDeputadores.git

1 answer

0

Depending on the parameter int[] quadroEnquadrado multiple threads can be launched, even if all threads do exactly the same thing, nothing guarantees that they end up in the same order they started, in this situation, one thread may have more to do than the others, so it ends later, thus touching the order from "ab Cde" to "cdeab". In situations where you receive "ab" or "Cde" it is possible that the last finished thread replaces what the other threads did.

Susgestão:

Browser other questions tagged

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