On 8 Dec 1998, Juergen Kreileder wrote:

> >>>>> Mario Camou writes:
>     Mario> public class Foo {
>     Mario>   private Vector data;
> 
>     Mario>   public Vector getData() {
>     Mario>     if (data == null) {
>     Mario>       return null;
>     Mario>     }
>     Mario>     synchronized (data) {
>     Mario>       while (status != LOADED) {
>     Mario>         try {
>     Mario>           wait();
> 
> Yes, that is 'this.wait()' and you're not the owner of this' monitor.
> (You're only the owner of data's monitor). To get this' monitor you'll
> have to use synchronized (this) or a synchronized method.

In other words, what you probably want to do here is data.wait();
if you're synchronizing on data, otherwise, you would nede to synchronize
on 'this'

[ more code snippets cut ]

>     Mario>   public void loadData() {
>     Mario>     data = new Vector();
>     Mario>     synchronized (data) {
>     Mario>       try {
>     Mario>         while (something) {
>     Mario>           SomeObject obj;
>     Mario>           // blah blah blah (create object obj and load data into it)
>     Mario>           data.addElement (obj);
>     Mario>         } finally {
>     Mario>           notifyAll();
> 
> ditto

do a data.notifyAll(); if synchronizing on data, etc.

. . . Sean.

Reply via email to