[...]
> Each time I create a thread it gets added to a vector v. In the run method
> of this thread I have a flag. If this flag for some reason is set to true
[quoted text clipped - 4 lines]
> if (flag_wait){
> synchronized(this){
You now synchronize on this Runnable. Without knowing your code it is
impossible to tell if that is the same object you end up putting in your
Vector, but let's assume it is (for now).
> try{
> flag_wait = false;
> wait();
Now the executing thread is added to the wait set associated with this
Runnable instance, and the lock is relinquished.
> }
> catch(InterruptedException e){}
> }
> }
> }
You have already been told in no uncertain terms to _only_ _ever_
call wait() in a while loop. Please heed this advice. If you don't
believe it, don't start to argue but read the many many past threads
discussing this point first!
> If I create two threads and sets their flag_wait to true, I will now in my
> vector v have two threads that are waiting at index 0 and index 1.
[quoted text clipped - 6 lines]
>
> if (flag_notify){
For all I know, flag_notify could be false all the time so this block
might never run. How should anyone know?
> synchronized(this){
Now you synchronize on _this_ Runnable. That's certainly another
object than the one the wait() in the other thread is synchronizing on.
> try{
>
> flag_notify = false;
> v.get(1).notifyAll(); // now only thread at index 1 should
> be notified!
So at this point you are synchronized on the lock associated with this
Runnable instance, but very probably (impossible to say for sure without
seeing the rest of the code, but very probably) not on the one the
wait() above is using as a monitor. Therefore you will likely see an
IllegalMonitorStateException here.
> }
> catch(InterruptedException e){}
[quoted text clipped - 3 lines]
>
> But for some reason I cannot run notifyAll() on a thread in a vector!
Did you get a CannotRunNotifyAllOnThreadInVectorException, or what
makes you say that?

Signature
Cheers, Tilman
`Boy, life takes a long time to live...' -- Steven Wright
> Each time I create a thread it gets added to a vector v. In the run method
> of this thread I have a flag. If this flag for some reason is set to true
> then that current thread will be set to wait:
On what? You need a specific monitor to wait on. That monitor needs to
be shared with other threads, which intend to call notify() on it. I'm
not generally as picky about this as some other members of the group...
but you've shown yourself not very adept at supplying the information we
need in code snippets and error messages to answer your questions on the
newsgroup. If you want help, the best thing for you to do is post
complete example code in the future, so that we can compile and run in
ourselves.
One more comment...
> if (flag_wait){
> synchronized(this){
[quoted text clipped - 4 lines]
> catch(InterruptedException e){}
> }
This lacks a predicate loop, and so it's hopelessly broken. You need to
change the "if" statement to a "while" statement, and the notifying
thread needs to clear flag_wait, *not* the waiting thread. The entire
predicate loop should be inside the synchronized block.
I'd also recommend that you forget all about optimizing until you've got
something working. Use *one* shared object as a monitor, and not a
Vector or anything else. Once that's working, you can try something
else.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation