python 多线程实现循环打印 abc

好久没写过python了, 想自己实践一下把

非阻塞版

  1. import threading
  2. import time
  3. def print_a():
  4. global value
  5. global lock
  6. global stop_flag
  7. while stop_flag:
  8. while True:
  9. if value == 0 or value == 3:
  10. break
  11. lock.acquire()
  12. value = 1
  13. time.sleep(1)
  14. print("aaa")
  15. lock.release()
  16. def print_b():
  17. global value
  18. global lock
  19. global stop_flag
  20. while stop_flag:
  21. while True:
  22. if value == 1:
  23. break
  24. lock.acquire()
  25. value = 2
  26. time.sleep(1)
  27. print("bbb")
  28. lock.release()
  29. def print_c():
  30. global value
  31. global lock
  32. global stop_flag
  33. while stop_flag:
  34. while True:
  35. if value == 2:
  36. break
  37. lock.acquire()
  38. value = 3
  39. time.sleep(1)
  40. print("ccc")
  41. lock.release()
  42. if __name__ == "__main__":
  43. stop_flag = True
  44. value = 0
  45. threads = []
  46. lock = threading.Lock()
  47. thread_a = threading.Thread(target=print_a)
  48. thread_b = threading.Thread(target=print_b)
  49. thread_c = threading.Thread(target=print_c)
  50. threads.append(thread_a)
  51. threads.append(thread_b)
  52. threads.append(thread_c)
  53. for thread in threads:
  54. thread.start()
  55. time.sleep(5)
  56. stop_flag = False

阻塞版

  1. import threading
  2. import time
  3. def print_a():
  4. global value
  5. global stop_flag
  6. global lock
  7. global con
  8. while stop_flag:
  9. try:
  10. lock.acquire()
  11. while value != 0 and value != 3:
  12. con.wait()
  13. time.sleep(1)
  14. value = 1
  15. print("aaa")
  16. con.notify_all()
  17. finally:
  18. lock.release()
  19. def print_b():
  20. global value
  21. global stop_flag
  22. global lock
  23. global con
  24. while stop_flag:
  25. try:
  26. lock.acquire()
  27. while value != 1:
  28. con.wait()
  29. time.sleep(1)
  30. value = 2
  31. print("bbb")
  32. con.notify_all()
  33. finally:
  34. lock.release()
  35. def print_c():
  36. global value
  37. global stop_flag
  38. global lock
  39. global con
  40. while stop_flag:
  41. try:
  42. lock.acquire()
  43. while value != 2:
  44. con.wait()
  45. time.sleep(1)
  46. value = 3
  47. print("ccc")
  48. con.notify_all()
  49. finally:
  50. lock.release()
  51. if __name__ == "__main__":
  52. stop_flag = True
  53. value = 0
  54. threads = []
  55. # 注意这里使用的是条件变量
  56. lock = threading.Lock()
  57. con = threading.Condition(lock=lock)
  58. thread_a = threading.Thread(target=print_a)
  59. thread_b = threading.Thread(target=print_b)
  60. thread_c = threading.Thread(target=print_c)
  61. threads.append(thread_a)
  62. threads.append(thread_b)
  63. threads.append(thread_c)
  64. for thread in threads:
  65. thread.start()
  66. time.sleep(5)
  67. print("stop")
  68. stop_flag = False
  69. for thread in threads:
  70. thread.join()

python 多线程实现循环打印 abc的更多相关文章

  1. Java多线程循环打印ABC的5种实现方法

    https://blog.csdn.net/weixin_39723337/article/details/80352783 题目:3个线程循环打印ABC,其中A打印3次,B打印2次,C打印1次,循环 ...

  2. 多线程循环打印ABC

    主要是利用线程的wait()和notify()来实现 public class MyThread implements Runnable { private String name; private ...

  3. 多线程同步循环打印和Guarded suspension 模式

     * 迅雷笔试题: * 有三个线程ID分别是A.B.C,请有多线编程实现,在屏幕上循环打印10次ABCABC…  由于线程执行的不确定性,要保证这样有序的输出,必须控制好多线程的同步. 线程同步有两种 ...

  4. python中while循环打印星星的四种形状

    在控制台连续输出五行*,每一行星号数量一次递增 * ** *** **** ***** #1.定义一个行计数器 row = 1 while row <= 5: #定义一个列计数器 col = 1 ...

  5. 用三个线程按顺序循环打印ABC三个字母

    有两种方法:semaphore信号量和mutex互斥锁.需要注意的是C++11已经没有semaphore. C++ 并发编程(六):信号量(Semaphore) - 止于至善 - SegmentFau ...

  6. python使用for循环打印9*9乘法表。

    代码如下: for a in range(1, 10): for b in range(1, 10): if b <= a: print("%d*%d=%d\t" % (b, ...

  7. java多线程编程之连续打印abc的几种解法

    一道编程题如下: 实例化三个线程,一个线程打印a,一个线程打印b,一个线程打印c,三个线程同时执行,要求打印出10个连着的abc. 题目分析: 通过题意我们可以得出,本题需要我们使用三个线程,三个线程 ...

  8. Java多线程wait和notify协作,按序打印abc

    有一个经典的多线程面试题:启三个线程,按序打印ABC 上代码: package cn.javaBase.study_thread1; class MyRunnable1 implements Runn ...

  9. python—用for循环、while循环和一句话打印九九乘法表

    用for循环打印九九乘法表: for i in range (1,10): for j in range(1,10): print(j,"x",i,"=",i* ...

随机推荐

  1. 记_JavaEE框架应用开发期末设计(一)

    日志 工作者:Black_YeJing 工作目标:实现卖家dao层的商品的增删改查(只能对自己发布的进行增删改查). 工作进程追踪: ①创建了Shop类(卖家类) ②创建了ShopDao的接口里面编写 ...

  2. How to enable remote connections to SQL Server

    <img src="https://miro.medium.com/max/1400/1*18lrHvJ8YtADJDT7hxIThA.jpeg" class="g ...

  3. centos7--web项目使用远程mysql数据库

    07-django项目连接远程mysql数据库   比如电脑a(ip地址为192.168.0.aaa)想要连接访问电脑b(ip地址为192.168.0.bbb)的数据库: 对电脑a(ip地址为192. ...

  4. 原型相关的知识点-new的实现原理

    let obj = {}let fn = function(){ this.content = 'zhangsan'} let fn2 = new fn() fn2是fn实例化出来的一个对象,要了解n ...

  5. OSCP-Kioptrix2014-1 环境搭建

    环境搭建 该系列文章参考 : https://www.youtube.com/watch?v=bWM0BCQ5q1o&list=PL9WW-prbqvGzHsGK_OqTyYWbCZjucpI ...

  6. ubuntu版本信息查看

    1.cat /etc/issue 2.cat /etc/lsb-release 3.uname -a 4.cat /proc/version 5.lsb_release -a 显卡信息1.lspci ...

  7. python模块、面向对象编程

    目录: 模块补充 xml 面向对象 一.模块补充 shutil: 文件复制模块:进行文件copy.压缩: 使用方法: 将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfileobj( ...

  8. Linux系统组成和获取命令帮助2

    基于cobbler进行网络安装:    https://cobbler.github.io/ 终端:terminal        无论是系统的图形界面还是文字界面,都可以叫做控制台,终端       ...

  9. 如何保存ActionMailbox inbound HTML email和关于ActionText与ActiveStorage的附加

    gi代码: https://github.com/gorails-screencasts/action-mailbox-action-text/commit/3aeedc09441696c9489ed ...

  10. CSS基础学习-2.CSS选择器(上)

    元素选择符 关系选择符 属性选择符 伪类选择符 伪对象选择符 一.元素选择符 1.通配符:*{ } 2.类选择符:.类名称{ } 3.id选择符::#id名称{ } 4.类型选择符(标签选择符):标签 ...