Question about yield.

Hello everyone,

Using yield can control how much time will we wait for a specific thread to accomplish its execution. I am meeting with a new scenario, which is a little different. My requirement is that I want my main thread starts several threads at the same time, which will perform their individual tasks in a concurrent way and I also want to control main thread waiting each thread for at most some time in a concurrent way. I am wondering how to "yield" several threads in a concurrent way (traditional yield function can only yield one thread)?

Thanks in advance,

George

[603 byte] By [GeorgeLiner] at [2007-9-30 22:06:02]
# 1
Hi,Yield? yield doesn't control how long you wait on something. Are you talking about sleep, join, or wait?/Kaj
kajbj at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks kajbj,> Hi,> > Yield? yield doesn't control how long you wait on> something. Are you talking about sleep, join, or wait?> > /KajSorry, I am talking about join of a thread.regards,George
GeorgeLiner at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 3
You can use join with a tiemout.
Peter-Lawrey at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks Peter-Lawrey,

> You can use join with a tiemout.

Your approach can apply in only one thread scenario. But my situation is that I want to join several threads at the same time and Java does not provide an approach to join a group of threads at the same time.

For example, I start 10 threads from main thread, and I want main thread wait each thread for at most 3 seconds. I do not want to join each thread sequentially which will cost main thread 30 seconds to accomplish its task. I want main thread to wait the 10 threads in a concurrent way, which will cost main thread only 3 seconds to accomplish. Have I made myself understood?

regards,

George

GeorgeLiner at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 5

The following will wait until the timeout is reached. It adjusts the timeout for each thread based on how long it already waited.

public static void join(Thread[] threads, long timeout) throws InterruptedException {

long start = System.currentTimeMillis();

for (int j = 0; j < threads.length; j++) {

long waitTime = start + timeout - System.currentTimeMillis();

if (waitTime <= 0) {

return;

}

threads[j].join(waitTime);

}

}

Peter-Lawrey at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks Peter-Lawrey,

> The following will wait until the timeout is reached.

> It adjusts the timeout for each thread based on how

> long it already waited.

> > public static void join(Thread[] threads, long

> timeout) throws InterruptedException {

> long start = System.currentTimeMillis();

> for (int j = 0; j < threads.length; j++) {

> long waitTime = start + timeout -

> System.currentTimeMillis();

> if (waitTime <= 0) {

> return;

> }

> threads[j].join(waitTime);

> }

> }

>

Your sample is very helpful. I want to add one more point to this sample, which is sending interrupt signal to those threads which have not accomplished their tasks by invoking interrupt on them. Through invoking interrupt on them, it is more easy and clean to release resources. What is your opinion on this point?

regards,

George

GeorgeLiner at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 7

> I want to add one more

> point to this sample, which is sending interrupt

> signal to those threads which have not accomplished

> their tasks by invoking interrupt on them. Through

> invoking interrupt on them, it is more easy and clean

> to release resources. What is your opinion on this

> point?

Nice idea. Go ahead and implement the code you describe.

sjasja at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...
# 8

Thanks for encourage, sjasja.

> > I want to add one more

> > point to this sample, which is sending interrupt

> > signal to those threads which have not accomplished

> > their tasks by invoking interrupt on them. Through

> > invoking interrupt on them, it is more easy and

> clean

> > to release resources. What is your opinion on this

> > point?

>

> Nice idea. Go ahead and implement the code you

> describe.

regards,

George

GeorgeLiner at 2007-7-7 11:19:08 > top of Java-index,Java Essentials,Java Programming...