D - synchronized (anArray) { }
- Brian Hammond (10/10) Apr 21 2004 I am trying to synchronize access to an array using the synchronized key...
- Kris (32/42) Apr 21 2004 You can synchronize on any object, so you might do this:
- Brian Hammond (2/51) Apr 21 2004
- Kris (39/98) Apr 21 2004 You might note that this approach can be useful for synchronizing severa...
I am trying to synchronize access to an array using the synchronized keyword. The idea here is to disallow other threads from adding a new element to the array while execution of another thread is in the synchronized block. This is disallowed as synchronized requires an Object reference not an array. Any thoughts? class A { int[] n_; void foo() { synchronized (n_) {} } } will not compile... "can only synchronize on class objects, not 'int[]'" An obvious workaround is to use synchronized (this) {} but I was hoping for something more fine-grained (similar for making the method synchronized). Thanks!
Apr 21 2004
You can synchronize on any object, so you might do this: class A { private static Object o; private int[] array; // static constructor (nice thing in D) static this() { // setup a fine-grained synchronizable o = new Object(); } void foo() { synchronized (o) { // do stuff } } void bar() { synchronized (o) { // do other stuff } } } "Brian Hammond" <d brianhammond.com> wrote in message news:c66f6c$1qfd$1 digitaldaemon.com...I am trying to synchronize access to an array using the synchronizedkeyword.The idea here is to disallow other threads from adding a new element tothearray while execution of another thread is in the synchronized block. This is disallowed as synchronized requires an Object reference not anarray.Any thoughts? class A { int[] n_; void foo() { synchronized (n_) {} } } will not compile... "can only synchronize on class objects, not 'int[]'" An obvious workaround is to use synchronized (this) {} but I was hopingforsomething more fine-grained (similar for making the method synchronized). Thanks!
Apr 21 2004
Ah... That makes sense. Thank you. In article <c66fjl$1r8k$1 digitaldaemon.com>, Kris says...You can synchronize on any object, so you might do this: class A { private static Object o; private int[] array; // static constructor (nice thing in D) static this() { // setup a fine-grained synchronizable o = new Object(); } void foo() { synchronized (o) { // do stuff } } void bar() { synchronized (o) { // do other stuff } } } "Brian Hammond" <d brianhammond.com> wrote in message news:c66f6c$1qfd$1 digitaldaemon.com...I am trying to synchronize access to an array using the synchronizedkeyword.The idea here is to disallow other threads from adding a new element tothearray while execution of another thread is in the synchronized block. This is disallowed as synchronized requires an Object reference not anarray.Any thoughts? class A { int[] n_; void foo() { synchronized (n_) {} } } will not compile... "can only synchronize on class objects, not 'int[]'" An obvious workaround is to use synchronized (this) {} but I was hopingforsomething more fine-grained (similar for making the method synchronized). Thanks!
Apr 21 2004
You might note that this approach can be useful for synchronizing several different, but related, classes like so (or some variation on the theme); class A { private Object sync; this (Object sync) { this.sync = sync; } void foo () { synchronized (synch) blah blah ... } } class B { private Object sync; this (Object sync) { this.sync = sync; } void bar () { synchronized (synch) blah blah ... } } void test() { Object o = new Object(); // both classes synchronized on a common object A a = new A(o); B b = new B(o); } "Brian Hammond" <d brianhammond.com> wrote in message news:c66gt5$1tgu$1 digitaldaemon.com...Ah... That makes sense. Thank you. In article <c66fjl$1r8k$1 digitaldaemon.com>, Kris says...'int[]'"You can synchronize on any object, so you might do this: class A { private static Object o; private int[] array; // static constructor (nice thing in D) static this() { // setup a fine-grained synchronizable o = new Object(); } void foo() { synchronized (o) { // do stuff } } void bar() { synchronized (o) { // do other stuff } } } "Brian Hammond" <d brianhammond.com> wrote in message news:c66f6c$1qfd$1 digitaldaemon.com...I am trying to synchronize access to an array using the synchronizedkeyword.The idea here is to disallow other threads from adding a new element tothearray while execution of another thread is in the synchronized block. This is disallowed as synchronized requires an Object reference not anarray.Any thoughts? class A { int[] n_; void foo() { synchronized (n_) {} } } will not compile... "can only synchronize on class objects, notsynchronized).An obvious workaround is to use synchronized (this) {} but I was hopingforsomething more fine-grained (similar for making the methodThanks!
Apr 21 2004