Writing Data To The Same OutputStream from Multiple Threads

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Writing Data To The Same OutputStream from Multiple Threads

Postby win1for » Tue Jul 27, 2010 2:37 am

Hi Guys,

I am having a little problem with my application and need ideas and suggestions. In fact am a little bit stuck now and need ideas to go forward.

I have two robots and a GUI application running on a PC. Due to restrictions in an inbound connections, The GUI is directly connected to the two robots. The two robots send messages to each other through the GUI.

The first robot moves and i need to send its light sensor, motors, and color sensor status to the GUI for visualization. At the same time the first robot will be sending information to the second robot and vice versa.

My problem here is i have been sending the information/data from the first robot to the GUI and also to the second robot but it seems that some of the data is overwritten by others. For instance while the touch and color sensor information is being sent by the first robot to the GUI, it is possible that due to a condition, the first robot must at the same time send a data also to the second robot.

I am using the same output stream for sending the data.

For this reason the application is not functioning well. Sometimes the first robot has to move but don't because may be the data it sent to the second robot couldn't reach there for it to receive a reply in order to move.

I am asking what i must do so that the first robot can send data to the GUI and to the second robot in parallel(at the same time) without any lost of data?



This is what i have done so far but is not working properly.
Some of the data are over written by others


//NOTE:::: I PREFIX THE DATA I SEND TO THE GUI WITH A SPECIAL STRING.
//FOR INSTANCE IF THE DATA IS MEANT FOR THE GUI THEN I PREFIX WITH "gui".
//IF IT IS FOR THE SECOND ROBOT I PREFIX WITH "second".
//SO WHEN THE DATA REACHES THE GUI IT WILL DETERMINE
// WHETHER IT SHOULD KEEP THE DATA OR FORWARD IT TO THE SECOND ROBOT
// PLEASE AM USING JAVA leJOS NXJ FOR THE PROGRAMMING AND IT ALLOWS ON ONE CONNECTION BETWEEN
// TWO DEVICES. THAT IS WHY DATA FROM THE FIRST ROBOT TO THE SECOND ROBOT IS SENT THROUGH THE GUI ALSO.

These two classes are on the side of the first robot

// This class is a Thread that reads the touch, light sensor datas as well as the motors
// continuously and send them to the GUI application


Code: Select all
public class SensorReader implements Runnable{
       
     DataOutputStream out;
   
    // Additional data members declaration here

    public SensorReader( DataOutputStream out){

        this.out = out;
    }


    public void run(){
   
         int i = 1;
       
         while(i == 1){
         
               // Here i read all the data. The touch, light sensors, motors etc.
              // I concatenate all the data as a single string and i send them to the the GUI

              String Str = "gui";
              String data = touch + light + motor;
              this.out.writeChars(str + data);
             this.out.flush();
         }


   }

}



// This is the first robot class. In this class i send data to the second robot


Code: Select all

public class FirstRobot(){
 
  // A whole lot of declarations here
  BTConnection bt;
  DataOutputStream outStream;

 
  public FirstRobot(){
     bt = Connect();  //  Here it connects the Bluetooth
     outStream = bt.getOutputStream();  // the output stream is initialized
   
    // Here i initialized the SenserReader thread to start
    // working in order to read sensor information and send them to the GUI continuously.
     // I give it the outputstream as an argument to the constructor.

     SensorReader  reader = new SensorReader(outStream);
     Thread s = new Thread(reader);
     s.start();
   
     // other initialization here
   
   
   }


   //You can see also from the next method that both the sensor thread and this class are using
   // the same output stream to send data to the GUI.
   
   
  // In this methed the first robot send information to the second robot

  public void sendDataToSecondrobot(){
        String Str = "second";
       String data= "turnYourself";

       this.outStream.writeChars(str + data);
        this.outStream.flush();
   
   }
 

}






// main class

Code: Select all

public class MainAPP(){

   public static void main(String[] str){
   
   // declaring the first robot class here ....

  }
}



Thanks for your help.
win1for
Novice
 
Posts: 48
Joined: Tue Mar 02, 2010 11:22 pm

Postby gloomyandy » Tue Jul 27, 2010 10:36 am

The methods in DataOutputStream are not synchronized/thread safe so you need to provide synchronization around writes to them from multiple threads....

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4174
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff