Sorting algorithms/Sleep sort: Difference between revisions
Content deleted Content added
→{{header|NetRexx}}: Merge logic from two loops into one; make sure logic in run waits on latch |
|||
Line 422: | Line 422: | ||
class RSortingSleepsort |
class RSortingSleepsort |
||
properties constant private |
properties constant private |
||
dflt |
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 |
properties indirect |
||
startLatch = CountDownLatch |
startLatch = CountDownLatch |
||
Line 437: | Line 437: | ||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
method sleepSort(iArg) public |
method sleepSort(iArg) public |
||
loop mn = 1 to iArg.words() |
|||
⚫ | |||
end mn |
|||
setStartLatch(CountDownLatch(1)) -- used to put all threads on hold until we're ready to run |
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 |
setDoneLatch(CountDownLatch(iArg.words())) -- used to indicate all work is done |
||
loop mn = 1 to iArg.words() |
loop mn = 1 to iArg.words() |
||
⚫ | |||
Thread(SortThread(iArg.word(mn))).start() -- loop through input and create a thread for each element |
Thread(SortThread(iArg.word(mn))).start() -- loop through input and create a thread for each element |
||
end mn |
end mn |
||
Line 463: | Line 461: | ||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
method run() public |
method run() public |
||
⚫ | |||
⚫ | |||
do |
do |
||
parent.getStartLatch().await() -- wait until all threads are constructed |
parent.getStartLatch().await() -- wait until all threads are constructed |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
catch ie = InterruptedException |
catch ie = InterruptedException |
||
ie.printStackTrace() |
ie.printStackTrace() |