Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29338 > unrolled thread

thread: catch the current thread in a share object

Started byBert_emme <bertanimauro@gmail.com>
First post2014-07-26 09:20 -0700
Last post2014-07-26 14:04 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  thread: catch the current thread in a share object Bert_emme <bertanimauro@gmail.com> - 2014-07-26 09:20 -0700
    Re: thread: catch the current thread in a share object markspace <markspace@nospam.nospam> - 2014-07-26 10:08 -0700
    Re: thread: catch the current thread in a share object Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-26 14:19 -0400
      Re: thread: catch the current thread in a share object Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-27 08:06 -0400
    Re: thread: catch the current thread in a share object Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-07-26 14:04 -0700

#29338 — thread: catch the current thread in a share object

FromBert_emme <bertanimauro@gmail.com>
Date2014-07-26 09:20 -0700
Subjectthread: catch the current thread in a share object
Message-ID<61f2c6cc-5832-466c-ac70-325df061fb17@googlegroups.com>
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();
        }
    }

}

Thanks in advance
Mauro

[toc] | [next] | [standalone]


#29343

Frommarkspace <markspace@nospam.nospam>
Date2014-07-26 10:08 -0700
Message-ID<lr0n9t$14u$1@dont-email.me>
In reply to#29338
On 7/26/2014 9:20 AM, 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.

I don't know what you are trying to accomplish here.  And it doesn't not 
sound like good design.

But it also sounds like you don't know about Thread.currentThread().


 > class ObjShare{
 >      private Queue<Thread> q;
 >
 >      public synchronized mymethod(){
 >
 >          if(condition){
 >             q.put( Thread.currentThread() )
 >             wait();
 >          }
 >      }
 >
 > }

[toc] | [prev] | [next] | [standalone]


#29346

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-07-26 14:19 -0400
Message-ID<lr0rg8$sjj$1@dont-email.me>
In reply to#29338
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().

     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.

     Again: See markspace's response, with particular attention to
the phrase "not good design."  You are almost certainly doing
something you'll eventually regret.

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29360

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-07-27 08:06 -0400
Message-ID<lr2q06$m1t$1@dont-email.me>
In reply to#29346
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().

     <dope_slap> "Never mind." </dope_slap>

>      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

[toc] | [prev] | [next] | [standalone]


#29352

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2014-07-26 14:04 -0700
Message-ID<lr1540$sc9$1@dont-email.me>
In reply to#29338
On 7/26/2014 09:20, 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();
>          }
>      }
>
> }
>
> Thanks in advance
> Mauro
>

Mauro:

Take a look at BlockingQueue in the docs.  I think that is what you are 
looking for.  One thread puts elements onto the queue and the other 
thread takes them off for processing.

knute...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web