fec1ae298b1886fe815739746fa5fc7c6b0e82c4
[nit.git] / lib / pthreads / examples / threadpool_example.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Simple example using threadpool
16 module threadpool_example
17
18 import threadpool
19
20 # Task printing "hello world" on standard output
21 class HWTask
22 super JoinTask
23
24 # Sleeping time
25 var sec: Int
26
27 # id
28 var id: Int
29 redef fun main do
30 print "Hello from {id}"
31 nanosleep(sec, 0)
32 print "World from {id}"
33 end
34 end
35
36 var tp = new ThreadPool
37 for i in 100.times do
38 var t = new HWTask(2.rand, i)
39 tp.execute(t)
40 end
41
42 nanosleep(20,10)