Demo entry 6625114

1

   

Submitted by 1 on Jun 19, 2017 at 13:06
Language: Python. Code size: 6.9 kB.

init python:
    import threading
    from random import randint
    import time
    import Queue
    # condition = threading.Condition()
    products = 0
    # limit = 30
    occupyNow = "" ##CPU占用线程
    logging = ""

    runnableQueue = Queue.Queue()
    blockedQueue = Queue.Queue()

    class Producer(threading.Thread):
        '''生产者'''
        def __init__(self, runTime, totalTime):
            threading.Thread.__init__(self)
            self.runTime = int(runTime) ##时间片
            self.startTime = time.time()

            self.totalTime = int(totalTime)
            self.totalRunTime = 0

            self.count = int(totalTime)/int(runTime)
            self.rate = 0
            self.status = "runnable"
            self.runFlag = False

        # def checkTime(self):
        #     if time.time() - self.startTime >= self.runTime:
        #         return False
        #     else:
        #         return True
        def run(self):
            global products, timeNow, occupyNow, blockedQueue, runnableQueue
            while True:
                if self.runFlag == True:
                    actionTipsContentAdd("{size=11}"+timeNow+'{/size}{color=#BCD2EE}(生)'+self.getName()+'{/color} 从{color=#FFDEAD}就绪{/color}进入{color=#B3EE3A}运行{/color}状态。')
                    self.status = "running"
                    occupyNow = self.getName()

                    time.sleep(self.runTime)

                    if self.totalTime%self.runTime != 0 and self.rate == self.count:
                        actionTipsContentAdd("      {color=#FFFFF050}————经过{size=28} "+str(self.totalTime%self.runTime)+" {/size}秒————{/color}")

                    else:
                        actionTipsContentAdd("      {color=#FFFFF050}————经过{size=28} "+str(self.runTime)+" {/size}秒————{/color}")

                    self.rate += 1
                    self.totalRunTime += self.runTime


                    if self.totalRunTime < self.totalTime:

                        actionTipsContentAdd("{size=11}"+timeNow+'{/size}{color=#BCD2EE}(生)'+self.getName()+'{/color} 时间片到,从{color=#B3EE3A}运行{/color}进入{color=#FFDEAD}就绪{/color}状态。')
                    if self.totalRunTime >= self.totalTime:

                        products += 1
                        
                        actionTipsContentAdd("{size=11}"+timeNow + '{/size}{color=#BCD2EE}(生)' + self.getName() + '{/color} {color=#B3EE3A}生产{/color}了一个产品。')

                        if blockedQueue.qsize() != 0:

                            bP = blockedQueue.get()
                            runnableQueue.put(bP)
                            bP.status = "runnable"
                            actionTipsContentAdd("{size=11}"+timeNow + '{/size}{color=#BCD2EE}(消)' + bP.getName() + '{/color} 从{color=#FF4500}等待{/color}进入{color=#FFDEAD}就绪{/color}状态。')
                        if runnableQueue.qsize() != 0:

                            newP = runnableQueue.get()
                            newP.runFlag = True

                        self.status = "stopped"
                        break
                    else:

                        self.status = "runnable"
                        runnableQueue.put(self)
                        self.runFlag = False
                        newP = runnableQueue.get()
                        newP.runFlag = True

                time.sleep(0.5)



    class Consumer(threading.Thread):
        '''消费者'''
        def __init__(self, runTime, totalTime):
            threading.Thread.__init__(self)
            self.runTime = int(runTime)
            self.startTime = time.time()

            self.totalTime = int(totalTime)
            self.totalRunTime = 0

            self.count = int(totalTime)/int(runTime)
            self.rate = 0
            self.status = "runnable"
            self.runFlag = False

        # def checkTime(self):
        #     if time.time() - self.startTime >= self.runTime:
        #         return False
        #     else:
        #         return True

        def run(self):
            global products, timeNow, occupyNow, blockedQueue, runnableQueue
            while True:
                if self.runFlag == True:
                    actionTipsContentAdd("{size=11}"+timeNow+'{/size}{color=#BCD2EE}(消)'+self.getName()+'{/color} 从{color=#FFDEAD}就绪{/color}进入{color=#B3EE3A}运行{/color}状态。')
                    if products <= 0:
                        self.status = "blocked"
                        blockedQueue.put(self)

                        actionTipsContentAdd("{size=11}"+timeNow + '{/size}{color=#BCD2EE}(消)' + self.getName() + '{/color} 因为产品小于<=0,进入{color=#FF4500}等待{/color}状态。')

                        self.runFlag = False
                        newP = runnableQueue.get()
                        newP.runFlag = True

                    else:
                        actionTipsContentAdd("{size=11}"+timeNow+'{/size}{color=#BCD2EE}(消)'+self.getName()+'{/color} 从{color=#FFDEAD}就绪{/color}进入{color=#B3EE3A}运行{/color}状态。')
                        self.status = "running"
                        occupyNow = self.getName()

                        time.sleep(self.runTime)
                        if self.totalTime%self.runTime != 0 and self.rate == self.count:

                            actionTipsContentAdd("      {color=#FFFFF050}————经过{size=28} "+str(self.totalTime%self.runTime)+" {/size}秒————{/color}")
                        else:

                            actionTipsContentAdd("      {color=#FFFFF050}————经过{size=28} "+str(self.runTime)+" {/size}秒————{/color}")
                        self.totalRunTime += self.runTime

                        self.rate += 1
                        if self.totalRunTime < self.totalTime:
                            actionTipsContentAdd("{size=11}"+timeNow+'{/size}{color=#BCD2EE}(消)'+self.getName()+'{/color} 时间片到,从{color=#B3EE3A}运行{/color}进入{color=#FFDEAD}就绪{/color}状态。')

                        if self.totalRunTime >= self.totalTime:
                            products -= 1
                            actionTipsContentAdd("{size=11}"+timeNow + '{/size}{color=#BCD2EE}(消)' + self.getName() + '{/color} {color=#FF4500}消费{/color}了一个产品。')

                            if runnableQueue.qsize() != 0:
                                newP = runnableQueue.get()
                                newP.runFlag = True

                            self.status = "stopped"
                            break

                        else:
                            self.status = "runnable"
                            runnableQueue.put(self)
                            self.runFlag = False
                            newP = runnableQueue.get()
                            newP.runFlag = True

                time.sleep(0.5)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).