From: Marcin Rodzik on
I've looked at this: http://bit.ly/9GqZoT but still have some
doubts... I'm implementing a thread pool containing multiple threads,
which are created when the application starts, and then they hang
around waiting to be given a job to do. It involves a loop as follows
or similar:

while (true) {
while (job == null)
// wait
// and then do the job:
job.run();
job = null; // the job is done
}

Another method exist to submit the job to this pool thread. It means a
method which perform the assignment:
job = new SomeJobToDo();

The problem is how to perform the "wait" operation. For just poor
esthetic reasons I prefer using the Thread.yield() or eg.
Thread.sleep(500) method. But examples I found in the Internet tend to
use another object (let's say lock) and its Object.wait() method. This
causes that the Object.notify() method must be invoked after ordering
the job (the assignment).

My questions:
1) Which one of these two basic methods is better and, which is more
important for my, WHY?
2) What is better: using sleep or yield in the first method?
From: Owen Jacobson on
On 2010-05-29 12:45:53 -0400, Marcin Rodzik said:

> I've looked at this: http://bit.ly/9GqZoT but still have some
> doubts... I'm implementing a thread pool containing multiple threads,
> which are created when the application starts, and then they hang
> around waiting to be given a job to do. It involves a loop as follows
> or similar:
>
> while (true) {
> while (job == null)
> // wait
> // and then do the job:
> job.run();
> job = null; // the job is done
> }
>
> Another method exist to submit the job to this pool thread. It means a
> method which perform the assignment:
> job = new SomeJobToDo();
>
> The problem is how to perform the "wait" operation. For just poor
> esthetic reasons I prefer using the Thread.yield() or eg.
> Thread.sleep(500) method. But examples I found in the Internet tend to
> use another object (let's say lock) and its Object.wait() method. This
> causes that the Object.notify() method must be invoked after ordering
> the job (the assignment).
>
> My questions:
> 1) Which one of these two basic methods is better and, which is more
> important for my, WHY?
> 2) What is better: using sleep or yield in the first method?

Don't write your own thread pool. You're reinventing
java.util.concurrent.ExecutorService -- you probably want something
along the lines of

ExecutorService threadPool = Executors.newCachedThreadPool();
// ...

/* SomeJobToDo implements Runnable */
threadPool.submit(new SomeJobToDo());
/* or */

/* SomeJobToDo implements Callable<MyResultType> */
Future<MyResultType> resultFuture =
threadPool.submit(new SomeJobToDo());
// ...
MyResultType result = resultFuture.get();

Hope that helps,
-o

From: markspace on
Owen is 100% correct. You should not try to write your own thread pool.
There are already working versions which will be much easier for you
to use.

However, I'll assume you are a student and trying to learn. Therefore:


Marcin Rodzik wrote:
> My questions:
> 1) Which one of these two basic methods is better and, which is more
> important for my, WHY?
> 2) What is better: using sleep or yield in the first method?


wait() is 100% superior. Both yield() and sleep() means the thread
continues to run, consuming resources. If a thread does nothing, but
still runs, you'll slow the CPU down and other tasks trying to do real
work will be very slow, since they can't get the CPU they need. If you
make a thread POOL of threads that still consume CPU time, then you have
lots of CPU time dedicated to doing nothing. This is even worse.

Also, sleep() is not as responsive as wait() (In more technical terms,
the latency is higher.) sleep() will always wait at least as long as
the value you give it. So if a thread pool has a sleeping thread, and
you give it a task to run, the thread pool will still sleep, and only
wake up later and then run the job. This makes sleep a slow solution.

wait() on the other had does not delay. Even though it's not
guaranteed, the operating system "knows" that a waiting thread wants to
be woken up as soon as possible, and so makes an effort to get it going
as fast as it can. Thus wait is a much faster solution than either
sleep() or yeild(). And wait() consumes no CPU when it is waiting, so
your OTHER threads run faster.

Overall, wait() is really the only solution. sleep() and yield() aren't
even second class solutions, their more like third or fourth class
solutions. Actually, yield() is probably more like a 100th class
solution. Don't use that to implement a waiting worker thread, its far
far worse than sleep() because it really runs a lot more than sleep()
and consumes a LOT more CPU.

If you do need timing like sleep(), wait() takes a time out:

wait( 100 ); // wait 100 milliseconds

This might help you out in some situations.
From: Eric Sosman on
On 5/29/2010 12:45 PM, Marcin Rodzik wrote:
> I've looked at this: http://bit.ly/9GqZoT but still have some
> doubts... I'm implementing a thread pool containing multiple threads,

In other words, as Owen Jacobson points out, you're reinventing
the wheel. Why? Are you hoping to make it squarer?

> [...]
> The problem is how to perform the "wait" operation. For just poor
> esthetic reasons I prefer using the Thread.yield() or eg.
> Thread.sleep(500) method. But examples I found in the Internet tend to
> use another object (let's say lock) and its Object.wait() method. This
> causes that the Object.notify() method must be invoked after ordering
> the job (the assignment).
>
> My questions:
> 1) Which one of these two basic methods is better and, which is more
> important for my, WHY?

Object.wait() with Object.notify() or Object.notifyAll() is better.
The waiting thread, which can't do anything useful until other threads
have made progress, allows those other threads to use "all" the CPU
cycles and doesn't slow them down. Also, the waiting thread wakes up
"immediately" when there's something for it to do; it doesn't sit idle
while a silly timeout runs its course.

> 2) What is better: using sleep or yield in the first method?

Thread.sleep() is better than Thread.yield(), because it gives
other threads a chance to use CPU cycles.

Thread.yield() is better than Thread.sleep() because it "notices"
the change of state sooner.

But you're asking whether Holofernes is better than Haman. Both
techniques are *vastly* inferior to wait(). And (again, read Owen
Jacobson's response) all this work has already been done for you, most
likely by someone with greater expertise than you possess at the moment.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Marcin Rodzik on
On May 29, 9:42 pm, Eric Sosman <esos...(a)ieee-dot-org.invalid> wrote:
>      Thread.sleep()
>      Thread.yield()
> Both techniques are *vastly* inferior to wait().

So whay does yield() exist? Is there any case in which it can be
preferred?
Anyway, thanks, I really appreciate your answers.

MR
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Enum Idiom Question
Next: Suggession for your website