









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










import time import javax.swing as swing class Timer: def init(self): self.callbacks = [] def registerCallback(self, callback): self.callbacks.append(callback) def waitForEvent(self): while 1: time.sleep(5) for cb in self.callbacks: cb() def myCallback(): now = time.localtime(time.time()) print "The time is now " + str(now[3]) + ":" + str(now[4]) + ":" + str(now[5]) if name == "main": disp = Timer() disp.registerCallback(myCallback) disp.waitForEvent() swing.JFrame(”I’m running”).show() The Problem: the waitForEvent() method has to block waiting for the timer to expire. The method never returns control back to the main program.
import time import thread import threading import javax.swing as swing class Timer: def init(self): self.callbacks = [] self.thread = threading.Thread(target=self.waitForEvent) self.thread.start() def registerCallback(self, callback): self.callbacks.append(callback) def waitForEvent(self): while 1: time.sleep(5) for cb in self.callbacks: cb() def myCallback(): now = time.localtime(time.time()) print "The time is now " + str(now[3]) + ":" + str(now[4]) + ":" + str(now[5]) if name == "main": disp = Timer() disp.registerCallback(myCallback) swing.JFrame("hello").show() Make a new thread to execute code Start it running. Make it execute the waitForEvent() method forever. The second thread starts when the Timer is created. No need to call waitForEvents() here.
Jack Jill Look in fridge, out of milk Leave for store Arrive at store Look in fridge, out of milk Buy milk Leave for store Arrive home, put milk away Arrive at store Buy milk Arrive home, put milk away Oh no!
import threading, time, random class Counter: def init(self): self.count = 0; def increment(self): self.count = self.count + 1 return self.count counter = Counter() class Worker: def init(self, name): self.thread = threading.Thread(target=self.run) self.thread.start() self.name = name def run(self): for i in range(10): value = counter.increment() time.sleep(random.randint(10, 100) / 1000.0) print self.thread.getName(), "finished", value for i in range(10): w = Worker(i)
The Problem: Multiple workers may try to run this line of code at the same time. Worker 1 looks up the value of self.count and adds 1 to it. Worker 2 does the same thing. Gets the same value that Worker 1 sees. Both then assign to self.count. Effective result is that one increment has been lost.
import threading, time, random class Counter: def init(self): self.lock = threading.Lock() self.count = 0; def increment(self): self.lock.acquire() value = self.count = self.count + 1 self.lock.release() return value counter = Counter() class Worker: def init(self, name): self.thread = threading.Thread(target=self.run) self.thread.start() self.name = name def run(self): for i in range(10): value = counter.increment() time.sleep(random.randint(10, 100) / 1000.0) print self.thread.getName(), "finished", value for i in range(10): w = Worker(i)
This is the critical section of code. Protect it with a lock!