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
|
#!/usr/bin/env python
import
threading
import
time
def sayHi(n):
time.sleep(
1
)
print
'Hi this is thread %s'
%n
thread_list = [] #用来存放多线程执行返回的函数入口,因此这里存放的是函数入口
for
i
in
range(
20
):
thread_list.append(threading.Thread(target=sayHi, args=(i,)))
for
i
in
thread_list: #执行列表中存放的函数入口,只有执行了之后才会执行sayHi()的程序代码,否则上面只是把函数入口存放在这个列表中而已
i.start()
for
i
in
thread_list: #检测对应的线程是否已经执行完毕(即函数入口是否已经被执行,从而执行相关的程序代码)
i.join() #join()中可以加超时时间,如i.join(
3
),表示当前i对应的函数入口所执行的
线程,如果在
3
秒内还没有执行完就超时,在线程可以正常执行的时候不加也是没有关系的,
但是当线程出现异常而无法正常执行时,由于i.join()还没有检测到线程已经执行完毕,所
以会一直处于等待状态,这样的话就会造成程序的代码不能继续执行(程序还停留在i.join(
)这里等待这一个其对应的线程执行完毕),设定超时时间就可以避免这一个问题,下面会有相关说明
print
'\033[32;1mThis is the last line\033[0m'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ python day6thread1.py
Hi
this
is
thread
0
Hi
this
is
thread
1
Hi
this
is
thread
2
Hi
this
is
thread
3
Hi
this
is
thread
4
Hi
this
is
thread
5
Hi
this
is
thread
6
Hi
this
is
thread
7
Hi
this
is
thread
8
Hi
this
is
thread
9
Hi
this
is
thread
11
Hi
this
is
thread
14
Hi
this
is
thread
17
Hi
this
is
thread
12
Hi
this
is
thread
15
Hi
this
is
thread
16
Hi
this
is
thread
10
Hi
this
is
thread
13
Hi
this
is
thread
18
Hi
this
is
thread
19
This
is
the last line
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env python
import
threading
import
time
def sayHi(n):
time.sleep(
1
)
print
'Hi this is thread %s'
%n
thread_list = []
for
i
in
range(
20
):
thread_list.append(threading.Thread(target=sayHi, args=(i,)))
for
i
in
thread_list:
i.start()
#
for
i
in
thread_list:
# i.join()
print
'\033[32;1mThis is the last line\033[0m'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ python day6thread1.py
This
is
the last line
Hi
this
is
thread
0
Hi
this
is
thread
2
Hi
this
is
thread
6
Hi
this
is
thread
9
Hi
this
is
thread
10
Hi
this
is
thread
15
Hi
this
is
thread
16
Hi
this
is
thread
3
Hi
this
is
thread
8
Hi
this
is
thread
11
Hi
this
is
thread
14
Hi
this
is
thread
17
Hi
this
is
thread
5
Hi
this
is
thread
13
Hi
this
is
thread
4
Hi
this
is
thread
1
Hi
this
is
thread
7
Hi
this
is
thread
12
Hi
this
is
thread
18
Hi
this
is
thread
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#!/usr/bin/env python
import
threading
import
time
def sayHi(n):
time.sleep(
2
)
print
'Hi this is thread %s'
%n
thread_list = []
for
i
in
range(
10
):
thread_list.append(threading.Thread(target=sayHi, args=(i,)))
for
i
in
thread_list:
i.start()
for
i
in
thread_list:
print i.join(
0.1
)
print
'\033[32;1mThis is the last line\033[0m'
|
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
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7$ time python day6thread1.py
None
None
None
None
None
None
None
None
None
None
This
is
the last line
Hi
this
is
thread
0
Hi
this
is
thread
1
Hi
this
is
thread
2
Hi
this
is
thread
3
Hi
this
is
thread
4
Hi
this
is
thread
5
Hi
this
is
thread
7
Hi
this
is
thread
8
Hi
this
is
thread
6
Hi
this
is
thread
9
real 0m2.080s
user 0m0.048s
sys 0m0.016s
|