0

I am new to threading and have a JAVA code running on my Raspberry PI to control an arduino uno. in my JAVA code I have to threads that are initialised when the connection to the serial port is made. One of these threads is for writing data to the serial port, the other is for reading from it.

As I see it they do not share any data, so is it therefor necessary to implement any thread safety?

code snippet from the connection class:

 InputStream in = serialPort.getInputStream();
    OutputStream out = serialPort.getOutputStream();

    SerialReader sr = new SerialReader(in);
    Thread reader = new Thread(sr);
    reader.start();

    SerialWriter wr = new SerialWriter(out);
    Thread writer = new Thread(wr);
    writer.start();
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
  • 1
    can you post the code of your complete class – Sajith Edirisinghe Dec 01 '17 at 12:30
  • Most probably the SerialReader and SerialWriter will somehow interact with one another or with your main thread, and that's where you should think about thread safety. It's quite possible that you don't have to do anything special, but interactions between threads should be examined. – Ralf Kleberhoff Dec 01 '17 at 12:33
  • As previous commenter said, it's not quite possible to tell whether you need to strive for threadsafe code given just this snippet. You might want to, you might not want to. However, the thread safety is a concept that is applied regardless of number of threads, however small that might be. And by the way, your code snippet has 3 threads in it, not 2 (there is also a thread that starts the other two threads). – M. Prokhorov Dec 01 '17 at 12:48
  • Make sure you even need those Readers. Can a direct stream to stream copy work? https://stackoverflow.com/questions/4919690/how-to-read-one-stream-into-another –  Dec 02 '17 at 16:11

0 Answers0