• <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 閱讀(2038) 評論(0)  編輯 收藏 引用 所屬分類: 可愛的python
            精品久久人人做人人爽综合| 国产精品久久久久久久午夜片| 2021久久国自产拍精品| 亚洲一级Av无码毛片久久精品| 国产亚洲精久久久久久无码AV| 久久免费视频观看| 久久激情五月丁香伊人| 色天使久久综合网天天| 久久久久久久亚洲Av无码| 久久久久久夜精品精品免费啦| 久久se精品一区二区| 青青草国产成人久久91网| 日韩欧美亚洲综合久久影院Ds| 免费久久人人爽人人爽av| 久久精品无码专区免费东京热| 久久人人爽人人爽AV片| 久久国产精品77777| 久久久久久久久久久免费精品| 热久久视久久精品18| 91精品国产91久久久久久| 日本人妻丰满熟妇久久久久久| 国产激情久久久久影院老熟女免费 | 99久久99久久精品国产片| 日韩欧美亚洲综合久久影院Ds| 久久精品国产亚洲AV无码偷窥| 亚洲国产香蕉人人爽成AV片久久| 99久久超碰中文字幕伊人| 思思久久99热只有频精品66| 精品视频久久久久| 91精品国产色综久久 | 精品无码久久久久国产动漫3d| 久久er国产精品免费观看8| 97久久精品人人澡人人爽| 久久亚洲精品无码观看不卡| 久久国产影院| 久久99国产亚洲高清观看首页| 激情综合色综合久久综合| 97久久超碰国产精品2021| 精品免费tv久久久久久久| 亚洲伊人久久成综合人影院 | 精品久久久久久久久久久久久久久 |