Sorting algorithms/Sleep sort: Difference between revisions

→‎{{header|NetRexx}}: Merge logic from two loops into one; make sure logic in run waits on latch
(→‎{{header|NetRexx}}: Merge logic from two loops into one; make sure logic in run waits on latch)
Line 422:
class RSortingSleepsort
properties constant private
dflt = '-6 3 1 4 5 2 3 -7 1 6 001 3 -9 2 5 -009 -8 4 6 1 9 8 7 6 5 -7 3 4 5 2 0 -2 -1 -5 -4 -3 -0 000 0'
properties indirect
startLatch = CountDownLatch
Line 437:
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sleepSort(iArg) public
loop mn = 1 to iArg.words()
setFloor(getFloor().min(iArg.word(mn))) -- save smallest -ve number so we can use it as a scale
end mn
setStartLatch(CountDownLatch(1)) -- used to put all threads on hold until we're ready to run
setDoneLatch(CountDownLatch(iArg.words())) -- used to indicate all work is done
loop mn = 1 to iArg.words()
setFloor(getFloor().min(iArg.word(mn))) -- save smallest -ve number so we can use it as a scale for sleep
Thread(SortThread(iArg.word(mn))).start() -- loop through input and create a thread for each element
end mn
Line 463 ⟶ 461:
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run() public
sleepTime = getNum() + parent.getFloor().abs() -- shifted by value of smallest number (permits numbers < 0)
sleepTime = sleepTime * 250 -- scale up; milliseconds are not granular enough
do
parent.getStartLatch().await() -- wait until all threads are constructed
sleepTime = getNum() + parent.getFloor().abs() -- shifted by value of smallest number (permits numbers < 0)
Thread.sleep(sleepTime) -- wait for this number's turn to run
sleepTime = sleepTime * 250 -- scale up; milliseconds are not granular enough
Thread.sleep(sleepTime) -- wait for this number's turn to run
catch ie = InterruptedException
ie.printStackTrace()
Anonymous user