守護執行緒和執行緒常用方法

2022-09-03 03:21:14 字數 978 閱讀 2247

from threading import

thread

import

time

deftask():

print("

子執行緒 開始工作")

time.sleep(2)

print("

子執行緒 結束工作")

t1 = thread(target=task)

t1.daemon =true

t1.start()

print("

主線程")#

結果:子執行緒 開始工作主線程

current_thread當前執行緒名

active_count當先活躍的執行緒數量

enumerate返回活躍的執行緒物件列舉

name執行緒名字

from  threading import

thread,current_thread,active_count,enumerate

import

time

deftask():

print("

子執行緒"

) time.sleep(1)

print(current_thread())#

當前執行緒名

t = thread(target=task,name="

建立執行緒1號")

print(t.name)#

執行緒名字 print("over")

print(t)#

t.start()

print(active_count())#

當先活躍的執行緒數量 主線程+子執行緒 2

print(enumerate())#

返回活躍的執行緒物件列舉

#[<_mainthread(mainthread, started 14248)>, ]

print("

主線程 over

")

守護執行緒和非守護執行緒

基本概念 示例 非守護執行緒 public class mythread extends thread catch interruptedexception e system.out.println 執行緒 thread.currentthread getname 執行了 i 次 public st...

守護執行緒和守護程序

守護程序隨著主程序的 的執行結束而結束 守護執行緒會在主線程結束之後等待其他子執行緒的結束才結束 如有其他子執行緒,沒有其他子執行緒就是主線程結束守護執行緒隨之結束 主程序在執行玩完自己的 後不會立即結束,而是等待子程序結束之後,子程序的資源 import time from threading i...

主線程 守護執行緒 非守護執行緒

main,但不是守護執行緒。是指在程式執行的時候在後台提供一種通用服務的執行緒。如gc。也叫使用者執行緒,由使用者建立。主線程和守護執行緒一起銷毀 主線程和非守護執行緒互不影響。例如 package com.peng.thread 1 使用者執行緒 非守護執行緒 有主線程建立 2 守護執行緒和主線程...