crawl當動詞主要是"爬行"的意思,
但是用在皮膚就是"讓人起雞皮疙瘩"的意思,
什麼時候會起雞皮疙瘩呢?當然就是覺得害怕或噁心囉......
make sb's skin craw
=> to make sb feel afraid or full of disgust
Her singing makes my skin crawl.
#-*- coding: utf-8 -*-
#!/usr/bin/python
import thread
import time
def Threadfun(string, sleeptime, *args):
while(True):
print '{0}_{1}\n'.format(string, sleeptime)
time.sleep(sleeptime)
if __name__ == "__main__":
for i in range(1,5):
thread.start_new_thread(Threadfun, ("ThreadFun", i))
while(True):
print 'MainThread {0}'.format(thread.get_ident())
time.sleep(1)#-*- coding: utf-8 -*-
#!/usr/bin/python
import thread
import time
def Threadfun(string, sleeptime, lock, *args):
while(True):
print 'Enter_{0}\r\n'.format(string)
time.sleep(sleeptime)
print 'Leave_{0}\r\n'.format(string)
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(Threadfun, ("ThreadFun1", 2, lock))
thread.start_new_thread(Threadfun, ("ThreadFun2", 2, lock))
while (True):
pass#-*- coding: utf-8 -*-
#!/usr/bin/python
import thread
import time
def Threadfun(string, sleeptime, lock, *args):
while(True):
lock.acquire()
print 'Enter_{0}\r\n'.format(string)
time.sleep(sleeptime)
print 'Leave_{0}\r\n'.format(string)
lock.release()
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(Threadfun, ("ThreadFun1", 2, lock))
thread.start_new_thread(Threadfun, ("ThreadFun2", 2, lock))
while (True):
pass#-*- coding: utf-8 -*-
#!/usr/bin/python
from threading import Thread
import time
class MyThread(Thread):
def __init__(self, string, sleeptime):
Thread.__init__(self)
self.sleeptime = sleeptime
self.setName(str(sleeptime))
def run(self):
while(True):
print 'Threadfun_{0}\r\n'.format(self.getName())
time.sleep(self.sleeptime)
if __name__ == "__main__":
thrList = [MyThread('ThreadFun', i) for i in range(1,5)]
# thrList[0]~thrList[3]
for i in range(0,4):
thrList[i].start()
# another way
#for i in range(1,5):
# MyThread('ThreadFun', i).start()#!/usr/bin/python # -*- coding:utf-8 -*- from threading import * import time class itemX: def __init__(self): self.cnt = 0 def produce(self, num=1): self.cnt += 1 def consume(self, num=1): if self.cnt: self.cnt -= 1 else: print 'WARNING***********************WARNING' def isEmpty(self): return not self.cnt def getCount(self): return self.cnt class Producer(Thread): def __init__(self, condition, item, sleeptime=2): Thread.__init__(self) self.con = condition self.item = item self.sleeptime = sleeptime def run(self): while (True): time.sleep(self.sleeptime) self.con.acquire() self.item.produce() print 'produce 1 product\r\n' print self.item.getCount() self.con.notifyAll() self.con.release() class Consumer(Thread): def __init__(self, condition, item, sleeptime=2): Thread.__init__(self) self.con = condition self.item = item self.sleeptime = sleeptime def run(self): while (True): time.sleep(self.sleeptime) self.con.acquire() print '({0})enter'.format(self.getName()) while self.item.isEmpty(): print '({0})wait'.format(self.getName()) self.con.wait() self.item.consume() print '({0})consume 1 product\r\n'.format(self.getName()) print self.item.getCount() self.con.release() if __name__ == "__main__": X = itemX() cond = Condition() Producer(cond, X).start() Consumer(cond, X).start() Consumer(cond, X).start() while (True): pass
#!/usr/bin/python
# -*- coding:utf-8 -*-
from threading import *
import time
class itemX:
def __init__(self):
self.cnt = 0
def produce(self, num=1):
self.cnt += 1
def consume(self, num=1):
if self.cnt:
self.cnt -= 1
else:
print 'WARNING***********************WARNING'
def isEmpty(self):
return not self.cnt
def getCount(self):
return self.cnt
class Producer(Thread):
def __init__(self, condition, event, item, sleeptime=1):
Thread.__init__(self)
self.con = condition
self.event = event
self.item = item
self.sleeptime = sleeptime
def run(self):
while (True):
time.sleep(self.sleeptime)
self.con.acquire()
self.item.produce()
print 'produce 1 product, remain({0})\r\n'.format(self.item.getCount())
self.event.set()
self.con.release()
class Consumer(Thread):
def __init__(self, condition, event, item, sleeptime=1):
Thread.__init__(self)
self.con = condition
self.event = event
self.item = item
self.sleeptime = sleeptime
def run(self):
while (True):
time.sleep(self.sleeptime)
self.con.acquire()
print '({0})enter\r\n'.format(self.getName())
#while self.item.isEmpty():
while (True):
print '({0})wait'.format(self.getName())
self.event.wait()
break
self.item.consume()
self.event.clear()
print '({0})consume 1 product, remain({1})\r\n'.format(self.getName(), self.item.getCount())
self.con.release()
if __name__ == "__main__":
X = itemX()
cond_Con = Condition()
cond_Pro = Condition()
event = Event()
Producer(cond_Pro, event, X).start()
Consumer(cond_Con, event, X).start()
Consumer(cond_Con, event, X).start()
while (True):
pass #-*- coding: utf-8 -*-
#!/usr/bin/python
from threading import Time
def hello(msg):
print msg
t = Timer(3, hello, ['Hello world'])
t.start()
#!/usr/bin/python
#-*- coding:utf-8 -*-
from threading import *
import Queue
import time
class MyThread(Thread):
def __init__(self, condition):
Thread.__init__(self)
self.cond = condition
def run(self):
print '{0} start\r\n'.format(self.getName())
global cnt
while (True):
id = threadPool.get()
if id != None:
self.cond.acquire()
print '{0}_{1}'.format(self.getName(), id)
for x in xrange(101):
cnt += x
time.sleep(2)
print 'cnt = {0}\r\n'.format(cnt)
cnt = 0
self.cond.release()
threadPool.task_done()
threadPool = Queue.Queue(0)
condition = Condition()
cnt = 0
for i in xrange(2):
MyThread(condition).start()
for i in xrange(10):
threadPool.put(i)
threadPool.join()
print 'done'