classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [cp-patches] Implementing Thread.sleep() via Thread.wait()


From: Jeroen Frijters
Subject: RE: [cp-patches] Implementing Thread.sleep() via Thread.wait()
Date: Mon, 3 Jan 2005 08:11:23 +0100

Mark Wielaard wrote:
> But in this case it looks like it is just a stupid bug in at least
> Sun's implementation.

That's pretty arrogant, if you ask me. Here is a spec that is entirely
reasonable and consistent with the observed Sun behavior:

(Assume that a thread can be in the states as defined in JDK 1.5
Thread.State, with the addition that RUNNABLE is split into RUNNING and
READY_TO_RUN. RUNNING represents threads that are actually running, and
READY_TO_RUN are threads that are available for the scheduler to select
as the next RUNNING thread.)

Thread.yield() - Switch the thread state to READY_TO_RUN and allow the
scheduler to select another thread in the READY_TO_RUN state of equal or
higher priority, if no such thread exists, switch this thread back to
RUNNING.

Thread.sleep(T) - Switch the thread state to TIMED_WAITING for exactly T
ms and after that back to READY_TO_RUN.

Thread.interrupt() - Set the "interrupt pending" flag for the thread. If
the thread is in either TIMED_WAITING or WAITING, interrupt the wait and
set the thread state to READY_TO_RUN. Before a thread enters the WAITING
or TIMED_WAITING state it checks the "interrupt pending" flag and if it
is set the thread will not enter the (TIMED_)WAITING state but instead
continue to run and dispatch the InterruptedException.

> Your argument is that some programs might depend on sleep(0) 
> to act like yield() to escape from tight loops? Are there
> actually such programs.
> And wouldn't you just consider them buggy? I would.

And this is why most closed source JVM implementers don't take us
seriously. "Considering" a program buggy isn't really helping anybody.

BTW, there before Archie files the bug, there was already a bug filed
(based on a request from a licensee, so Sun might actually care about
that one) to clarify why Thread.sleep(0) doesn't throw an
InterruptedException. See
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6194486

Regards,
Jeroen




reply via email to

[Prev in Thread] Current Thread [Next in Thread]