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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_c_symbol.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_threadgroup.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Thread</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#abort_on_exception">abort_on_exception</a> <a href="#abort_on_exception_eq">abort_on_exception=</a> <a href="#critical">critical</a> <a href="#critical_eq">critical=</a> <a href="#current">current</a> <a href="#exit">exit</a> <a href="#fork">fork</a> <a href="#kill">kill</a> <a href="#list">list</a> <a href="#main">main</a> <a href="#new">new</a> <a href="#pass">pass</a> <a href="#start">start</a> <a href="#stop">stop</a> <a href="#_ob_cb"><i>[ ]</i></a> <a href="#_ob_cb_eq"><i>[ ]=</i></a> <a href="#abort_on_exception"><i>abort_on_exception</i></a> <a href="#abort_on_exception_eq"><i>abort_on_exception=</i></a> <a href="#alive_qm"><i>alive?</i></a> <a href="#exit"><i>exit</i></a> <a href="#join"><i>join</i></a> <a href="#key_qm"><i>key?</i></a> <a href="#kill"><i>kill</i></a> <a href="#priority"><i>priority</i></a> <a href="#priority_eq"><i>priority=</i></a> <a href="#raise"><i>raise</i></a> <a href="#run"><i>run</i></a> <a href="#safe_level"><i>safe_level</i></a> <a href="#status"><i>status</i></a> <a href="#stop_qm"><i>stop?</i></a> <a href="#value"><i>value</i></a> <a href="#wakeup"><i>wakeup</i></a> <p></p><hr>
<P></P>
<code>Thread</code> encapsulates the behavior of a thread of execution,
including the main thread of the Ruby script. See the tutorial in
Chapter 11, beginning on page 113.
<P></P>
In the descriptions that follow, the
parameter <i>aSymbol</i> refers to a symbol, which is either a
quoted string or a <code>Symbol</code> (such as <code>:name</code>).<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abort_on_exception"><b>abort_on_exception</b></a></font></td><td bgcolor="#ffaaaa">
Thread.abort_on_exception
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
Returns the status of the global ``abort on exception''
condition. The default is <code>false</code>.
When set to <code>true</code>, will cause all threads to abort (the
process will <code>exit(0)</code>) if an
exception is raised in any thread. See also <a href="ref_c_thread.html#abort_on_exception_eq"><code>Thread::abort_on_exception=</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abort_on_exception_eq"><b>abort_on_exception=</b></a></font></td><td bgcolor="#ffaaaa">
Thread.abort_on_exception=
<i>aBoolean</i>-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
When set to <code>true</code>, all threads will abort if an
exception is raised. Returns the new state.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Thread.abort_on_exception = true
t1 = Thread.new do
puts "In second thread"
raise "Raise exception"
end
t1.join
print "not reached\n"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
In second thread
prog.rb:4: Raise exception (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="critical"><b>critical</b></a></font></td><td bgcolor="#ffaaaa">
Thread.critical
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the status of the global ``thread critical''
condition.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="critical_eq"><b>critical=</b></a></font></td><td bgcolor="#ffaaaa">
Thread.critical= <i>aBoolean</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Sets the status of the global ``thread critical''
condition and returns it.
When set to <code>true</code>, prohibits scheduling of any existing
thread. Does not block new threads from being created and run.
Certain thread operations (such as stopping or killing a thread,
sleeping in the current thread, and raising an exception) may
cause a thread to be scheduled even when in a critical section.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
count=0
Thread.new { while true; sleep(1); print "a "; count+=1; end }
while count < 3 do end # no-op wait
Thread.critical = true
puts "no more a's will come out."
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a a a no more a's will come out.
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="current"><b>current</b></a></font></td><td bgcolor="#ffaaaa">
Thread.current
-> <i>aThread</i>
</td></tr><td></td><td>
<P></P>
Returns the currently executing thread.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Thread.current</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Thread:0x40196528 run></code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="exit"><b>exit</b></a></font></td><td bgcolor="#ffaaaa">
Thread.exit
</td></tr><td></td><td>
<P></P>
Terminates the currently running thread and schedules another
thread to be run. If this thread is already marked to be
killed, <code>exit</code> returns the <code>Thread</code>. If this is the main thread, or
the last thread, exit the process.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="fork"><b>fork</b></a></font></td><td bgcolor="#ffaaaa">
Thread.fork { block }
-> <i>aThread</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_thread.html#new"><code>Thread::new</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="kill"><b>kill</b></a></font></td><td bgcolor="#ffaaaa">
Thread.kill( <i>aThread</i> )
</td></tr><td></td><td>
<P></P>
Causes the given thread to exit (see <a href="ref_c_thread.html#exit"><code>Thread::exit</code></a>).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>count = 0</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>a = Thread.new { while true do count += 1 end }</code></td>
</tr>
<tr>
<td valign="top"><code>sleep(1)</code></td>
<td valign="top"></td>
<td valign="top"><code>1</code></td>
</tr>
<tr>
<td valign="top"><code>Thread.kill(a)</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Thread:0x4018d52c dead></code></td>
</tr>
<tr>
<td valign="top"><code>count</code></td>
<td valign="top"></td>
<td valign="top"><code>911276</code></td>
</tr>
<tr>
<td valign="top"><code>a.alive?</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="list"><b>list</b></a></font></td><td bgcolor="#ffaaaa">
Thread.list -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of <code>Thread</code> objects for all threads that are
either runnable or stopped.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>Thread.new { sleep(200) }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>Thread.new { 1000000.times {|i| i*i } }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>Thread.new { Thread.stop }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>l = Thread.list</code></td>
</tr>
<tr>
<td valign="top"><code>l</code></td>
<td valign="top"></td>
<td valign="top"><code>[#<Thread:0x4018d658 sleep>, #<Thread:0x4018d6d0 run>, #<Thread:0x4018d70c sleep>, #<Thread:0x40196528 run>]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="main"><b>main</b></a></font></td><td bgcolor="#ffaaaa">
Thread.main
-> <i>aThread</i>
</td></tr><td></td><td>
<P></P>
Returns the main thread for the process.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Thread.main</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Thread:0x40196528 run></code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Thread.new( <i>[</i><i>arg</i><i>]<sup>*</sup></i> ) {| args | block }
<P></P>
-> <i>aThread</i>
</td></tr><td></td><td>
<P></P>
Creates a new thread to execute the instructions given in
<i>block</i>, and begins running it. Any arguments passed to
<a href="ref_c_thread.html#new"><code>Thread::new</code></a> are passed into the block.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
x = Thread.new { sleep .1; print "x"; print "y"; print "z" }
a = Thread.new { print "a"; print "b"; sleep .2; print "c" }
x.join # Let the threads finish before
a.join # main thread exits...
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
abxyzc
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="pass"><b>pass</b></a></font></td><td bgcolor="#ffaaaa">
Thread.pass
</td></tr><td></td><td>
<P></P>
Invokes the thread scheduler to pass execution to another
thread.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = Thread.new { print "a"; Thread.pass;
print "b"; Thread.pass;
print "c" }
b = Thread.new { print "x"; Thread.pass;
print "y"; Thread.pass;
print "z" }
a.join
b.join
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
axbycz
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
Thread.start( <i>[</i><i>args</i><i>]<sup>*</sup></i> ) {| args | block }
<P></P>
-> <i>aThread</i>
</td></tr><td></td><td>
<P></P>
Basically the same as <a href="ref_c_thread.html#new"><code>Thread::new</code></a>. However, if class
<code>Thread</code> is subclassed, then calling <code>start</code> in that
subclass will not invoke the subclass's <code>initialize</code> method.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="stop"><b>stop</b></a></font></td><td bgcolor="#ffaaaa">
Thread.stop
</td></tr><td></td><td>
<P></P>
Stops execution of the current thread, putting it into a
``sleep'' state, and schedules execution of another thread.
Resets the ``critical'' condition to <code>false</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = Thread.new { print "a"; Thread.stop; print "c" }
Thread.pass
print "b"
a.run
a.join
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
abc
</pre></td></tr></table>
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<P></P>
<i>thr</i>[ <i>aSymbol</i> ] -> <i>anObject</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Attribute Reference---Returns the value of a thread-local
variable, using either a symbol or a string name. If the
specified variable does not exist, returns <code>nil</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = Thread.new { Thread.current["name"] = "A"; Thread.stop }
b = Thread.new { Thread.current[:name] = "B"; Thread.stop }
c = Thread.new { Thread.current["name"] = "C"; Thread.stop }
Thread.list.each {|x| print x.inspect, x[:name], "\n" }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
#<Thread:0x4018d3ec sleep>C
#<Thread:0x4018d43c sleep>B
#<Thread:0x4018d4a0 sleep>A
#<Thread:0x40196528 run>nil
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb_eq"><b>[ ]=</b></a></font></td><td bgcolor="#ffaaaa">
<P></P>
<i>thr</i>[ <i>aSymbol</i> ] = <i>anObject</i>-> <i>anObject</i>
<P></P>
</td></tr><td></td><td>
Attribute Assignment---Sets or creates the value of a thread-local
variable, using either a symbol or a string. See also
<a href="ref_c_thread.html#_ob_cb"><code>Thread#[]</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abort_on_exception"><b>abort_on_exception</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.abort_on_exception
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
Returns the status of the ``abort on exception''
condition for <i>thr</i>. The default is <code>false</code>.
See also <a href="ref_c_thread.html#abort_on_exception_eq"><code>Thread::abort_on_exception=</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abort_on_exception_eq"><b>abort_on_exception=</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.abort_on_exception=
<code>true</code> or <code>false</code>-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
When set to <code>true</code>, causes all threads (including the main
program) to abort if an exception is raised in <i>thr</i>.
The process will effectively <code>exit(0)</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="alive_qm"><b>alive?</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.alive?
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>thr</i> is running or sleeping.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Thread.current.alive?</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="exit"><b>exit</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.exit -> <i>thr</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Terminates <i>thr</i> and schedules another
thread to be run. If this thread is already marked to be
killed, <code>exit</code> returns the <code>Thread</code>. If this is the main thread, or
the last thread, exits the process.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="join"><b>join</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.join
-> <i>thr</i>
</td></tr><td></td><td>
<P></P>
The calling thread will suspend execution and run <i>thr</i>. Does
not return until
<i>thr</i> exits. Any threads not joined will be killed when the
main program exits.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
x.join # Let x thread finish, a will be killed on exit.
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
axyz
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="key_qm"><b>key?</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.key?( <i>aSymbol</i> ) -> <code>true</code> or <code>false</code><br>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the given string (or symbol) exists as a
thread-local variable.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>me = Thread.current</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>me[:oliver] = "a"</code></td>
</tr>
<tr>
<td valign="top"><code>me.key?(:oliver)</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>me.key?(:stanley)</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="kill"><b>kill</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.kill
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_thread.html#exit"><code>Thread#exit</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="priority"><b>priority</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.priority
-> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Returns the priority of <i>thr</i>. Default is zero;
higher-priority threads will run before lower-priority threads.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Thread.current.priority</code></td>
<td valign="top"></td>
<td valign="top"><code>0</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="priority_eq"><b>priority=</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.priority= <i>anInteger</i>
-> <i>thr</i>
</td></tr><td></td><td>
<P></P>
Sets the priority of <i>thr</i> to <i>anInteger</i>.
Higher-priority threads will run before lower-priority threads.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1
</pre></td></tr></table>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>b = Thread.new do</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> loop { count2 += 1 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b.priority = -2</code></td>
</tr>
<tr>
<td valign="top"><code>sleep 1</code></td>
<td valign="top"></td>
<td valign="top"><code>1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>Thread.critical = 1</code></td>
</tr>
<tr>
<td valign="top"><code>count1</code></td>
<td valign="top"></td>
<td valign="top"><code>619920</code></td>
</tr>
<tr>
<td valign="top"><code>count2</code></td>
<td valign="top"></td>
<td valign="top"><code>11586</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="raise"><b>raise</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.raise( <i>anException</i> )
</td></tr><td></td><td>
<P></P>
Raises an exception (see <a href="ref_m_kernel.html#raise"><code>Kernel::raise</code></a>
on page 425 for details) from <i>thr</i>.
The caller does not have to be <i>thr</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Thread.abort_on_exception = true
a = Thread.new { sleep(200) }
a.raise("Gotcha")
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
prog.rb:3: Gotcha (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="run"><b>run</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.run -> <i>thr</i>
</td></tr><td></td><td>
<P></P>
Wakes up <i>thr</i>, making it eligible for scheduling. If not
in a critical section, then invokes the scheduler.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = Thread.new { puts "a"; Thread.stop; puts "c" }
Thread.pass
puts "Got here"
a.run
a.join
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a
Got here
c
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="safe_level"><b>safe_level</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.safe_level
-> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Returns the safe level in effect for <i>thr</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Thread.current.safe_level</code></td>
<td valign="top"></td>
<td valign="top"><code>0</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="status"><b>status</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.status
-> <i>aString</i>, <code>false</code> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Returns the status of <i>thr</i>: ``<code>sleep</code>'' if <i>thr</i> is sleeping
or waiting on I/O, ``<code>run</code>'' if <i>thr</i> is executing,
<code>false</code> if <i>thr</i> terminated normally, and <code>nil</code> if
<i>thr</i> terminated with an exception.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Thread.new { raise("die now") }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b = Thread.new { Thread.stop }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>c = Thread.new { Thread.exit }</code></td>
</tr>
<tr>
<td valign="top"><code>a.status</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>b.status</code></td>
<td valign="top"></td>
<td valign="top"><code>"sleep"</code></td>
</tr>
<tr>
<td valign="top"><code>c.status</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>Thread.current.status</code></td>
<td valign="top"></td>
<td valign="top"><code>"run"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="stop_qm"><b>stop?</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.stop?
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>thr</i> is dead or sleeping.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Thread.new { Thread.stop }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b = Thread.current</code></td>
</tr>
<tr>
<td valign="top"><code>a.stop?</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>b.stop?</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="value"><b>value</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.value
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Waits for <i>thr</i> to complete (via <a href="ref_c_thread.html#join"><code>Thread#join</code></a>) and
returns its value.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Thread.new { 2+2 }</code></td>
</tr>
<tr>
<td valign="top"><code>a.value</code></td>
<td valign="top"></td>
<td valign="top"><code>4</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="wakeup"><b>wakeup</b></a></font></td><td bgcolor="#ffaaaa">
<i>thr</i>.wakeup
-> <i>thr</i>
</td></tr><td></td><td>
<P></P>
Marks <i>thr</i> as eligible for scheduling (it may still remain
blocked on I/O, however). Does not invoke the scheduler (see
<a href="ref_c_thread.html#run"><code>Thread#run</code></a>).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
c = Thread.new { Thread.stop; puts "hey!" }
c.wakeup
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
hey!
</pre></td></tr></table>
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_c_symbol.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_threadgroup.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|