New threads should be added to the end of the list

If there are multiple threads using sleep(0) for example, the new threads are
always pop'ed first and may cause a starvation problem for existing threads.
Now threads are handled in the order they were added.
Fixed maximum allowed timeout for select

When there are sleeping threads, select can't be blocking (timeout is nil) and
we must use the lowest possible timeout.