Skip to content

Commit 1144f10

Browse files
committed
committed from zkp
1 parent 66575cf commit 1144f10

6 files changed

Lines changed: 110 additions & 0 deletions

File tree

多线程_多进程/17.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time, threading
2+
def loop1():
3+
print("loop1 start{}".format(time.ctime()))
4+
time.sleep(4)
5+
print("loop1 end {}".format(time.ctime()))
6+
def loop2():
7+
print("loop2 start{}".format(time.ctime()))
8+
time.sleep(2)
9+
print("loop2 end {}".format(time.ctime()))
10+
11+
def main():
12+
start = time.time()
13+
print("**"*10)
14+
t1 = threading.Thread(target=loop1, args=())
15+
t1.start()
16+
t2 = threading.Thread(target=loop2, args=())
17+
t2.start()
18+
t1.join()
19+
t2.join()
20+
end = time.time()
21+
print("程序共用了{}".format(end - start))
22+
main()

多线程_多进程/18.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import time, threading
2+
def loop1():
3+
print("loop1 start{}".format(time.ctime()))
4+
time.sleep(4)
5+
print("loop1 end {}".format(time.ctime()))
6+
def loop2():
7+
print("loop2 start{}".format(time.ctime()))
8+
time.sleep(2)
9+
print("loop2 end {}".format(time.ctime()))
10+
11+
def main():
12+
start = time.time()
13+
print("**"*10)
14+
t1 = threading.Thread(target=loop1, args=())
15+
t1.setDaemon(True)
16+
t1.start()
17+
t2 = threading.Thread(target=loop2, args=())
18+
t2.start()
19+
# t1.join()
20+
# t2.join()
21+
end = time.time()
22+
print("程序共用了{}".format(end - start))
23+
main()

多线程_多进程/19.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import threading, time
2+
3+
class SayHi(threading.Thread):
4+
def __init__(self,name):
5+
super(SayHi, self).__init__()
6+
self.name = name
7+
8+
def run(self):
9+
time.sleep(2)
10+
print("{} say hello".format(self.name))
11+
12+
if __name__ == '__main__':
13+
t = SayHi("lili")
14+
t.start()
15+
print("主线程")

多线程_多进程/20.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import threading
2+
Sum = 0
3+
loopsum = 1000000
4+
def add():
5+
global Sum, loopsum
6+
for i in range(loopsum):
7+
Sum += 1
8+
9+
def minu():
10+
global Sum , loopsum
11+
for i in range(loopsum):
12+
Sum -= 1
13+
if __name__ == "__main__":
14+
print('start sum={}'.format(Sum))
15+
t1 = threading.Thread(target=add, args=())
16+
t2 = threading.Thread(target=minu, args=())
17+
t1.start()
18+
t2.start()
19+
t1.join()
20+
t2.join()
21+
print(Sum)

多线程_多进程/21.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import multiprocessing
2+
from time import sleep, ctime
3+
4+
def clock(interval):
5+
while True:
6+
print("The time is %s" % ctime())
7+
sleep(interval)
8+
9+
if __name__ == '__main__':
10+
p = multiprocessing.Process(target=clock, args=(5,))
11+
p.start()
12+

多线程_多进程/22.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import multiprocessing
2+
import time
3+
4+
class Clockprocess(multiprocessing.Process):
5+
6+
def __init__(self, interval):
7+
self.interval = interval
8+
super(Clockprocess, self).__init__()
9+
10+
def run(self):
11+
while True:
12+
print("The time is {}".format(time.ctime()))
13+
time.sleep(self.interval)
14+
15+
if __name__ == '__main__':
16+
p = Clockprocess(5)
17+
p.start()

0 commit comments

Comments
 (0)