馬老師Java零基礎(chǔ)從入門到就業(yè)
from multiprocessing import Processimport timeclass SumProcess(Process):
? ?def __init__(self, n, e=None):
? ? ? ?super().__init__() ?# 調(diào)用父類的初始化方法: 一定寫,因類進程的創(chuàng)建由系統(tǒng),初始化方法由系統(tǒng)調(diào)用。
? ? ? ?self.n = n
? ? ? ?self.e = e ? ?def run(self):
? ? ? ?# 當(dāng)前進程分配資源后,由創(chuàng)建的線程來執(zhí)行的函數(shù),系統(tǒng)調(diào)用的
? ? ? ?s = 0
? ? ? ?for i in range(1, self.n + 1):
? ? ? ? ? ?if self.e and i % self.e == 0:
? ? ? ? ? ? ? ?continue
? ? ? ? ? ?s += i
? ? ? ? ? ?time.sleep(0.001)
? ? ? ? ? ?print(self.name, i)
? ? ? ?print(self.name, self.n, '以內(nèi)的所有數(shù)據(jù)和為', s)
標(biāo)簽: