Thread th = Thread.CurrentThread;什么意思
时间: 2024-06-14 07:03:59 浏览: 6
这段代码`Thread th = Thread.CurrentThread;`的意思是获取当前正在执行的线程对象,并将其赋值给变量`th`。通过调用`Thread.currentThread()`方法,可以获取当前系统正在执行的线程,然后可以对该线程进行其他操作。
相关问题:
1. 如何创建和启动一个线程?
2. 如何在Java中实现多线程同步?
3. 如何处理线程间的通信?
相关问题
优化import os.path import pprint import textwrap import threading import time import requests import re import json from queue import Queue q_list = Queue(100) from threading import Thread headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 ' 'Safari/537.36' } # 获取m3u8视频片段的所有地址 def get_links(url): # 获取视频页的网页源代码 r = requests.get(url, headers=headers) info = re.findall('window.pageInfo = window.videoInfo =(.*?)window.videoResource', r.text, re.DOTALL)[0].strip()[0:-1] # 获取m3u8列表地址 filename = json.loads(info)['title'] m3u8_url = json.loads(json.loads(info)["currentVideoInfo"]["ksPlayJson"])['adaptationSet'][0]['representation'][1]['url'] m3u8_list = requests.get(m3u8_url, headers=headers).text ts_files = re.sub('#.*', '', m3u8_list).split() ts_length = len(ts_files) # 获取m3u8地址片段 for num, ts in enumerate(ts_files): ts_url = 'https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/' + ts q_list.put([ts_url, num]) return filename, ts_length # print(filename, ts_url) # 分别下载这些视频片段-多线程 def download(filename): while not q_list.empty(): ts_url, num = q_list.get() video_content = requests.get(ts_url, headers=headers).content with open(f'video/{filename}_{num}.ts', 'wb') as f: f.write(video_content) print(f'{threading.current_thread().name}已下载...第{num}个片段') # 合并视频-构成完整的片段 def combine(filename, ts_length): fp = open(f'video/{filename}.mp4', 'ab') for i in range(ts_length): if os.path.exists(f'video/{filename}_{i}.ts'): with open(f'video/{filename}_{i}.ts', 'rb') as f: ts_slice = f.read() fp.write(ts_slice) print(f'已合并...第{i}个片段') os.remove(f'video/{filename}_{i}.ts') print(f'已删除...第{i}个片段') fp.close() # 主文件调用 def main(): start_time = time.time() url = 'https://www.acfun.cn/v/ac41409604' filename, ts_length = get_links(url) tasks = [] for i in range(3): th = Thread(target=download, args=(filename,), name=f'线程{i}') th.start() tasks.append(th) for t in tasks: t.join() combine(filename, ts_length) end_time = time.time() print(f'总共耗时{end_time - start_time}')
可以考虑对代码进行如下优化:
1. 将所有的 import 放在文件开头,按照标准库、第三方库和本地库的顺序进行排列,方便阅读和维护。
2. 可以将获取视频片段地址和下载视频片段的代码放在同一个函数中,这样可以减少函数调用和确保两个步骤的原子性。
3. 可以使用 with 语句来打开文件,这样可以确保文件对象被正确关闭,避免出现文件泄露和意外删除的情况。
4. 可以使用 f-strings 来格式化输出,这样可以使代码更加简洁、易读和易维护。
5. 可以对代码中的变量名进行调整,使其更符合 Python 的命名规范,并且更加易于理解和使用。
下面是优化后的代码:
```python
import os.path
import pprint
import textwrap
import threading
import time
import requests
import re
import json
from queue import Queue
from threading import Thread
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
}
def download_video_segments(url, ts_queue):
# 获取视频页的网页源代码
response = requests.get(url, headers=headers)
info = re.findall('window.pageInfo = window.videoInfo =(.*?)window.videoResource', response.text, re.DOTALL)[0].strip()[0:-1]
# 获取m3u8列表地址
filename = json.loads(info)['title']
m3u8_url = json.loads(json.loads(info)["currentVideoInfo"]["ksPlayJson"])['adaptationSet'][0]['representation'][1]['url']
m3u8_list = requests.get(m3u8_url, headers=headers).text
ts_files = re.sub('#.*', '', m3u8_list).split()
ts_length = len(ts_files)
# 获取m3u8地址片段
for num, ts in enumerate(ts_files):
ts_url = 'https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/' + ts
ts_queue.put([ts_url, num])
return filename, ts_length
def download_video_segment(filename, ts_queue):
while not ts_queue.empty():
ts_url, num = ts_queue.get()
video_content = requests.get(ts_url, headers=headers).content
with open(f'video/{filename}_{num}.ts', 'wb') as f:
f.write(video_content)
print(f'{threading.current_thread().name}已下载...第{num}个片段')
def combine_video_segments(filename, ts_length):
with open(f'video/{filename}.mp4', 'ab') as fp:
for i in range(ts_length):
if os.path.exists(f'video/{filename}_{i}.ts'):
with open(f'video/{filename}_{i}.ts', 'rb') as f:
ts_slice = f.read()
fp.write(ts_slice)
print(f'已合并...第{i}个片段')
os.remove(f'video/{filename}_{i}.ts')
print(f'已删除...第{i}个片段')
def main():
start_time = time.time()
url = 'https://www.acfun.cn/v/ac41409604'
ts_queue = Queue(100)
filename, ts_length = download_video_segments(url, ts_queue)
tasks = []
for i in range(3):
th = Thread(target=download_video_segment, args=(filename, ts_queue), name=f'线程{i}')
th.start()
tasks.append(th)
for t in tasks:
t.join()
combine_video_segments(filename, ts_length)
end_time = time.time()
print(f'总共耗时{end_time - start_time}')
package syliu1; public class Syliu1 { public static void main(String[] args) { // TODO Auto-generated method stub th1 t1 = new th1("芜湖"); t1.start(); th1 t2 = new th1("芜湖2"); t2.start(); } } class th1 extends Thread { String name; public th1(String name) { this.name = name; } @Override public void run() { // TODO Auto-generated method stub Thread current= Thread.currentThread(); { System.out.println(current.getName()+":"+name); System.out.println(Thread.currentThread().getName() +":" + name); } } }
这是一个Java程序,其中定义了一个名为"th1"的线程类和一个名为"Syliu1"的主类。在主类中,创建了两个"th1"线程对象,并启动它们。"th1"线程类中,定义了一个名为"name"的字符串变量和一个构造函数,构造函数将传入的参数赋值给"name"变量。在"run"方法中,输出当前线程的名称和"name"变量的值。
你想问我这段代码的作用或者有什么问题吗?
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)