Chapter 5 Thread

  1. 进程具有两个基本属性:使得进程成为并发执行的基本单位
    1. 是一个拥有资源的独立单位
      • 它可独立分配虚地址空间、主存和其他资源
    2. 是一个可独立调度和分派的基本单位
  2. 引入线程的目的是简化进程间的通信,以小的开销来提高进程内的并发程度
  3. 线程的引入
    1. 进程:资源分配单位(存储器、文件)和CPU调度(分派)单位。又称为"任务(task)"
    2. 线程:作为CPU调度单位,而进程只作为其他资源分配单位。
      • 只拥有必不可少的资源,如:线程状态、程序计数器、寄存器上下文和栈
      • 同样具有就绪、阻塞和执行三种基本状态
      • 与同属一个进程的其它线程共享进程拥有的全部资源
      • 可并发执行
  4. 线程的优点
    • 减少并发执行时间和空间开销(线程的创建、退出和调度),因此容许在系统中建立更多的线程来提高并发程度
    • 线程的创建时间比进程短
    • 线程的终止时间比进程短
    • 同进程内线程切换时间比进程短
    • 同进程线程间可以直接进行而不通过内核进行通信
  5. Thread
    • A thread is a basic unit of CPU utilization, consisting of
      • PC
      • Register set
      • Stack space
    • A thread shares with its peer threads its
      • Coded section
      • Data section
      • OS resources
    • A traditional or heavy-weight process is equal to a task with one thread
  6. 进程和线程的比较
    • 并发性
    • 拥有资源:进程是拥有资源的独立单位
    • 系统开销
    • 地址空间和其他资源
    • 通信:进程间通信IPC,线程间可以直接读写进程数据段来进行通信
    • 调度
  7. Benefits
    • Responsiveness
    • Resource sharing
    • Economy
    • Utilization of MP architecture
  8. 用核线程和内核线程
    • 调度方式
      • 用户线程不需要OS的支持
    • 调度单位
      • 用核线程的调度以进程为单位进行,在采用时间片轮转调度算法时,每个进程分配相同的时间片。对内核级线程,每个线程分配的时间片不同。(优先级?)
  9. Thread pool
    • Reasons
      • Avoid creation and termination overhead, so that it is faster to serve a request
      • Put bound on number of threads, thus limit CPU and memory usage
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的`threading`模块来实现多线程下载。具体步骤如下: 1. 导入`threading`模块和其他需要的模块: ```python import threading import os import requests ``` 2. 定义`download`函数,该函数接收三个参数:`book_file`表示书籍保存的文件夹路径,`chapter_rename`表示章节重命名后的名字,`page_text`表示章节内容。 ```python def download(book_file, chapter_rename, page_text): chapter_path = os.path.join(book_file, chapter_rename) with open(chapter_path, "w", encoding="utf8") as f: for line in page_text: f.write(line.strip() + "\n") ``` 3. 定义`DownloadThread`类,该类继承自`threading.Thread`类。在`__init__`方法中,定义了`book_file`、`chapter_rename`和`page_text`三个属性。在`run`方法中,调用`download`函数下载章节内容。 ```python class DownloadThread(threading.Thread): def __init__(self, book_file, chapter_rename, page_text): super(DownloadThread, self).__init__() self.book_file = book_file self.chapter_rename = chapter_rename self.page_text = page_text def run(self): download(self.book_file, self.chapter_rename, self.page_text) ``` 4. 在主程序中,创建多个`DownloadThread`实例,并调用`start`方法启动线程。 ```python if __name__ == "__main__": book_file = "book" if not os.path.exists(book_file): os.mkdir(book_file) # 假设有10个章节需要下载 for i in range(10): chapter_rename = f"chapter{i}.txt" page_text = [f"line {j}" for j in range(10)] thread = DownloadThread(book_file, chapter_rename, page_text) thread.start() ``` 这样,就可以实现多线程下载了。每个线程都会下载一个章节的内容,并保存到指定的文件中。注意,如果需要下载的章节数量很多,可能会因为同时打开太多文件而导致程序崩溃,需要进行适当的优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值