javaマルチスレッドメモ(一)

5458 ワード

      ?
                
            (  )   
             

      ?
                  
                  
                 
       -------------------------------------
             :          
       ------------------------------------
      :  1/     cpu      2/         3/         
=========================================
1/     :
    java       :
       Thread    Runable  
       ----------------------------------------
           public static void main(String[] args) {
               Thread t = Thread.currentThread();
               System.out.println(t.getName());
               t.setName("modify");
               System.out.println(t.getName());
           }
       -----------------------------------------
(   )      
        ------------------------------------
          Thread   -->     run()           ,  start    
        ------------------------------------
          Runnable   -->     run()           ,  start    
        ------------------------------------
          :                          
          :                      
                                                     ,    ,java     ,     
                                        ----------------------------------------------------------
                                                 start()   
                                                     

 ======================================================
          :          
        --------------------------------------------
            public static void main(String[] args) {
                Thread thread = new testThread();
                Thread thread1 = new testThread();
                thread.start();         //     
                thread1.start();          //     
            }
         ------------------------------------------
              :
                Thread-0--->-56-
                Thread-0--->-57-
                Thread-1--->-0-
                Thread-1--->-1-
                Thread-1--->-2-  .....         ,     ,                    
                                                                 ....
        ------------------------------------------------------
                                                   run     
                                            ------------------------
                                                    thread.run();
                                                    thread1.run();
                                            -------------------------
                                              :
                                                main--->-98-
                                                main--->-99-
                                                main--->-0-
                                                main--->-1-
                                                .............
        ------------------------------------------------------
                                            1/             
                                            2/        run()  
=========================================================================================
   (   )      

                   //  ----->               new  Thread ()           
                   //  cpu  ---->                     .start()   
                                            . sleep()  
            
            
        ----------------------------------
                   api   =========     
                setPriority()            
                                  1~10   ,  1      ,10            5  ;
                            thread.setPriority(Thread.MAX_PRIORITY);//            
                join()                    
                                 A     B                      B.join();
                               B        A               ,       
                yield()                       
                                                                 
                                          
                                    run        Thread.yield();         
                isAlive()            ..
                sleep()                 
                            thread.sleep(1000);  //      
        ----------------------------------
    (   )      
               
                 
        -----------------------------------------------
                       ,         
                            ----------------------------
                                :   synchronize    ,           
                                 ,                       
                            ----------------------------
                  ,           
                    
        -----------------------------------------------
                   :             ,           ;         ,         。
                      :
                            1、    ;2、      (    );3、    ;4、    。
                         ,           ,       。  ,            :
                          1、          ,  "    "  ;
                          2、                 ,    "    "  ;
                          3、         ,  "  "  。
                                          ,                        。
                            。               。
                                      ,              ,        。
                      "    "  ,           ,               。
                          :1、     ;2、