File tree Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Expand file tree Collapse file tree 1 file changed +29
-9
lines changed Original file line number Diff line number Diff line change @@ -2274,13 +2274,23 @@ puts "==== I am the main thread."
2274
2274
# thread.join # Try to uncomment these two lines to see the differences.
2275
2275
# puts "==== after thread.join"
2276
2276
```
2277
- You will find that if there is no ` thread.join ` , you can only see ` ==== I am the main thread. ` in console.
2277
+ You will find that if there is no ` thread.join ` , you can see
2278
+ ``` log
2279
+ ==== I am the main thread.
2280
+ ==== after thread.join
2281
+ ~~~~ 0
2282
+ ~~~~ 1
2283
+ ~~~~ 2
2284
+ ```
2285
+ in console.
2278
2286
2279
2287
After you added ` thread.join ` , you can see:
2280
- ``` ruby
2288
+ ``` log
2289
+ ==== I am the main thread.
2290
+ ~~~~ 0
2281
2291
~~~~ 1
2282
2292
~~~~ 2
2283
- ~~~~ 3
2293
+ ==== after thread.join
2284
2294
````
2285
2295
in console.
2286
2296
@@ -2289,26 +2299,36 @@ Try to run `test_thread_join2.rb`.
2289
2299
```ruby
2290
2300
# ./test_thread_join2.rb
2291
2301
arr = [
2292
- Thread.new { sleep 1 },
2293
2302
Thread.new do
2294
- sleep 5
2303
+ puts 'I am arr[0]'
2304
+ sleep 1
2305
+ puts 'After arr[0]'
2306
+ end,
2307
+ Thread.new do
2295
2308
puts 'I am arr[1]'
2309
+ sleep 5
2310
+ puts 'After arr[1]'
2296
2311
end,
2297
- Thread.new { sleep 8}
2312
+ Thread.new do
2313
+ puts 'I am arr[2]'
2314
+ sleep 8
2315
+ puts 'After arr[2]'
2316
+ end
2298
2317
]
2299
2318
2300
- puts Thread.list.size # returns 4 (including the main thread)
2319
+ puts " Thread.list.size: #{Thread.list.size}" # returns 4 (including the main thread)
2301
2320
2302
2321
sleep 2
2303
2322
2304
2323
arr.each { |thread| puts "~~~~~ #{thread}" }
2305
2324
2306
- puts Thread.list.size # returns 3 (because arr[0] is dead)
2325
+ puts " Thread.list.size: #{Thread.list.size}" # returns 3 (because arr[0] is dead)
2307
2326
2308
- # arr[1].join # uncomment to see differences
2327
+ arr[1].join # uncomment to see differences
2309
2328
2310
2329
arr.each { |thread| puts "~~~~~ #{thread}" }
2311
2330
2331
+ sleep 7
2312
2332
puts "Exit main thread"
2313
2333
```
2314
2334
You can’t perform that action at this time.
0 commit comments