JAVA 다 중 스 레 드 문제 및 처리

http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html

12.4            , , 。           , , 、 , 。 , 。           , 。 , DataThread , 200 n , n 1。 n Data , Data , Data 。 :                    package syn1; /**  *   */ public class Data {          public int n;          public Data(){                    n = 60;          } } package syn1; /**  *   */ public class TestMulThread1 {          public static void main(String[] args) {                    Data data = new Data();                    DataThread d1 = new DataThread(data," 1");                    DataThread d2 = new DataThread(data," 2");          } } package syn1; /**  *   */ public class DataThread extends Thread {          Data data;          String name;          public DataThread(Data data,String name){                    this.data = data;                    this.name = name;                    start();          }                     public void run(){                    try{                             for(int i = 0;i < 10;i++){                                      System.out.println(name + ":" + data.n);                                      data.n--;                                      Thread.sleep(200);                             }                    }catch(Exception e){}          } }           , , :                     1:60 2:60 2:58 1:58 2:56 1:56 2:54 1:54 2:52 1:52 2:50 1:50 2:48 1:48 2:47 1:46 2:44 1:44 2:42 1:42           , 60 , , n 60, , , n , , 2 47 。           : 1 n , , n 2 , , 。           , , , , , , CPU , , 。 。           , , , 、 , 。 , , 。           , , , synchronized。          synchronized , , , ( ), , , , , , 。 , , , , ,  ( CPU , ), 。           synchronized :                    package syn2; /**  *   */ public class Data2 {          public int n;          public Data2(){                    n = 60;          }                   public synchronized void action(String name){                    System.out.println(name + ":" + n);                    n--;          } } package syn2; /**  *   */ public class TestMulThread2 {          public static void main(String[] args) {                    Data2 data = new Data2();                    Data2Thread d1 = new Data2Thread(data," 1");                    Data2Thread d2 = new Data2Thread(data," 2");          } } package syn2; /**  *   */ public class Data2Thread extends Thread {          Data2 data;          String name;          public Data2Thread(Data2 data,String name){                    this.data = data;                    this.name = name;                    start();          }                     public void run(){                    try{                             for(int i = 0;i < 10;i++){                                      data.action(name);                                      Thread.sleep(200);                             }                    }catch(Exception e){}          } }           , :                     1:60 2:59 2:58 1:57 2:56 1:55 2:54 1:53 2:52 1:51 2:50 1:49 1:48 2:47 2:46 1:45 2:44 1:43 2:42 1:41           , n n action, synchronized , Data2 , action , , 。 synchronized , , , , 。 , —— 。           , , , , , , , , 。 , , , , , synchronized , , 。 :                   package syn3; /**  *   */ public class TestHuman {          public static void main(String[] args) {                    Toilet t = new Toilet(); //                    Human h1 = new Human("1",t);                    Human h2 = new Human("2",t);                    Human h3 = new Human("3",t);          } } package syn3; /**  *  ,  */ public class Human extends Thread {          Toilet t;          String name;          public Human(String name,Toilet t){                    this.name = name;                    this.t = t;                    start(); //          }                   public void run(){                    //                    t.enter(name);          } } package syn3; /**  *  ,  */ public class Toilet {          public synchronized void enter(String name){                    System.out.println(name + " !");                    try{                             Thread.sleep(2000);                    }catch(Exception e){}                    System.out.println(name + " !");          } }           , :                    1 ! 1 ! 3 ! 3 ! 2 ! 2 !           ,Toilet ,Human , ,TestHuman , 。 TestHuman , Toilet t, , Toilet , 。 Human , name , 3 , , t enter , , enter , , 2 , , , enter 。           , Toilet t enter synchronized , , enter , t , enter , Human , t, enter , —— 。 12.4.2            , , , 。           。 , , , , , , , 。 , , , , , , 。 , 。             , “ - ” , , , 。 —— 。           : , , , , , , 。 , , , , , CPU , 。 :                    package syn4; /**  *   */ public class TestAccount {          public static void main(String[] args) {                    Accout a = new Accout();                    StudentThread s = new StudentThread(a);                    GenearchThread g = new GenearchThread(a);          } } package syn4; /**  *   */ public class StudentThread extends Thread {          Accout a;          public StudentThread(Accout a){                    this.a = a;                    start();          }          public void run(){                    try{                             while(true){                                      Thread.sleep(2000);                                      a.getMoney(); //                             }                    }catch(Exception e){}          } } package syn4; /**  *   */ public class GenearchThread extends Thread {          Accout a;          public GenearchThread(Accout a){                    this.a = a;                    start();          }          public void run(){                    try{                             while(true){                                      Thread.sleep(12000);                                      a.saveMoney(); //                             }                    }catch(Exception e){}          } } package syn4; /**  *   */ public class Accout {          int money = 0;          /**           *            *  ,           */          public synchronized void getMoney(){                    System.out.println(" !");                    try{                             if(money == 0){                                      wait(); //                             }                             //                             System.out.println(" :" + money);                             money -= 50;                             //                             notify();                    }catch(Exception e){}                          }                   /**           *            *  , 200           */          public synchronized void saveMoney(){                    System.out.println(" !");                    try{                             if(money != 0){                                      wait(); //                             }                             //                             money = 200;                             System.out.println(" :" + money);                             //                             notify();                    }catch(Exception e){}                          } }           :                     ! ! :200 :200 ! :150 ! :100 ! :50 ! ! :200 :200 ! :150 ! :100 ! :50 !           ,TestAccount , Account , StudentThread GenearchThread。 StudentThread , 2 , 50 。 GenearchThread , 12 , 200。 , 。 , Account 。           Account , , money, 。 , ——wait notify , Object , , , Java 。 synchronized 。 wait , CPU , , , CPU ,notify , , 。 Account , StudentThread getMoney 0, StudentThread , 0, 50 , , GenearchThread saveMoney 0, GenearchThread , 0, 200 , 。           , : , , “ ” “ ”, run , 2 , a getMoney , a money 0, 。 , run , 12 , a saveMoney , a money , , 200 , , a , , , 50 , , , 。 , 12 , 4 , 4X2 =8 , , 0 , 12 , 0, 200 , 。           , , , 。 , 2 , , , , , , 。 , , , , 。 , ( , ), 。 12.4.3            , , , 。           —— 。 。 , , , : , : 。 , , 。 , , , 。           , , , , 。           , , , , , , , , 。           , , 。 12.4.4            , “XXX ”, ?           , 。 CPU , 。 , , , 。           Thread , , : l MAX_PRIORITY—— l NORM_PRIORITY—— , l MIN_PRIORITY—— , , NORM_PRIORITY, , Thread setPriority , :          public final void setPriority(int newPriority) t , t , :          t. setPriority(Thread. MAX_PRIORITY); , , 。 , , SecurityException 。 , :          package priority; /**  *   */ public class TestPriority {          public static void main(String[] args) {                    PrintNumberThread p1 = new PrintNumberThread(" ");                    PrintNumberThread p2 = new PrintNumberThread(" ");                    PrintNumberThread p3 = new PrintNumberThread(" ");                    p1.setPriority(Thread.MAX_PRIORITY);                    p2.setPriority(Thread.NORM_PRIORITY);                    p3.setPriority(Thread.MIN_PRIORITY);                    p1.start();                    p2.start();                    p3.start();          } } package priority; /**  *   */ public class PrintNumberThread extends Thread {          String name;          public PrintNumberThread(String name){                    this.name = name;          }          public void run(){                    try{                             for(int i = 0;i < 10;i++){                                      System.out.println(name + ":" + i);                             }                    }catch(Exception e){}          } } : :0 :1 :2 :0 :3 :1 :4 :2 :5 :6 :7 :8 :9 :3 :4 :5 :6 :7 :8 :9 :0 :1 :2 :3 :4 :5 :6 :7 :8 :9           ,PrintNumberThread , , TestPriority PrintNumberThread , 、 , 。 , , , 。           , , , 。 12.5            , 、 , , , 。           , 。   12.6           1、 3 ,          2、 3 , 1-10000 ( )          3、 1、 2 , , , quit exit 。          4、 , , , , 。          5、 : , 。

좋은 웹페이지 즐겨찾기