# 请教一个python的多线程问题

0

```#需求:我想把arr中的数字分别放在setA,setB中,A和B中的数字在多线程执行完后也不能相同

import random

class A:
setA = set()
setB = set()
arr = [1, 2, 3, 4, 5,6,7,8,9,0]

def fun(self, condition):
with condition:
condition.wait()
for i in self.arr:
r = random.randrange(1, 100)
if (r % 2 == 0):
elif(not i in self.setA):
condition.notify()

def tFun(self):
for i in range(10):
t.start()
t.join()
print(self.setA)
print(self.setB)

a = A()
a.tFun()```
hexllo | 菜鸟二级 | 园豆：318

0

setA = set()
setB = set()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

``````for i in arr:
r = random.randrange(0, 2)
if (r == 0):
else:
print(setA)
print(setB)``````

0
``````#加入锁即可,因为懒锁就加载函数开始和函数结束,你这Condition()本身你ctrl+左键看看他源码只不过导入了个锁的概念

import random
from multiprocessing import Lock
import queue

lock = queue
class A:
setA = set()
setB = set()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

def fun(self, lock):
lock.acquire()
for i in self.arr:
if (not i in self.setA):
else:
lock.release()

def tFun(self):
for i in range(10):
lock = Lock()
t.start()
t.join()
print(self.setA)
print(self.setB)

a = A()
a.tFun()

``````

0
``````import random

class A:
setA = set()
setB = set()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

def fun(self, condition):
with condition:
for i in self.arr:
r = random.randrange(1, 100)
if r % 2 == 0 and i not in self.setB:
elif i not in self.setA:

def tFun(self):
for i in range(10):