Re: multiple threads grabbing items from a vector

From: Jose Solorzano (jhsolorz_at_hotmail.com)
Date: 01/13/05


Date: 12 Jan 2005 16:11:16 -0800


johndesp@us.ibm.com wrote:
> Hi.
>
> I want to startup 3 threads which will access elements of a Vector
and
> perform an action based upon the value in the vector. I want to each
> thread to get priority to the Vector to grab the next element in
order
> that the particular thread completed enacting on the previous Vector
> element.
>
> Forinstance, Assuming each thread gets started in order..
>
> The first three elements of the vector would get executed in order
>
> Thread 0 ==> element 0
> Thread 1 ==> element 1
> Thread 2 ==> element 2
>
> However if Thread 1 finished before the others it would grab the next
> vector element..
>
> Thread 1 ==> element 3
> Thread 2 ==> element 4
> Thread 0 ==> element 5
>
> I can get the threads started but thread 0 always seems to have a
> "lock" on the Vector and the other threads can't get involved..
>
> I would appreciate it if someone could provide a solution and perhaps
> some psuedo code to make this happen. Like I said before I can get
all
> 3 threads to start , but thread 0 hogs up the Vector...
>
> thanks

I don't think a lock is the problem. Each method
in Vector is synchronized, but that doesn't mean
it locks the vector all the way thru while you
iterate it.

Thread 0 probably finishes the operation before
other threads even get a chance to start. If
you really need the threads to synchronize their
execution, you must use Object.wait() and
Object.notify/notifyAll().

Jose Solorzano



Relevant Pages

  • Re: multiple threads grabbing items from a vector
    ... Jose Solorzano wrote: ... > I don't think a lock is the problem. ... > you really need the threads to synchronize their ... > execution, ...
    (comp.lang.java.help)
  • Re: Global DataSet in asp.net Threat-Safety
    ... > The simplest way to synchronize all access to the dataset (or to any other ... > variable stored in the app state if you want to) and use it as a lock. ... > - every time you need to update the dataset, the writer locks on a ... >> has to be synchronized with the global dataset ... ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: [patch 00/15] Generic Mutex Subsystem
    ... > tries to grab it again, the other CPU will _not_ get it. ... I introduced a "lock stealing" ... The reason is that if you have a high priority process ... So when we have PREEMPT, your fairness is not being very fair. ...
    (Linux-Kernel)
  • Re: How to not synchronize on an object already locked ?
    ... > to continue doing usefull instead of waiting. ... > simply waiting on the lock, by checking some condition on the object. ... Then you need only synchronize on the ...
    (comp.lang.java.programmer)
  • Re: This works, but I dont know why :)
    ... there are overlays still on disk the exe needs ... My Clipper exes, all overlaid, are always open ... Have an active lock? ... operation the first step in execution. ...
    (comp.lang.clipper)