Solved

how to make InputStream and OutputStream thread safe.

Posted on 2006-11-01
14
2,582 Views
Last Modified: 2011-08-18
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
Comment
Question by:Knightley
  • 6
  • 4
  • 3
14 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 17849895
If they address different files there is no problem.

;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 17850063
> 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
 
LVL 1

Author Comment

by:Knightley
ID: 17851690
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 24

Expert Comment

by:sciuriware
ID: 17851784
The IO is, the thread operations may not be.

;JOOP!
0
 
LVL 35

Accepted Solution

by:
girionis earned 30 total points
ID: 17856234
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 17856251
Also the ClientConnection class should implement Runnable. I forgot about it.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18046470
:<
0
 
LVL 1

Author Comment

by:Knightley
ID: 18046619
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 18046706
No problem, you can't always  :)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18046721
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
 
LVL 35

Expert Comment

by:girionis
ID: 18051966
You can still ask a question in Community Support to reopen the question and assign the points to sciuriwar.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 18052087
I looked this over. Let it be as it is. Next question please ....
0
 
LVL 1

Author Comment

by:Knightley
ID: 18053327
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple java question 3 56
how do i compare an object based on two fields 6 54
nextBoolean(double p) for Random class 3 40
Problem to Alipay 10 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question