Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 21, 2004 synchronized (anArray) { } | ||||
---|---|---|---|---|
| ||||
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! |
April 21, 2004 Re: synchronized (anArray) { } | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian Hammond | 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 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! > > |
April 21, 2004 Re: synchronized (anArray) { } | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | 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 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! >> >> > > |
April 21, 2004 Re: synchronized (anArray) { } | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian Hammond | 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... > > > >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 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! > >> > >> > > > > > > |
Copyright © 1999-2021 by the D Language Foundation