AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I use the Java API in a Thread-Safe Manner?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer
View previous topic :: View next topic  
Author Message
Al Koch
Guest





PostPosted: Wed Jun 22, 2005 12:12 am    Post subject: How do I use the Java API in a Thread-Safe Manner? Reply with quote



How do I use the Java API in a Thread-Safe Manner?

Hello,

I am writing a Java servlet and am new to coding for thread synchronization.
I suspect that there must be an "obvious" solution to the following problem
but after sevearl days of research I can't find it!

It is my understanding that, in general, the J2SE 1.4 API is *not*
thread-safe. There are some APIs where the Sun documentation states that a
Class's methods are thread-safe (such as StringBuffer) but in general, most
of the text and web search references I have found have stated that the API
in general is not thread-safe. That means that there must be at least one
(and it's probably most) API that is not thread-safe and let me just call
such an unsafe method "UnsafeAPI()". My question is, how do I use
UnsafeAPI() and write code to make my code thread-safe?

For example, if I have the following code:

public final class MyClass {
private int i;
private int j;
private int k;

public finale void MyMethod(int i, int j, int k)
{
this.i = i;
this.j = j;
this.k = k;
}
}

I have a thread safety issue here. Assignement to an int is atomic so no
one of the three assignment statements in MyMethod() can be interrupted by
another thread. However, the *sequence of all three assignments* certainly
can be interrupted so I am vulnerable to a synchronization issue. I could
either fix this proiblem by adding the "sychronized" modifier to the
MyMethod declaration, or I could do the following:


public final class MyClass {
private int i;
private int j;
private int k;

public finale void MyMethod(int i, int j, int k)
{
synchronize(this) {
this.i = i;
this.j = j;
this.k = k;
}
}
}

At this point I am thread safe. However, assume that I also need to call
UnsafeAPI(). Now my code looks like:

public final class MyClass {
private int i;
private int j;
private int k;

public finale void MyMethod(int i, int j, int k)
{
synchronize(this) {
this.i = i;
this.j = j;
this.k = k;
UnsafeAPI(); // this is any J2SE 1.4 API that, in general, is not
thread safe!
}
}
}

The synchronize(this) guarantees that no other thread can enter *my code
block* in MyMethod() but (as far as I can tell) it in no way prevents
another thread from entering UnsafeAPI() from another block of code, either
in my servlet or even in another application altogether.

There must be some way to block a thread from entering UnsafeAPI() because
otherwise it would be impossible to write a thread safe application, but I
sure can't find a way to do this. Can someone explain how this is done? If
you can also tell me how/where you learned how to do this I would be very
interested because I can't locate any such information!)

Thank you!
Al

[email]AlKoch (AT) MyRealBoxREMOVEALLTHESECHARS (DOT) com[/email]


Back to top
Oliver Wong
Guest





PostPosted: Thu Aug 11, 2005 2:49 pm    Post subject: Re: How do I use the Java API in a Thread-Safe Manner? Reply with quote




"Al Koch" <AlKoch (AT) MyRealBox (DOT) com> wrote

Quote:

The synchronize(this) guarantees that no other thread can enter *my code
block* in MyMethod() but (as far as I can tell) it in no way prevents
another thread from entering UnsafeAPI() from another block of code,
either
in my servlet or even in another application altogether.

There must be some way to block a thread from entering UnsafeAPI() because
otherwise it would be impossible to write a thread safe application, but I
sure can't find a way to do this. Can someone explain how this is done?
If
you can also tell me how/where you learned how to do this I would be very
interested because I can't locate any such information!)

I don't think you can stop applications that you didn't write from
calling APIs. On the bright side, every Java program runs in its own virtual
machine, so synchronization across different applications usually won't be
an issue.

As for using APIs in a threadsafe manner, all you can do is "be
careful". Create a wrapper class around the API which does nothing except
provide thread safety, and discipline yourself to always use your wrapper
class instead of the API directly.

- Oliver



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.