# Information Technology
# Moore's law - Any electronic products that cannot be bought, in 18 The price will be reduced by half or more after the month.
# 18 months, the performance will be overturned.
# Andibir law: Whatever Andy gave you, Bill would take it for you. Hardware performance, but the software can't keep up.
# So when the software updates, the performance requirements for the hardware will increase.
# inverse Moore's law: a it company if today and 18 sold the same many, same products months ago, Its turnover is going to fall by half. it bounds called it the inverse Moore's law.
/span>
# line Cheng
Import Time fromThreadingImportThread, LockclassAccount (object):def __init__(self): self._balance=0 Self._lock=Lock () @propertydefbalance (self):returnself._balancedefDeposit (self, money):"""When multiple threads access a resource at the same time, it is possible that competing resources cause state crashes to be accessed by multiple threads, called critical resources, and access to critical resources needs to be protected. """ ifMoney:self._lock.acquire ()#add lock, start is parallel, lock and turn into serial, unlock and turn into parallel. Try: New_balance= Self._balance +Money Time.sleep (0.01) Self._balance=new_balancefinally: Self._lock.release ()classAddmoneythread (Thread):def __init__(self, account): Super (Addmoneythread, self).__init__() Self._account= AccountdefRun (self): Self._account.deposit (1)defMain (): account=Account () tlist= [] for_inchRange (100): T=addmoneythread (account) tlist.append (t) t.start () forTinchTlist:t.join ()Print('%d Yuan'%account.balance)if __name__=='__main__': Main ()
Five-car Tournament
fromRandomImportRandint fromThreadingImportThreadImportPygameImport TimeclassColor (object): BLACK=(0, 0, 0) White= (255, 255, 255) GRAY= (242, 242, 242) @staticmethoddefRandom_color (): R= Randint (0, 255) G= Randint (0, 255) b= Randint (0, 255) returnR, G, bclassCar (object):def __init__(self, x, y, color): self._x=x self._y=y Self._color=ColordefMove (self):ifSelf._x <= 870: self._x+ = Randint (1, 10) returnTruereturnFalsedefDraw (self, screens): pygame.draw.rect (screen, Self._color, [Self._x, Self._y,80, 40], 0)defMain ():classBackgroundthread (Thread):defRun (self): nonlocal screen is_go_on=True whileIs_go_on:screen.fill (Color.gray) pygame.draw.line (screen, Color.Black, (130, 0), (130, 600), 4) pygame.draw.line (screen, Color.Black, (950, 0), (950, 600), 4) forCarinchcars:car.draw (screen) Pygame.display.flip () Time.sleep (0.05) forCarinchCars:if notcar.move (): is_go_on=False Cars= [] forIinchRange (5): Temp= Car (i * 120 + 50), Color.random_color ()) cars.append (temp) pygame.init () screen= Pygame.display.set_mode ((1000, 600)) Pygame.display.set_caption ('Five Car tournament') backgroundthread (daemon=True). Start () running=True whileRunning: forEventinchpygame.event.get ():ifEvent.type = =Pygame. Quit:running=False pygame.quit ()if __name__=='__main__': Main ()
Python fourth week multi thread and multi-process line Cheng, five car tournament