September 2017
Beginner
402 pages
9h 52m
English
To create a thread object, use the constructor of the Thread class. It takes the name of the thread and the code block in the corresponding named parameters name and code:
my $t = Thread.new( name => 'My thread', code => sub { say 'Hi there!'; });
The thread is now created but is not activated. To run it, the run method must be called:
$t.run();
Execute the following example and examine the order, in which the lines appear on the screen:
my $t = Thread.new( name => 'My thread', code => sub { say 'Start'; sleep 2; say 'End'; });say 'Before';$t.run();say 'After';
As soon as the thread is running, it prints its two messages, Start and End, separated by a delay of 2 seconds:
BeforeAfterStartEnd
It is possible ...
Read now
Unlock full access