Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: thread: catch the current thread in a share object Date: Sun, 27 Jul 2014 08:06:30 -0400 Organization: A noiseless patient Spider Lines: 41 Message-ID: References: <61f2c6cc-5832-466c-ac70-325df061fb17@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 27 Jul 2014 12:06:30 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="22589"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EDehe3dn/XDthVnMI8bGQ" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Cancel-Lock: sha1:f5bDZXXbLxrr6f6jJeq9wV/amFA= Xref: csiph.com comp.lang.java.programmer:29360 On 7/26/2014 2:19 PM, Eric Sosman wrote: > On 7/26/2014 12:20 PM, Bert_emme wrote: >> I try to implement a share object between thread. In the share object >> there is a queue. I want that if a condition is realizated the thread >> that at the moment lock the object go in wait and I want to put it in >> the queue of the shared object. But I don't idea on how referencing to >> this thread. >> Example >> >> class ObjShare{ >> Queue q; >> >> public synchronized mymethod(){ >> >> if(condition){ >> q.put(thecurrentthread) >> wait(); >> } > > See markspace's response. Also, this use of wait() is certainly > wrong, for at least two reasons: > > 1) It won't compile: wait() is an instance method of Object, > and you must apply it to some kind of Object reference. > Perhaps you meant this.wait(). "Never mind." > 2) wait() will re-aquire its Object's lock and return after > some other thread calls notify() or notifyAll() on that > Object, *or* at any random moment for no reason at all. > The fact that wait() returns says *nothing* about whether > the reasons for waiting no longer apply. That's why a > wait() call should (nearly always) be inside a synchronized > loop that re-tests the condition after awakening. This part's right, though. -- esosman@comcast-dot-net.invalid