Posts Tagged ‘threading’
// 300) { text = text + “\r\n\n本文来自CSDN博客,转载请标明出处:” + location.href; clipboardData.setData(“text”, text); } }, 100); } } // ]]> // Thread 是threading模块中最重要的类之一,可以使用它来创建线程。有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法;另一种是 创建一个threading.Thread对象,在它的初始化函数(__init__)中将可调用对象作为参数传入。下面分别举例说明。先来看看通过继承 threading.Thread类来创建线程的例子: threading.Thread #coding=gbk import threading, time, random count = 0 class Counter(threading.Thread): def __init__(self, lock, threadName): ””’@summary: 初始化对象。 @param lock: 琐对象。 @param threadName: 线程名称。 ”’ super(Counter, self).__init__(name = threadName) #注意:一定要显式的调用父类的初始 化函数。 self.lock = lock def run(self): ””’@summary: 重写父类run方法,在线程启动后执行该方法内的代码。 ”’ global count self.lock.acquire() for i in xrange(10000): count = count + 1 self.lock.release() lock = threading.Lock() [...]
// 300) { text = text + “\r\n\n本文来自CSDN博客,转载请标明出处:” + location.href; clipboardData.setData(“text”, text); } }, 100); } } // ]]> // 1.join()方法的使用 join方法,如果一个线程或者一个函数在执行过程中要调用另外一个线程,并且待到其完成以后才能接着执行,那么在调用这个线程时可以使用被调用线程的join方法 例子: import threading, time class MyThread(threading.Thread): def __init__(self, id): threading.Thread.__init__(self) self.id = id def run(self): time.sleep(5) print self.id t = MyThread(1) def func(): t.start() #t.join() print t.isAlive() print func() 输出结果 True(线程运行) none 然后等待5秒以后输出1 [...]

