0

I have 2 threads that can communicate with each other in the console(like a chat among each other). The thread only can send messages and the messages are user input. I want to send files(Ex: .pdf) among those two threads. How to do it?

In the code below you will find the two thread communication.

    import java.util.Scanner;

    public class Conversation {
        public static void main(String[] args) {

            Chat chat = new Chat();

           new Thread1(chat).start();
            new Thread2(chat).start();
        }
    }



    class Chat {
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        String str1,str2;
        int flag = 2;

        public synchronized void getTalk1() throws InterruptedException {
        if (flag==1) {
            wait();
        }
        System.out.print("User1: ");
        str1 = sc1.nextLine();
        if(str1.equalsIgnoreCase("bye")) {
            System.out.println("\nUser1 has left the chat. Conversation ended.");
            System.exit(0);
        }
        flag = 1;
        notify();
    }

        public synchronized void getTalk2() throws InterruptedException {
        if (flag == 2) {
            wait();
        }
        System.out.print("User2: ");
        str2 = sc2.nextLine();
        if(str2.equalsIgnoreCase("bye")) {
            System.out.println("\nUser2 has left the chat. Conversation ended.");
            System.exit(0);
        }
        flag = 2;
        notify();
        }
    }

    //Thread-1 class
    class Thread1 extends Thread {
    Chat chat;

    public Thread1(Chat chat) {
        this.chat = chat;
    }


    public void run() {
        try {
            while(true) {
                chat.getTalk1();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }

    //Thread-2 class
    class Thread2 extends Thread {
    Chat chat;


    public Thread2(Chat chat) {
        this.chat = chat;
    }


    public void run() {
        try {
            while(true) {
                chat.getTalk2();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }


This is the output on 2 thread communication : 2 threads communication

1 Answers1

1

If I understand this question correctly:

  • You have one thread writing to the console, and another thread reading from the console
  • You are using this for interthread communication??!!
  • You want to send the contents of a pdf via this inter-thread communication.

This is not at all a good way of sharing information between threads. You should never actually do this, other than for educational purposes. If you want to have two threads access a pdf you should just have a variable, either a byte array(If you want to share the contents of the pdf file), or a Path (if you want to share the location of the pdf). Make sure to synchronize accesses to these variables appropriately.

Answering the original question: Sharing the contents of a plain text file like this would be easy by simply using the existing code you have, and reading the plain text file as a string. If all you want to do is to share the location of a pdf file you can also send and receive this location as a string. If you want to share the contents of a pdf file, this gets much trickier, because consoles interpret special characters differently. For example some consoles may have special characters for clearing the screen, changing output colors, and even changing the font. The content of a pdf file may contain some of these special characters. Therefore you can't just send the contents as a byte array, and read a byte array from the console. The current standard way of dealing with these kind of problems is to use Base64 Encoding. This allows you to avoid any special characters in what you send over the console. The standard Java APIs for base64 encoding can be found in java.lang.Base64.Encoder. So to conclude, if you wanted to send the contents of a pdf over the console, you would first get the pdf as byte array, convert that byte array to base64, print out the base64 as a string, read the string in the other thread and then convert the base64 string back to a byte array. Just a reminder that you should use variables for this kind of interthread communication, and not the console as described above.

PiRocks
  • 1,708
  • 2
  • 18
  • 29
  • If want to send a PDF file location then how to do it? – singlepiece000 Aug 24 '19 at 06:59
  • Are you still insisting on sending this information through the console, or are you okay using some kind of Variable? All you really need is a variable accessible to both threads(for example a member variable of Chat). Then the thread that knows the location of the pdf can assign a location to that variable, and the other thread can read the location from that variable. Just make sure that any accesses to that variable are synchronized. – PiRocks Aug 24 '19 at 07:05
  • Thanks a lot for your help. I have another question. In my code, I created communication with two threads. But If I want to create communication with 3 threads, how can I do it ? – singlepiece000 Aug 24 '19 at 08:32