Metered concurrency: Difference between revisions
m
→{{header|J}}
(Metered concurrency in FreeBASIC) |
m (→{{header|J}}) |
||
Line 988:
=={{header|J}}==
Here's an approach which uses the new (j904, currently in beta) threading primitives:
This implementation, while sufficient for this task's requirements, is for demonstration purposes only, and is not meant for serious work.▼
<lang J>metcon=: {{
sleep=: 6!:3
task=: {{
11 T. lock NB. wait
sleep 2
echo 'Task ',y,&":' has the semaphore'
13 T. lock NB. release
}}
lock=: 10 T. 0
0&T.@'' each i.0>.4-1 T.'' NB. ensure at least four threads
> task t.''"0 i.10 NB. dispatch and wait for 10 tasks
14 T. lock NB. discard lock
}}</lang>
An example run might look like this:
<lang j> metcon''
Task 0 has the semaphore
Task 1 has the semaphore
Task 2 has the semaphore
Task 3 has the semaphore
Task 4 has the semaphore
Task 9 has the semaphore
Task 5 has the semaphore
Task 7 has the semaphore
Task 8 has the semaphore
Task 6 has the semaphore</lang>
▲
<lang J>scheduledumb=: {{
|