Metered concurrency: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, marked p2js incompatible) |
(J) |
||
Line 936: | Line 936: | ||
-> |
-> |
||
</pre> |
</pre> |
||
=={{header|J}}== |
|||
This implementation, while sufficient for this task's requirements, is for demonstration purposes only, and is not meant for serious work. |
|||
<lang J>scheduledumb=: {{ |
|||
id=:'dumb',":x:6!:9'' |
|||
wd 'pc ',id |
|||
(t)=: u {{u 0{::n[y[erase 1{::n}} (y;t=. id,'_timer') |
|||
wd 'ptimer ',":?100 |
|||
}} |
|||
sleep=: 6!:3 NB. seconds |
|||
timestamp=: 6!:1 NB. seconds |
|||
acquire=: {{ |
|||
imprison y |
|||
while. 1<count y do. |
|||
release y |
|||
sleep 0.1 |
|||
imprison y |
|||
end. |
|||
}} |
|||
release=: {{ counter=: (<:y{counter) y} counter }} |
|||
imprison=: {{ counter=: (>:y{counter) y} counter }} |
|||
count=: {{ y { counter }} |
|||
counter=: 0 0 |
|||
demo=: {{ |
|||
acquire 0 |
|||
echo 'unit ',y,&":' acquired semaphore, t=',":timestamp'' |
|||
sleep 2 |
|||
release 0 |
|||
}}</lang> |
|||
Task example: |
|||
<lang J> demo scheduledumb"0 i.5 |
|||
unit 1 acquired semaphore, t=54683.6 |
|||
unit 0 acquired semaphore, t=54685.6 |
|||
unit 4 acquired semaphore, t=54687.7 |
|||
unit 2 acquired semaphore, t=54689.7 |
|||
unit 3 acquired semaphore, t=54691.7</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |