Results 1 to 20 of 21
- 08-05-2010, 06:06 PM #1
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
Synchronization on non-final field
I am facing with Synchronization on non-final field .. warning message
what does it mean? what should I check?Java Code:protected Map returnListeners; public void addReturnListener(long invokeId, MyReturnListener listener) { synchronized (returnListeners) { //Synchronization on non-final field returnListeners.put(new Long(invokeId), listener); } }
- 08-05-2010, 07:46 PM #2
When you're trying to synchronize multiple threads, they have to be synchronized based on the same object reference/instance. This ensures that they both synchronize at the same time based on the same data.
Hope that makes a bit of sense; you just have to synchronize based on a finalized variable rather than a dynamic one.
- 08-05-2010, 07:54 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
You synchronize on objects, not on references thereof; suppose one thread synchronizes on an object (using a reference); another thread changes the reference to another object so no other thread can synchronize on the original object anymore (assuming that you don't have other references to the object available). Your code doesn't produce an error but it can go horribly wrong if you don't pay attention, hence the warning message.
kind regards,
Jos
- 08-05-2010, 07:58 PM #4
Actually it's more than a warning message; unless one uses a final variable, the code will not compile. (It's a compile-time error rather than a run-time error, if that's what you're getting at.)
- 08-05-2010, 08:04 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
Can you show me/us a code snippet where this situation actually causes a compile time error (instead of just a warning)? The following snippet doesn't even make the compiler whine:
kind regards,Java Code:public class T { // not a final field ... public Object lock= new Object(); public T() { synchronized (lock) { System.out.println(lock); } } public void m() { synchronized (lock) { System.out.println(lock); } } }
Jos
- 08-05-2010, 08:22 PM #6
Ah, weird. I think my mistake was in accidentally checking against an integer field rather than with an object. It produces the same error:
Thanks for clearing that up. ;)Java Code:public class X { public static int q = 5; public final static Object r = 6; public X() { synchronized (q) { // Error X.q++; } synchronized (r) { // No error X.q++; } } }
- 08-05-2010, 08:25 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 08-05-2010, 08:31 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
A bit more info for the above: you can't sync on a primitive, whether it's final or not so my first reply is still valid.
kind regards,
Jos
- 08-05-2010, 09:11 PM #9
Did the OP ever say if he/she got the code to work?
My experience with final requirements was for anonymous inner classes referencing a parm in a method. The compiler wanted the variable nailed down so it would be there when the code was called and the outer method(and its parms) had gone away.
- 08-06-2010, 06:06 AM #10
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
protected Map returnListeners;
public void addReturnListener(long invokeId, MyReturnListener listener) {
synchronized (returnListeners) // ==> THE WARNING GOES HERE!
{
returnListeners.put(new Long(invokeId), listener);
}
}
please help
thanks
- 08-06-2010, 06:39 AM #11
...did you even read any of the posts above?
Originally Posted by Zack
Originally Posted by JosAH
- 08-06-2010, 08:17 AM #12
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
Thanks Zack, you awaring me, but sorry, I still not not understand why this can be happened?
does it related because of the "import java.util.Hashtable;" is obsolete?
Java Code:import java.util.Hashtable; import java.util.HashSet; public class MyLink { protected Map returnListeners; protected Set eventListeners; public MyLink() { returnListeners = new Hashtable(); eventListeners = new HashSet(); } public void addReturnListener(long invokeId, MyReturnListener listener) { synchronized (returnListeners) { // --> THE WARNING GOES HERE! returnListeners.put(new Long(invokeId), listener); } } .. ...
- 08-06-2010, 08:41 AM #13
Are you just not familiar with final variables?
final : Java Glossary
...or do you just not understand why the variable has to be final?
- 08-06-2010, 09:07 AM #14
Senior Member
- Join Date
- Jan 2010
- Posts
- 138
- Rep Power
- 0
thanks Zack!.. its cleared now! :)
- 08-10-2010, 07:44 AM #15
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Guys, I have a question here on thread synchronization.
What is the use of making a static method synchronized; threads get synchronized on a particular object and every object has only one lock. Having said that, i do not have to synchronize a static method (class level). But I found from some sources that if a static method is marked as synchronized then the class level lock is taken.
I am not able to understand this class level lock concept; wherein threads synchronize on objects (object level lock).
Please clarify.
- 08-10-2010, 07:48 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 08-10-2010, 08:39 AM #17
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Thanks Josah.A class itself also is an object of type Class; read the API documentation for that class and the getClass() method and the '.class' notation.
kind regards,
Still I am having a doubt that if the method is a static one (class level); what difference would that make if i mark it synchronized.
In this below example if i remove synchronized; still i would get the same output.
Please help in clarifying my confusions.Java Code:public class SynchronizedMethod extends Object { private static int count = 1; public static [B]synchronized [/B]int getCount() { int i = count; count++; return i; } private static void print(String msg) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + ": " + msg); } public static void main(String[] args) { try { Runnable runnable = new Runnable() { public void run() { System.out.println("count=" + getCount()); } }; Thread threadA = new Thread(runnable, "ThreadA"); threadA.start(); Thread.sleep(500); Thread threadB = new Thread(runnable, "ThreadB"); threadB.start(); Thread.sleep(500); Thread threadC = new Thread(runnable, "ThreadC"); threadC.start(); Thread.sleep(500); Thread threadD = new Thread(runnable, "ThreadD"); threadD.start(); } catch (Exception x) { } } }
- 08-10-2010, 08:53 AM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 08-10-2010, 09:35 AM #19
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
Hi Josah,
I tried to replicate the same by the below code; but still looks to be the same
would you please paste here a proper example of using synchronized in a static method(wherein if i remove synchronized keyword from the class i would get different op)Java Code:package test; public class SynchronizedMethod extends Object { private static int count = 1; public static int getCount() { int i = count; count++; System.out.println(Thread.currentThread().getName()); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } public static void main(String[] args) { try { Runnable runnable = new Runnable() { public void run() { System.out.println("count=" + getCount()); } }; Thread threadA = new Thread(runnable, "ThreadA"); threadA.start(); Thread.sleep(500); Thread threadB = new Thread(runnable, "ThreadB"); threadB.start(); Thread.sleep(500); Thread threadC = new Thread(runnable, "ThreadC"); threadC.start(); Thread.sleep(500); Thread threadD = new Thread(runnable, "ThreadD"); threadD.start(); } catch (Exception x) { } } }
- 08-10-2010, 09:42 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
My comments still apply (see my previous reply): you fire up a thread each 0.5 seconds; it increments a variable and sleeps for 5 seconds itself. Still no deadly embrace or anything. Make your threads run a loop:
kind regards,Java Code:while(true) { int i= code+1; // guess the next value if (i != ++code) // did something else touch the resource System.out.println("resource changed behind my back") }
Jos
Similar Threads
-
how from an Access Currency field I populate a hidden field
By lse123 in forum Java ServletReplies: 4Last Post: 01-17-2010, 11:13 PM -
[SOLVED] is final class members are also final ?
By haoberoi in forum New To JavaReplies: 4Last Post: 11-10-2008, 03:01 PM -
static final field
By techie.it19 in forum New To JavaReplies: 3Last Post: 10-16-2008, 04:12 AM -
FInal field cannot be assigned
By ravian in forum New To JavaReplies: 3Last Post: 12-13-2007, 02:26 PM -
Final field question
By derrickD in forum Advanced JavaReplies: 1Last Post: 04-28-2007, 10:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks