-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_actors.shtml
39 lines (34 loc) · 2.98 KB
/
example_actors.shtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<div class="highlight"><pre><span class="k">class</span> <span class="no">Ring</span> <span class="p">{</span>
<span class="k">class</span> <span class="no">Node</span> <span class="p">{</span>
<span class="k">def</span> <span class="nf">initialize:</span> <span class="vi">@next</span> <span class="nf">ring:</span> <span class="vi">@ring</span>
<span class="k">def</span> <span class="nf">count:</span> <span class="n">count</span> <span class="p">{</span>
<span class="k">match</span> <span class="vi">@next</span> <span class="p">{</span>
<span class="k">case</span> <span class="no">nil</span> <span class="o">-></span>
<span class="s">"DONE: "</span> <span class="o">++</span> <span class="n">count</span> <span class="n">println</span>
<span class="vi">@ring</span> <span class="n">finish</span><span class="o">!</span>
<span class="k">case</span> <span class="n">_</span> <span class="o">-></span>
<span class="s">"."</span> <span class="n">print</span>
<span class="vi">@next</span> <span class="o">@@</span> <span class="nf">count:</span> <span class="p">(</span><span class="n">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">die</span><span class="o">!</span> <span class="c1"># let this actor die to free resources</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">def</span> <span class="nf">initialize:</span> <span class="n">amount</span> <span class="p">{</span>
<span class="n">node</span> <span class="o">=</span> <span class="no">nil</span>
<span class="n">amount</span> <span class="nf">times:</span> <span class="p">{</span>
<span class="n">node</span> <span class="o">=</span> <span class="no">Node</span> <span class="nf">new:</span> <span class="n">node</span> <span class="nf">ring:</span> <span class="no">self</span>
<span class="p">}</span>
<span class="vi">@start</span> <span class="o">=</span> <span class="n">node</span>
<span class="p">}</span>
<span class="k">def</span> <span class="nf">start:</span> <span class="vi">@parent</span> <span class="p">{</span>
<span class="vi">@start</span> <span class="nf">count:</span> <span class="mi">1</span>
<span class="p">}</span>
<span class="k">def</span> <span class="n">finish</span><span class="o">!</span> <span class="p">{</span>
<span class="vi">@parent</span> <span class="n">run</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1"># create ring and run through it</span>
<span class="n">ring</span> <span class="o">=</span> <span class="no">Ring</span> <span class="nf">new:</span> <span class="mi">4000</span>
<span class="n">ring</span> <span class="nf">start:</span> <span class="o">$</span> <span class="no">Thread</span> <span class="n">current</span>
<span class="no">Thread</span> <span class="n">stop</span> <span class="c1"># wait until finished</span>
</pre></div>