为什么子线程没有运行

为什么子线程没有运行

相关代码:

"""
幼儿园举办捡豆子比赛,三个小朋友一组,
将地上的 30 颗豆子捡起来,每个小朋友捡起一颗豆子,
随机耗时1-3秒,请运用多线程相关的知识,
编程实现三个小朋友捡豆子,并看看谁捡的豆子多
"""
import random
import threading
import time

ranking = []
douzi = list(range(1,31))
count = 0
def douzi_jian():
    while len(douzi) > 1:
        t = threading.current_thread()
        sleep_time = random.randint(1,3)
        choice = random.choice(douzi)
        douzi.remove(choice)
        print('{}捡起了一颗豆子,豆子的编号为{}'.format(t.name,choice))
    ranking.append(t.name,count)
    print('{}完成了比赛'.format(t.name))

class ChidThread1(threading.Thread):
    def douzi(self):
        douzi_jian()
        
class ChidThread2(threading.Thread):
    def douzi(self):
        douzi_jian()
        
class ChidThread3(threading.Thread):
    def douzi(self):
        douzi_jian()
        
chid1_thread = ChidThread1(name="小朋友1")
chid2_thread = ChidThread2(name="小朋友2")
chid3_thread = ChidThread3(name="小朋友3")

chid1_thread.start()
chid2_thread.start()
chid3_thread.start()

chid1_thread.join()
chid2_thread.join()
chid3_thread.join()

print('本次比赛的排名结果为:{}'.format(ranking))


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小猿 2025-03-13 09:54:09

同学,你好!1、线程的执行方法是run方法,继承 threading.Thread 类并重写 run 方法,能够清晰地定义线程要执行的任务,定义线程类时所有的douzi()方法应改成run()方法,例ChildThread1线程类修改如下:

https://img1.sycdn.imooc.com/climg/02bde46709d237b404140146.jpg

2、ranking是列表,append追加元素时,参数只能写一个,可将线程名加入到ranking列表内,修改如红框

https://img1.sycdn.imooc.com/climg/1839a76709d2384d03110056.jpg

3、同学定义了随机数用于time.sleep()进入睡眠状态,利于线程切换,但同学没有使用time.sleep(),time.sleep()应用在从列表中移除捡到的豆子后,再执行time.sleep(),添加代码如下:

https://img1.sycdn.imooc.com/climg/c738226709d239d406310186.jpg

完整代码参考如下:

import random
import threading
import time

ranking = []
douzi = list(range(1, 31))
count = 0


def douzi_jian():
    while len(douzi) > 1:
        t = threading.current_thread()
        sleep_time = random.randint(1, 3)
        choice = random.choice(douzi)
        douzi.remove(choice)
        time.sleep(sleep_time)
        print('{}捡起了一颗豆子,豆子的编号为{}'.format(t.name, choice))
    ranking.append(t.name)
    print('{}完成了比赛'.format(t.name))


class ChidThread1(threading.Thread):
    def run(self):
        douzi_jian()


class ChidThread2(threading.Thread):
    def run(self):
        douzi_jian()


class ChidThread3(threading.Thread):
    def run(self):
        douzi_jian()


chid1_thread = ChidThread1(name="小朋友1")
chid2_thread = ChidThread2(name="小朋友2")
chid3_thread = ChidThread3(name="小朋友3")

chid1_thread.start()
chid2_thread.start()
chid3_thread.start()

chid1_thread.join()
chid2_thread.join()
chid3_thread.join()

print('本次比赛的排名结果为:{}'.format(ranking))

祝学习愉快~

  • 提问者 小方同学_ #1

    代码中哪里是体现谁豆子捡起来的多的?

    2025-03-13 22:27:54
  • 好帮手慕小猿 回复 提问者 小方同学_ #2

    同学,你好!1、以上代码中没有体现哪位小朋友捡起来的豆子多的。作业要求是“编程实现三个小朋友捡豆子,并看看谁捡的豆子多”,其中“看看”没有明确说要输出捡豆子最多的小朋友。若是明确要求“编程实现三个小朋友捡豆子,并输出哪位小朋友捡的豆子多”程序是必须体现哪位小朋友捡起的最多的。
    2、同学要体现哪位小朋友捡的豆子多,可以将小朋友名字及捡豆子个数放到字典中,再通过循环找到捡豆子最多的同学,根据第一名的个数(有时可能有多名小朋友第一),使用if......else......语句输出捡豆子最多的同学,参考代码如下:

    import random
    import threading
    import time
    
    # 定义一个字典来记录每个小朋友捡起豆子的数量
    count_dict = {}
    douzi = list(range(1, 31))
    
    
    def douzi_jian():
        t = threading.current_thread()
        count = 0  # 每个线程本地的计数器
        while len(douzi) > 1:
            sleep_time = random.randint(1, 3)
            choice = random.choice(douzi)
            count += 1
            douzi.remove(choice)
            time.sleep(sleep_time)
            print('{}捡起了一颗豆子,豆子的编号为{}'.format(t.name, choice))
    
        count_dict[t.name] = count  # 将本地计数器的值存入字典
        print('{}完成了比赛'.format(t.name))
    
    
    class ChidThread1(threading.Thread):
        def run(self):
            douzi_jian()
    
    
    class ChidThread2(threading.Thread):
        def run(self):
            douzi_jian()
    
    
    class ChidThread3(threading.Thread):
        def run(self):
            douzi_jian()
    
    
    chid1_thread = ChidThread1(name="小朋友1")
    chid2_thread = ChidThread2(name="小朋友2")
    chid3_thread = ChidThread3(name="小朋友3")
    
    chid1_thread.start()
    chid2_thread.start()
    chid3_thread.start()
    
    chid1_thread.join()
    chid2_thread.join()
    chid3_thread.join()
    
    print("小朋友们的捡豆子数量分别是:", count_dict)
    
    # 找出字典中捡起豆子最多的小朋友
    max_count = 0
    max_names = []
    for name, count in count_dict.items():
        if count > max_count:
            max_count = count
            max_names = [name]
        elif count == max_count:
            max_names.append(name)
    
    if len(max_names) > 1:
        print('本次比赛捡起豆子最多的小朋友有:{},他们都捡起了{}颗豆子'.format(', '.join(max_names), max_count))
    else:
        print('本次比赛捡起豆子最多的小朋友是:{},共捡起了{}颗豆子'.format(max_names[0], max_count))

    祝学习愉快~

    2025-03-14 11:02:06
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师