• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            sunrise

            每天不斷學習,才能不斷提升自己。

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              64 隨筆 :: 0 文章 :: 92 評論 :: 0 Trackbacks
            1.創建threading.Thread的子類來包裝一個線程對象
            #encoding:utf8

            import threading
            import time

            class timer(threading.Thread):
                def __init__(self,num,interval):
                    threading.Thread.__init__(self)
                    #設置產生線程的個數
                    self.thread_num = num
                    #產生線程的相隔時間
                    self.interval = interval
                    self.thread_stop = False

                def run(self):
                    while not self.thread_stop:
                        print 'Thread Object(%d),Time:%s\n'%(self.thread_num,time.ctime())
                        time.sleep(self.interval)

                def stop(self):
                    self.thread_stop = True


            def test():
                thread1 = timer(1,1)
                thread2 = timer(2,2)
                thread1.start()
                thread2.start()
                time.sleep(10)
                thread1.stop()
                thread2.stop()
                return

            if __name__ == '__main__':
                test()
                

            threading.Thread類的使用:

            1).在自己的線程類的__init__里調用threading.Thread.__init__(self, name = threadname)

            Threadname為線程的名字

            2). run(),通常需要重寫,編寫代碼實現做需要的功能。

            3).getName(),獲得線程對象名稱

            4).setName(),設置線程對象名稱

            5).start(),啟動線程

            6).jion([timeout]),等待另一線程結束后再運行。

            7).setDaemon(bool),設置子線程是否隨主線程一起結束,必須在start()之前調用。默認為False

            8).isDaemon(),判斷線程是否隨主線程一起結束。

            9).isAlive(),檢查線程是否在運行中。

                此外threading模塊本身也提供了很多方法和其他的類,可以幫助我們更好的使用和管理線程。可以參看http://www.python.org/doc/2.5.2/lib/module-threading.html
            2.簡單的同步

            #encoding:utf8

            import threading

            mylock 
            = threading.RLock()
            num 
            = 0

            class myThread(threading.Thread):
                
            def __init__(self,name):
                    threading.Thread.
            __init__(self)
                    self.t_name 
            = name

                
            def run(self):
                    
            global num
                    
            while True:
                        mylock.acquire()
                        
            print '\nThread(%s) locked,Number:%d'%(self.t_name,num)
                        
            if num >= 4:
                            mylock.release()
                            
            print '\nThread(%s) released,Number:%d'%(self.t_name,num)
                            
            break
                        num 
            += 1
                        
            print '\nThread(%s) released,Number:%d'%(self.t_name,num)
                        mylock.release()


            def test():
                thread1 
            = myThread('A')
                thread2 
            = myThread('B')
                thread1.start()
                thread2.start()

            if __name__ == '__main__':
                test()

               Pythonthreading module是在建立在thread module基礎之上的一個module,在threading module中,暴露了許多thread module中的屬性。在thread module中,python提供了用戶級的線程同步工具“Lock”對象。而在threading module中,python又提供了Lock對象的變種: RLock對象。RLock對象內部維護著一個Lock對象,它是一種可重入的對象。對于Lock對象而言,如果一個線程連續兩次進行acquire操作,那么由于第一次acquire之后沒有release,第二次acquire將掛起線程。這會導致Lock對象永遠不會release,使得線程死鎖。RLock對象允許一個線程多次對其進行acquire操作,因為在其內部通過一個counter變量維護著線程acquire的次數。而且每一次的acquire操作必須有一個release操作與之對應,在所有的release操作完成之后,別的線程才能申請該RLock對象。修改共享數據的代碼成為“臨界區”。必須將所有“臨界區”都封閉在同一個鎖對象的acquirerelease之間。












            posted on 2013-01-04 14:25 SunRise_at 閱讀(2030) 評論(0)  編輯 收藏 引用 所屬分類: 可愛的python
            国产精品美女久久久| 国内精品免费久久影院| 久久综合久久伊人| 久久亚洲日韩看片无码| 国产精品女同久久久久电影院| 色偷偷888欧美精品久久久| 色综合色天天久久婷婷基地| 久久精品这里只有精99品| 国产美女久久久| 最新久久免费视频| 国产精品免费看久久久| 亚洲国产精品嫩草影院久久| 亚洲精品tv久久久久久久久| 青青草原综合久久大伊人精品| 超级碰久久免费公开视频| 亚洲国产精品成人AV无码久久综合影院| 99久久精品无码一区二区毛片 | 久久99精品国产一区二区三区| 国产精品久久国产精品99盘| 久久国产福利免费| 99精品久久精品一区二区| 久久精品国产99国产精品| 日韩精品久久无码人妻中文字幕| 精品国产福利久久久| 久久久久亚洲精品中文字幕| 久久久久国产精品熟女影院| 亚洲一区精品伊人久久伊人| 成人久久综合网| 国产精品久久久亚洲| 欧美伊人久久大香线蕉综合| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 午夜福利91久久福利| 久久99精品国产自在现线小黄鸭 | 久久久久亚洲AV无码去区首| 国产精品99久久久久久人| 亚洲AV无码久久精品成人| 久久亚洲国产成人影院网站| 一本色道久久88加勒比—综合| 久久综合丝袜日本网| 久久精品国产亚洲网站| 久久免费精品一区二区|