| Author |
Synchronizing a instance variable - please help
|
Sanju Shah
Greenhorn
Joined: Apr 06, 2006
Posts: 19
|
|
Hello All, Following is the scenario. The baseclass A spaws multiple threads and has a instance vairable VAR1 . This instance variable is modified by the subclass Class B. So in effect each thread spawed by the base class can have its own value of instance variable . So my questions is can I synchronize the isntance variable in the sub class Class B to make sure the values of VAR1 of different threads don't get mixed up ? If yes, how can I do it? BTw, I tested out sunchronizing the method in classB which modifies the VAR1 but that didn't work as the values of VAR1 were getting mixed up with differnt thread. Please help me as I'm new to java Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Synchronization is based on objects -- not references. So... if one thread synchronize with a reference from the baseclass, while another uses a reference from the subclass, while a third uses a completely different reference -- they will synchronize with each other, as long as they all refer to the *same* object. On the other hand... if one thread uses a reference from a class, and another uses the *exact* same reference, it will *not* work if the reference changes to another object between access of the two threads. Henry [ July 12, 2006: Message edited by: Henry Wong ]
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Sanju Shah
Greenhorn
Joined: Apr 06, 2006
Posts: 19
|
|
thanks henry for the reply Then how do I make sure that each thread retains it own value of VAR1?? Eg thread -1 has VAR1 = A, thread -2 has VAR2 = B thanks
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
We run into this kind of trouble when two threads modify a shared resource. Code as simple as this can go badly: because another thread can change the thing variable between those two statements. If you want to own and control that variable for the span of several statements, you need to put those statements in a block that synchronizes on the same or another shared resource. Now anybody who synchronizes on the someArgument object will play nicely. A thread must get the Monitor to enter the block. If some other thread has the Monitor we have to wait until that other thread gives it up. Imagine you're at a meeting where you can speak only if you hold the Team Flag in your hand. You can't get the flag until somebody else is done speaking and puts it down on the table. I'm smiling as I imagine the folks on my team scrambling across the table for the flag.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Originally posted by Sanju Shah: thanks henry for the reply Then how do I make sure that each thread retains it own value of VAR1?? Eg thread -1 has VAR1 = A, thread -2 has VAR2 = B thanks
Not sure what you are asking... But why do you need to synchronize if each thread is working with a different object? A piece of code example, along with a description of what you want to happen, would be nice here. Henry
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Reading your posts again, maybe you don't want to share at all. I think my first choice would be to keep the variable in the Runnable or Thread. That way it can always have its own stuff. Another choice might be a map on the shared object: I used the Runnable as key to the map; you could also use a unique id of any kind. You'd probably want a synchronized map or synchronized methods that get, put and remove items. Yet another choice would be ThreadLocal. That's a kind of holder that manages one instance per thread. I haven't used them; don't know how they work if the thread ends or if there is way to iterate all the instances being held. Is that closer to your problem?
|
 |
Sanju Shah
Greenhorn
Joined: Apr 06, 2006
Posts: 19
|
|
THis is what I'm trying to do Abstract class A { protected String var = "base"; abstract protected void modify(string input); StartRUnning(){ new thread(){ public void run(){ modify(String input); } } } } 2) I have a subclass Class B which extends Class A class B extends Class A { synchronized modify(String input){ var = "default" if (input.equlasIgnorecase("A") var = "subclass"; } } When the applciation runs.. I know that based on the input when I print the value of the VAR in the baseclass classA Ideally I want the following ThreadA should have a var="subclass" ThreadB should have a var="default" But what is happening is ThreadA should have a var="default" ThreadB should have a var="default" Hope this makes it clear
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Sorry, but I can't tell from your code what is thread A, and what is thread B. Could you elaborate? In any case, something tells me that you don't need synchronization at all. Henry
|
 |
Sanju Shah
Greenhorn
Joined: Apr 06, 2006
Posts: 19
|
|
Henry, I think U might be right . I think I'm looking at this from a wrong angle. I'm not sure now whether its anything to do with threads Each user who comes in with a Idenfier string will be given a new thread from the threadpool which is for example thread A and threadB etc . If the Identifer string matches a particular pattern , then I change the instance variable in the sub class from defualt to an another value and each time I reset the value of the instance variable to defualt before doing comparsion of Identifier string . Hope this is clear now. So anyway since multiple threads are modifiing the instance variable which is defined in the base class. Do U think it will make a difference if I modify the instance variable with getter and setter method instead of modifying it directly? If getter and setter methods are needed , then should I have those in basseclass or sub class? thanks
|
 |
 |
|
|
subject: Synchronizing a instance variable - please help
|
|
|