Python 程序的输出 | 第十六套

简介: Python 程序的输出 | 第十六套
  • 难度级别: 简单

1)  以下程序的输出是什么?

python

复制代码

import threading
barrier = threading.Barrier(4)
class thread(threading.Thread):
  def __init__(self, thread_ID, thread_name):
    threading.Thread.__init__(self)
    self.thread_ID = thread_ID
    self.thread_name = thread_name
  def run(self):
    print("ThreadID = " + str(self.thread_ID) + ", ThreadName = " +
self.thread_name + "\n")
    try:
      barrier = threading.Barrier(4)
      barrier.wait()
    except:
      print("barrier broken")
thread1 = thread(100, "HY")
thread2 = thread(101, "Hai")
thread3 = thread(102, "Haiyong")
thread1.start()
thread2.start()
thread3.start()
barrier.wait()
print("Exit")

a) ThreadID = 100, ThreadName = HY

ThreadID = 101, ThreadName = Hai

ThreadID = 102, ThreadName = Haiyong

b) ThreadID = 100, ThreadName = HY

ThreadID = 101, ThreadName = Hai

ThreadID = 102, ThreadName = Haiyong

Exit

c) 编译错误

d) 运行时错误

答:a)

解释: 这是一个死锁的例子。每个线程创建自己的屏障并在该屏障上调用 .wait() 函数。

2)  以下哪一项不是以下程序的输出?

import threading
class thread(threading.Thread):
  def __init__(self, thread_ID, thread_name):
    threading.Thread.__init__(self)
    self.thread_ID = thread_ID
    self.thread_name = thread_name
  def run(self):
    print(self.thread_name)
thread1 = thread(100, "HY")
thread2 = thread(101, "Hai")
thread3 = thread(102, "Haiyong")
thread1.start()
thread2.start()
thread3.start()
print("Exit")

a) HY Hai Haiyong 退出

b) 退出 Hai Haiyong HY

c) HY 退出 Haiyong Hai

d) 以上都不是

答:(d)

说明: 在线程上调用 start() 方法会将线程移动到就绪状态。线程调度器负责调度线程。因此,可以随时安排特定线程。

3)  以下程序的输出是什么?

import threading
class thread(threading.Thread):
  def __init__(self, thread_ID, thread_name):
    threading.Thread.__init__(self)
    self.thread_ID = thread_ID
    self.thread_name = thread_name
  def run(self):
    print(self.thread_name)
thread1 = thread(100, "HY")
thread2 = thread(101, "Hai")
thread3 = thread(102, "Haiyong ")
thread = []
thread.append(thread1)
thread.append(thread2)
thread.append(thread3)
thread1.start()
thread2.start()
for thread in thread:
  thread.join()
thread3.start()
print("Exit")

答。 (C)

说明: 无法在尚未开始执行的线程上调用 join() 方法。

4)以下程序的输出是什么?

import threading
i = 5
class thread(threading.Thread):
  def __init__(self, thread_ID, thread_name):
    threading.Thread.__init__(self)
    self.thread_ID = thread_ID
    self.thread_name = thread_name
  def run(self):
    i = i + 1
    print(i)
thread1 = thread(100, "HY")
thread2 = thread(101, "Hai")
thread1.start()
thread2.start()

a) 66

b) 67

c) 编译错误

d) 运行时错误

答: (d)

说明: 每个线程都有自己的内存保留空间。因此,对于每个线程,thread1 和 thread2,变量 temp 未声明,因为 temp 未在线程的 run 方法中定义。

5)  以下程序的输出是什么?

import threading
class thread(threading.Thread):
  def __init__(self, thread_ID):
    self.thread_ID = thread_ID
  def run(self):
    print(self.thread_ID)
thread1 = thread(100)
thread1.start()

a) 100

b) 编译错误

c) 运行时错误

d) 这些都不是

答:(C)

说明:  thread.__init__() 必须由在 __init__ 函数中创建的每个线程显式调用。

目录
相关文章
|
7天前
|
数据处理 UED Python
Python 进度条:告别枯燥等待,让你的程序动感十足!
Python 进度条:告别枯燥等待,让你的程序动感十足!
27 1
|
21天前
|
IDE Linux 开发工具
Python中编写第一个 Python 程序
【7月更文挑战第27天】
21 7
|
17天前
|
消息中间件 网络协议 Python
信号传递新风尚!Python IPC,让你的程序间沟通无界限
【8月更文挑战第3天】在多程序系统中,进程间通信(IPC)是实现数据共享与协作的关键。Python提供多种IPC机制,如管道、消息队列和套接字,使信息交流高效灵活。通过`multiprocessing.Pipe()`,进程间可直接传递消息;利用消息队列实现异步通信,提高解耦与扩展性;借助socket库,支持网络内外进程通信。合理运用这些技术,能够显著增强程序间的协同能力,构建更灵活、可扩展的系统。
34 1
|
19天前
|
Python
惊!Python进程间通信IPC,让你的程序秒变社交达人,信息畅通无阻
【8月更文挑战第1天】在编程世界中,进程间通信(IPC)犹如一场社交舞会,各进程通过IPC机制优雅地交换信息,共同完成复杂任务。IPC就像隐形桥梁,连接并行运行的进程,使它们能跨越边界自由沟通。Python提供了多种IPC机制,如管道、队列、共享内存和套接字等,适应不同需求。例如,使用`multiprocessing.Queue`实现进程间通信,生产者向队列添加数据,消费者取出并处理数据,两者虽独立却能有效协作。IPC打破了进程界限,使得程序能像社交达人般自由交流,构建出高效、灵活的应用。掌握IPC,让程序信息畅通无阻。
16 1
|
21天前
|
JSON 监控 开发者
Python I/O管理新篇章:优化你的程序,让数据流动更顺畅
【7月更文挑战第30天】在数据驱动时代, Python I/O操作效率至关重要。理解I/O瓶颈,使用缓冲技术(如调整`open`的`buffering`参数),并发与异步I/O(借助`asyncio`),高效序列化(json, msgpack),及监控调试(cProfile)能显著提升性能。示例展示了缓冲读取和异步文件操作的最佳实践。不断学习可助开发者优化数据流。
37 2
|
29天前
|
Shell 程序员 开发工具
[oeasy]python0026_调试程序_pdb3_帮助_help_求助_文档
调试程序_debug_next_下一步_list_pdb3 🥋
33 1
|
8天前
|
并行计算 开发者 Python
解锁Python多进程编程的超能力:并行计算的魔法与奇迹,探索处理器核心的秘密,让程序性能飞跃!
【8月更文挑战第12天】在Python编程领域,多进程编程是一项关键技能,能有效提升程序效率。本文通过理论与实践结合,深入浅出地介绍了Python中的多进程编程。首先解释了多进程的概念:即操作系统中能够并发执行的多个独立单元,进而提高整体性能。接着重点介绍了`multiprocessing`模块,演示了如何创建和启动进程,以及进程间的通信方式,如队列等。此外,还提到了更高级的功能,例如进程池管理和同步原语等。通过这些实例,读者能更好地理解如何在实际项目中利用多核处理器的优势,同时注意进程间通信和同步等问题,确保程序稳定高效运行。
22 0
|
1月前
|
网络协议 Python
Scapy一个强大的 Python 程序(一)
Scapy是Python的网络数据包操作工具,用于创建、分析和发送网络包。启动Scapy需以管理员权限运行`sudo scapy`。在交互式环境中,可构建自定义数据包,如设置IP包的`ttl`、`src`和`dst`。通过`/`叠加协议层,如IP和TCP。发送数据包示例:构造向`www.slashdot.org`的HTTP GET请求。Scapy还能用于嗅探、过滤和修改数据包,功能强大。
|
1月前
|
网络协议 安全 Python
Scapy一个强大的 Python 程序(二)
Scapy是Python的网络安全工具,可用于创建和修改网络包
|
17天前
|
测试技术 程序员 开发者
探索代码整洁之道:编写可维护和可扩展的Python程序
【8月更文挑战第3天】在编程的海洋中,我们经常追求的是那些能够高效运行、易于理解和维护的代码。本文将深入探讨如何通过遵循一系列的最佳实践来提升Python代码的整洁度,从而增强其可维护性和可扩展性。我们将通过具体示例,展示如何应用这些原则来编写更优雅、更健壮的Python程序。
11 0