The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads entering a non-synchronized static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads entering a non-synchronized static method?" Watch "Threads entering a non-synchronized static method?" New topic
Author

Threads entering a non-synchronized static method?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
First off, I'm sure I'm going to screw up trying to explain what I'm asking so bare with me
What I'm curious about is how Threads act when they are accessing static methods in another class.
Besides wanting to know this for the test, I'm concerned wtih this on a practical level. I develop web
applications with Java and JSP and in a sense all the users are running their own Threads but they could be accessing a static method that they all share. I usually write a utility class with static methods to handle miscellaneous stuff such as formatting dates, etc. Normally the methods aren't very long but I started wondering what would happen if the static methods were very time consuming. In other words what would happen if another Thread(another web user) accessed a non-synchronized static method before the other Thread was finished doing stuff in that method.
1) My first question probably has to deal more with a local method than Threads. I'm wondering first off when a Thread would enter a static method and set a local variable to one of the method's arguments could this local variable be changed by another incoming thread? I've tried some code to test this and don't think it can, but not sure if maybe my code is just flawed. So for example if you have:

Is it possible after Thread A enters someMethod() and sets val, that a Thread B could come along and enter the static method and change the value of val such that Thread A would then end up having the value of val changed by what Thread B set it to ? Since they are both in this method at the same time, I'm not sure what happens.
2) What about the scenario with a static class variable:

In this scenario I would definitely think classVar could be modified by another Thread entering someMethod and changing the value of classVar after another Thread had already changed it. Although I can't get this behavior to be evident in the code below in TestThreading2. I really have a tough time with this Threading stuff configuring timers and sleep() calls to generate the possibilities that could happen. Maybe someone could help me here?
Below are two sets of code. The first one I'm trying to check the behavior with the local variable and the second with the static. Thanks for more information concerning this.
Rick

CODE TEST I


CODE TEST 2
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Rick Reumann:

What I'm curious about is how Threads act when they are accessing static methods in another class.
[/quote
There is nothing special about a thread executing a non-synchronizd static method..it does so as you would expect it to. A thread executing a synchronized static method however prevents any other thread from executing that same method until the first thread exists the method.

1) My first question probably has to deal more with a local method than Threads. I'm wondering first off when a Thread would enter a static method and set a local variable to one of the method's arguments could this local variable be changed by another incoming thread?

No. each thread has its own exection stack, so local variables are private to the thread's stack.

2) What about the scenario with a static class variable:

In this scenario I would definitely think classVar could be modified by another Thread entering someMethod and changing the value of classVar after another Thread had already changed it.

You are correct. If you wanted to ensure that no other thread changed the value of classVar until the method completes, you should synchronize the method.

Rob


Rob
SCJP 1.4
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi rick,
this is what i understand.
static methods r class methods. so, one can access it just the way we use functions in C language. the only diff is that we MUST have class name as its OO.
so, exactly the same thing would happen when u have multiple C processes accessing the same calling the same function at the same time. every caller is having it's own STACK FRAME and so every local var is diff for each calling process/thread.
this answers ur first problem. local vars of static methods don't get confused and it works fine.
now, imagine, if many threads/process tries to access shared data. what is expected?? it must go thru critical section and all that stuff related with Synchronization... static members in a class are shared variables and so whenever they are expected to be accessed by multiple threads access it MUST be synchronized.
this should answer ur second question.
so as a conclusion,
if u only have all local vars in static methods, u r fine with multiple threads. u don need to do anything else.
if u r also accessing static vars of the class, u should have synch mechanism to take care of the problem that might arise.
i guess u know we can make static methods synchronized (which r called 'to obtain class lock').
now, interesting part comes when u want only a part of (a small block) LONG static method to be synch.
how do u do that?? u cant use synchronize(this) { ...} thing as static methods are not able to access "this" ref.
so, whats the solution?
i read once in IBM's Java forum that, one can use something like this,
class Test {
static void m() {
// some code
// CODE THAT REQUIRES SYnch
synchronize(Test) {
// code goes here...
}
// some more code
}
}
this seems strange little bit but it works! i tried it. it runs.
hih,
maulin.
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Rick to solve your problem with threads and class static members, you can synchronize at the class level!
To lock at the class level use the following code


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
wood burning stoves
 
subject: Threads entering a non-synchronized static method?