Sitemap" content="www.duozhankeji.com">
全國免費熱線:
>Java並發編程之同步互斥問題
作者:中国IT实验室 点击:11372次 日期:2012-03-12
字號::T | T
欢迎进入Java社区论坛,与200万技术人员互动交流 >>進入
  1. do{  
  2.  flag[i]=true;  
  3.  while(flag[j]);  
  4.  临界区;  
  5.  flag[i]=false;  
  6.  剩余区;  
  7. }while(1

Java實現代碼如下:

  1. package mutiple_thread;  
  2.  
  3. public class OS_SYN_A2{  
  4.     public static  int flag[]=new int [3];  
  5.     public static int cnt=0;  
  6.     public static void main(String args[]){  
  7.         class proo implements Runnable{  
  8.             public proo(){  
  9.                   
  10.             }  
  11.             @Override 
  12.             public void run() {  
  13.                 // TODO Auto-generated method stub  
  14.                 while(true){  
  15.                     flag[1]=1;  
  16.                     while(flag[2]==1){  
  17.                           
  18.                     }  
  19.                     if(cnt==5){  
  20.                         flag[1]=0;  
  21.                     }else{  
  22.                         cnt++;  
  23.                         System.out.println("pro ++! now id"+cnt);  
  24.                         flag[1]=0;  
  25.                     }  
  26.                 }  
  27.             }  
  28.               
  29.         }  
  30.  
  31.         class conn implements Runnable{  
  32.  
  33.             @Override 
  34.             public void run() {  
  35.                 // TODO Auto-generated method stub  
  36.                 while(true){  
  37.                     flag[2]=1;  
  38.                     while(flag[1]==1){  
  39.                           
  40.                     }  
  41.                     //臨界區  
  42.                     if(cnt==0){  
  43.                         flag[2]=0;  
  44.                     }else{  
  45.                         cnt--;  
  46.                         System.out.println("con --! now id"+cnt);  
  47.                         //退出臨界區  
  48.                         flag[2]=0;  
  49.                     }  
  50.                 }  
  51.             }  
  52.         }  
  53.         new Thread(new proo()).start();  
  54.         new Thread(new conn()).start();  
  55.     }  
  56.       

這個算法的主要思路是通過設置flag來確定執行哪個線程,但是可能會造成饑餓,因此不行。

1.2 算法3

算法3通过共享两个变量 flag 和turn来实现同步。

  1. package mutiple_thread;  
  2.  
  3. public class OS_SYN_A3{  
  4.     public static  int flag[]=new int [3];  
  5.     public static int turn=0;  
  6.     public static int cnt=0;  
  7.     public static void main(String args[]){  
  8.         class proo implements Runnable{  
  9.             public proo(){  
  10.                   
  11.             }  
  12.             @Override 
  13.             public void run() {  
  14.                 // TODO Auto-generated method stub  
  15.                 while(true){  
  16.                     flag[1]=1;  
  17.                     turn=2;  
  18.                     while(flag[2]==1&&turn==2){  
  19.                           
  20.                     }  
  21.                     if(cnt==5){  
  22.                         flag[1]=0;  
  23.                     }else{  
  24.                         cnt++;  
  25.                         System.out.println("pro ++! now id"+cnt);  
  26.                         flag[1]=0;  
  27.                     }  
  28.                 }  
  29.             }  
  30.               
  31.         }  
  32.  
  33.         class conn implements Runnable{  
  34.  
  35.             @Override 
  36.             public void run() {  
  37.                 // TODO Auto-generated method stub  
  38.                 while(true){  
  39.                     flag[2]=1;  
  40.                     turn=1;  
  41.                     while(flag[1]==1&&turn==1){  
  42.                           
  43.                     }  
  44.                     //臨界區  
  45.                     if(cnt==0){  
  46.                         flag[2]=0;  
  47.                     }else{  
  48.                         cnt--;  
  49.                         System.out.println("con --! now id"+cnt);  
  50.                         //退出臨界區  
  51.                         flag[2]=0;  
  52.                     }  
  53.                 }  
  54.             }  
  55.         }  
  56.         new Thread(new proo()).start();  
  57.         new Thread(new conn()).start();  
  58.     }  
  59.       

這是一種正確的軟件實現方法。

2、經典同步問題的Java實現

2.1 读者写者问题

這裏實現的讀者優先的算法,使用了Java並發包的信號量來實現。

實現的僞代碼如下:

讀者進程:

  1. while1){  
  2.  wait(mutex)  
  3.    count++;  
  4.    if(readercount==1){  
  5.    wait(writer);   
  6.  }  
  7. signal(mutex);  
  8. do reading;  
  9. wait(mutex);  
  10. cnt--;  
  11. if(cnt==0){  
  12.   signal(writer);  
  13. }  
  14. signal(mutex);  
  15. }  
  16. }

算法通過共享writer和mutex兩個信號量,來處理同步問題

  1. package mutiple_thread;  
  2. import java.util.concurrent.Semaphore;  
  3. public class OS_Readerwriter{  
  4.     static Semaphore sem=new Semaphore(1);  
  5.     static Semaphore sem_wrt=new Semaphore(1);  
  6.     static int readercount=0;  
  7.     static String a="hahaha";  
  8.     public static void main(String args[]){  
  9.         class reader implements Runnable{  
  10.             public reader(){  
  11.                   
  12.             }  
  13.             @Override 
  14.             public void run() {  
  15.                 // TODO Auto-generated method stub  
  16.                 try {  
  17.                     sem.acquire();  
  18.                     readercount++;  
  19.                 } catch (InterruptedException e) {  
  20.                     // TODO Auto-generated catch block  
  21.                     e.printStackTrace();  
  22.                 }  
  23.                 if(readercount==1){  
  24.                     try {  
  25.                         sem_wrt.acquire();  
  26.                     } catch (InterruptedException e) {  
  27.                         // TODO Auto-generated catch block  
  28.                         e.printStackTrace();  
  29.                     }  
  30.                 }  
  31.                 sem.release();  
  32.                   
  33.                 System.out.println("Reading "+a);  
  34.                   
  35.                 try {  
  36.                     sem.acquire();  
  37.                 } catch (InterruptedException e) {  
  38.                     // TODO Auto-generated catch block  
  39.                     e.printStackTrace();  
  40.                 }  
  41.                 readercount--;  
  42.                 if(readercount==0){  
  43.                     sem_wrt.release();  
  44.                 }  
  45.                 sem.release();  
  46.             }  
  47.         }  
  48.           
  49.         class writer implements Runnable{  
  50.             public writer(){  
  51.                   
  52.             }  
  53.             @Override 
  54.             public void run() {  
  55.                 // TODO Auto-generated method stub  
  56.                 try {  
  57.                     sem_wrt.acquire();  
  58.                 } catch (InterruptedException e) {  
  59.                     // TODO Auto-generated catch block  
  60.                     e.printStackTrace();  
  61.                 }  
  62.                 a=a+"abc";  
  63.                 System.out.println("Writing "+a);  
  64.                 sem_wrt.release();  
  65.             }  
  66.               
  67.         }  
  68.         for(int i=1;i<=10;i++){  
  69.             new Thread(new writer()).start();  
  70.             new Thread(new reader()).start();  
  71.         }  
  72.           
  73.     }  

[1] [2] 下一頁

發表評論

昵稱 * 驗證碼 * 驗證碼
上一篇: 前台開發要求与测试
下一篇: >JAVA中LinkedList和ArrayList類型的區別

資質證書

CMMI Ⅲ APPRAISAL ID:30062
ISO9001:08915Q20090ROS
ISO27001:04817I20037R0S
高新技術企業:GR201753000141
網站问题免费诊断

电子商务三位一体發展戰略

技術研發

業務培训

實戰運營

戰略布局