1. 线程是最小的执行单位。
2. 线程间共享全局变量。
3. 线程间存在资源竞争(全局变量)的问题。
import time import threading def sing(): """唱歌 5秒钟""" for i in range(5): print("----正在唱:菊花茶----") time.sleep(1) def dance(): """跳舞 5秒钟""" for i in range(5): print("----正在跳舞----") time.sleep(1) def main(): t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() if __name__ == "__main__": main()
import time import threading def sing(): """唱歌 5秒钟""" for i in range(5): print("----正在唱:菊花茶----") time.sleep(1) def dance(): """跳舞 5秒钟""" for i in range(5): print("----正在跳舞----") time.sleep(1) def main(): t1 = threading.Thread(target=sing) t2 = threading.Thread(target=dance) t1.start() t2.start() if __name__ == "__main__": main()
import threading import time def test1(): for i in range(5): print("-----test1---%d---" % i) time.sleep(1) # 如果创建Thread时执行的函数,运行结束那么意味着 这个子线程结束了.... def test2(): for i in range(10): print("-----test2---%d---" % i) time.sleep(1) def main(): t1 = threading.Thread(target=test1) t2 = threading.Thread(target=test2) t1.start() t2.start() while True: # 查看运行的线程数 print(threading.enumerate()) if len(threading.enumerate())<=1: break time.sleep(1) if __name__ == "__main__": main()
import threading import time # 定义一个全局变量 g_num = 0 def test1(num): global g_num for i in range(num): g_num += 1 print("-----in test1 g_num=%d----" % g_num) def test2(num): global g_num for i in range(num): g_num += 1 print("-----in test2 g_num=%d=----" % g_num) def main(): # target指定将来 这个线程去哪个函数执行代码 # args指定将来调用 函数的时候 t1 = threading.Thread(target=test1, args=(1000000,)) t2 = threading.Thread(target=test2, args=(1000000,)) t1.start() t2.start() # 等待上面的2个线程执行完毕.... time.sleep(5) print("-----in main Thread g_num = %d---" % g_num) if __name__ == "__main__": main()
import threading import time # 定义一个全局变量 g_num = 0 def test1(num): global g_num # 上锁的区域越小越好 for i in range(num): mutex.acquire() # 上锁 g_num += 1 mutex.release() # 解锁 print("-----in test1 g_num=%d----" % g_num) def test2(num): global g_num for i in range(num): mutex.acquire() # 上锁 g_num += 1 mutex.release() # 解锁 print("-----in test2 g_num=%d=----" % g_num) # 创建一个互斥锁,默认是没有上锁的 mutex = threading.Lock() def main(): t1 = threading.Thread(target=test1, args=(1000000,)) t2 = threading.Thread(target=test2, args=(1000000,)) t1.start() t2.start() # 等待上面的2个线程执行完毕.... time.sleep(2) print("-----in main Thread g_num = %d---" % g_num) if __name__ == "__main__": main()
import socket import threading def recv_msg(udp_socket): """接收数据并显示""" # 接收数据 while True: recv_data = udp_socket.recvfrom(1024) print(recv_data) def send_msg(udp_socket, dest_ip, dest_port): """发送数据""" # 发送数据 while True: send_data = input("输入要发送的数据:") udp_socket.sendto(send_data.encode("utf-8"), (dest_ip, dest_port)) def main(): """完成udp聊天器的整体控制""" # 1. 创建套接字 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2. 绑定本地信息 udp_socket.bind(("", 7890)) # 3. 获取对方的ip dest_ip = input("请输入对方的ip:") dest_port = int(input("请输入对方的port:")) # 4. 创建2个线程,去执行相应的功能 t_recv = threading.Thread(target=recv_msg, args=(udp_socket,)) t_send = threading.Thread(target=send_msg, args=(udp_socket, dest_ip, dest_port)) t_recv.start() t_send.start() if __name__ == "__main__": main()
import threading import time # 定义一个全局变量 g_num = 0 def test1(num): global g_num # 上锁,如果之前没有被上锁,那么此时 上锁成功 # 如果上锁之前 已经被上锁了(创建的互斥锁被上锁了),那么此时会堵塞在这里,直到 这个锁被解开位置 mutex.acquire() for i in range(num): g_num += 1 # 解锁 mutex.release() print("-----in test1 g_num=%d----" % g_num) def test2(num): global g_num mutex.acquire() for i in range(num): g_num += 1 mutex.release() print("-----in test2 g_num=%d=----" % g_num) # 创建一个互斥锁,默认是没有上锁的 mutex = threading.Lock() def main(): t1 = threading.Thread(target=test1, args=(1000000,)) t2 = threading.Thread(target=test2, args=(1000000,)) t1.start() t2.start() # 等待上面的2个线程执行完毕.... time.sleep(2) print("-----in main Thread g_num = %d---" % g_num) if __name__ == "__main__": main()