• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread safe

 
brian kielbasa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am novice to j2ee technologies.

i have been playing around with jsp and servlets and then i ran into this issue.

i have a servlet and in that servlet i am creating an instance of a class (classA).i am calling some setters on that class. finally i am calling a method startThread() on that class.

my classA implements runnable interface and has few instance variables. most of them are of type string. i have implemented the run method. my run method calls some other method in the same class to do some processing. i have a startThread() method which creates instance of the Thread class and calls start on the thread.

Now is my code thread-safe ? As classA has instance variables and as there are setters to those variables i think they are not thread safe.

I would also appreciate if you can point me to a good book so that i can understand these thinks clearly.

thanks.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by brian kielbasa:
i am novice to j2ee technologies.

i have been playing around with jsp and servlets and then i ran into this issue.

i have a servlet and in that servlet i am creating an instance of a class (classA).i am calling some setters on that class. finally i am calling a method startThread() on that class.

my classA implements runnable interface and has few instance variables. most of them are of type string. i have implemented the run method. my run method calls some other method in the same class to do some processing. i have a startThread() method which creates instance of the Thread class and calls start on the thread.

Now is my code thread-safe ? As classA has instance variables and as there are setters to those variables i think they are not thread safe.

I would also appreciate if you can point me to a good book so that i can understand these thinks clearly.

thanks.


Threads is not just a J2EE technology -- it is core to Java itself.

But to answer your question, it isn't that simple. It really depends on how the threads behave. As for books, most general books, have a section on threads. (some are probably small enough that you can probably read the chapter in the store )

Henry
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:

As for books, most general books, have a section on threads. (some are probably small enough that you can probably read the chapter in the store )


Which just means that most books really gloss over a quite complex subject
 
krishnakumar ramamurthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have a single instance of the class with many threads accessing them then having unsynchronized setter methods will cause problems.
[ January 10, 2005: Message edited by: krishnakumar ramamurthy ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!