from random import randint
import threading
from time import ctime, sleep
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
class MyThread(threading.Thread):
lock = threading.Lock()
def __init__(self, task):
super(MyThread, self).__init__()
self.task = task
def run(self):
print("In time:{} Start Function:{} ".format(ctime(), self.task.__name__))
self.lock.acquire()
self.task()
self.lock.release()
def a():
#从后往前依次修改列表中的元素,
for i in data[::-1]:
new_val = 'xxx%s' % (randint(100, 150))
index = data.index(i)
data[index] = new_val
print("Original value:{} with index:{} modified to:{}".format(i, index, new_val))
def b():
#从前往后依次读取列表
for i in data:
print("Read Value:{} index:{}".format(i, data.index(i)))
def main():
funcs = [a, b]
threads = []
loop = range(len(funcs))
for i in loop:
t = MyThread(funcs[i])
threads.append(t)
for i in loop:
threads[i].start()
for i in loop:
threads[i].join()
if __name__ == '__main__':
main()
print(" all DONE!!!")
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341