Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3020
  • Last Modified:

how to make InputStream and OutputStream thread safe.

well, have multiple clients connecting to a Socket Server,
some kind simple chat application.

are the InputStream and OutputStream thread safe,
or do i have do something special to ensure that.

thanx
0
Knightley
Asked:
Knightley
  • 6
  • 4
  • 3
1 Solution
 
sciuriwareCommented:
If they address different files there is no problem.

;JOOP!
0
 
girionisCommented:
> are the InputStream and OutputStream thread safe,

They are since each time you will be getting the input/output stream of the coket connecting to the server.
0
 
KnightleyAuthor Commented:
a server connected with several clients is always thread safe?
i rememer somebody told me it is not, but forget where.

so when i start a server:
1. accept several clients by creating a thread for each.
2. do some input and output
3. do some System.out.println

everything i did above are thread safe?

thanx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sciuriwareCommented:
The IO is, the thread operations may not be.

;JOOP!
0
 
girionisCommented:
> everything i did above are thread safe?

If you do not deal with any instance variables that are shared between threads then it is. The following is thread safe:

public SocketServer server = new SocketServer(port);
public Socket connection;

while (true)
{
   connection = server.accept();
   ClientConnection client = new ClientConnection(connection);
   Thread t = new Thread(client);
   t.start();
}

public class ClientConnection
{
   private Socket socket = null;
   private InputStream is = null;
   private OutputStream os = null;

   public ClientConnection(Socket s)
   {
      socket = s;
   }

   public void run()
   {
      is = socket.getInputStream();
      os = socket.getOutputStream();
      while (...)
      ....
      ....
   }
}

The following is not thread safe

public SocketServer server = new SocketServer(port);
public Socket connection;

// This is not thread safe
public int notThreadSafeVariable = 0;

while (true)
{
   connection = server.accept();
   ClientConnection client = new ClientConnection(connection, notThreadSafeVariable);
   Thread t = new Thread(client);
   t.start();
}

public class ClientConnection
{
   private Socket socket = null;
   private InputStream is = null;
   private OutputStream os = null;
   private int notThreadSafeVariable = 0;

   public ClientConnection(Socket s, int notThreadSafeVariable)
   {
      socket = s;
      this.notThreadSafeVariable = notThreadSafeVariable;
   }

   public void run()
   {
      is = socket.getInputStream();
      os = socket.getOutputStream();
      while (...)
      ....
      ....
   }

   public int getNotThreadSafeVariable()
   {
      return notThreadSafeVariable--;
   }

   public void incrementNotThreadSafeVariable()
   {
      notThreadSafeVariable++;
   }
}

In the example above the "notThreadSafeVariable" is not safe since you cannot guarantee that each thread will see the variable incremented or decremented by exactly 1.
0
 
girionisCommented:
Also the ClientConnection class should implement Runnable. I forgot about it.
0
 
sciuriwareCommented:
:<
0
 
KnightleyAuthor Commented:
sorry sciuriware,

i received the CleanUp post, and give the points in a hurry.

in my defense, though your anwsers are correct and swift,
and should receive the points.

they are just so SHORT, i am afraid i overlooked them.

sorry again, if there is a way to send you the points,
tell me how. if not, i hope i could make it up to you somehow.

cu
0
 
sciuriwareCommented:
No problem, you can't always  :)
0
 
sciuriwareCommented:
This is an interesting forum,
sometimes you get points for almost nothing,
sometimes you work hard on a solution and ... nope.
Don't worry!

;JOOP!
0
 
girionisCommented:
You can still ask a question in Community Support to reopen the question and assign the points to sciuriwar.
0
 
sciuriwareCommented:
I looked this over. Let it be as it is. Next question please ....
0
 
KnightleyAuthor Commented:
i think its alright. girionis, u help a lot too.

next time u anwser my question i will give it to
scicuriware. :)

thanx to u both.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now