题目描述
问题描述:有4个线程和1个公共的字符数组。线程1的功能就是向数组输出A,线程2的功能就是向字符输出B,线程3的功能就是向数组输出C,线程4的功能就是向数组输出D。要求按顺序向数组赋值ABCDABCDABCD,ABCD的个数由线程函数1的参数指定。[注:C语言选手可使用WINDOWS SDK库函数]
接口说明:
void init(); //初始化函数
void Release(); //资源释放函数
unsignedint__stdcall ThreadFun1(PVOID pM) ; //线程函数1,传入一个int类型的指针[取值范围:1 – 250,测试用例保证],用于初始化输出A次数,资源需要线程释放
unsignedint__stdcall ThreadFun2(PVOID pM) ;//线程函数2,无参数传入
unsignedint__stdcall ThreadFun3(PVOID pM) ;//线程函数3,无参数传入
Unsigned int __stdcall ThreadFunc4(PVOID pM);//线程函数4,无参数传入
char g_write[1032]; //线程1,2,3,4按顺序向该数组赋值。不用考虑数组是否越界,测试用例保证
输入描述:
输入一个int整数
输出描述:
输出多个ABCD
示例1
输入
10
输出
ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
运用多线程,但结果不行
#构造类方法不通过循环执行
import threading
def go1():
with con:
print('A',end='')
con.wait()
con.notify()
def go2():
with con:
print('B',end='')
con.notify()
con.wait()
def go3():
with con:
print('C',end='')
con.notify()
con.wait()
def go4():
with con:
print('D',end='')
con.notify()
con.wait()
n=int(input())
con = threading.Condition()
for i in range(n):
threading.Thread(target=go1).start()
threading.Thread(target=go2).start()
threading.Thread(target=go3).start()
threading.Thread(target=go4).start()
#继承类方法无法实现线程1控制数量
import threading
class Mythread1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# self.num=num
self.line=''#定义一个line,后面用类的.方式调用属性
def run(self):
global condition
with condition:
self.line+='A'
class Mythread2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.line=''#定义一个line,后面用类的.方式调用属性
def run(self):
global condition
with condition:
self.line+='B'
class Mythread3(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.line=''#定义一个line,后面用类的.方式调用属性
def run(self):
global condition
with condition:
self.line+='C'
class Mythread4(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.line=''#定义一个line,后面用类的.方式调用属性
def run(self):
global condition
with condition:
self.line+='D'
while True:
try:
condition=threading.Condition()
result=[]
threadlist=[]
N=int(input())
for i in range(N):
th1=Mythread1()
th1.start()
th2=Mythread2()
th2.start()
th3=Mythread3()
th3.start()
th4=Mythread4()
th4.start()
threadlist.append(th1)
threadlist.append(th2)
threadlist.append(th3)
threadlist.append(th4)
for eachthread in threadlist:
result.append(eachthread.line)
print(''.join(result))
except:
break