曬太陽

          2009年11月17日

          java線程互鎖演示

           1 /**
           2  * 線程互鎖:線程td1得到obj1的鎖,線程td2得到了obj2的鎖,接著,線程td1在持有obj1的鎖的同時
           3  * 企圖得到obj2的鎖,而此時線程td2仍持有obj2的鎖,所以線程td1等待,而線程td2的行為也與線程td1相同
           4  * 因此就形成了死鎖
           5  */
           6 public class Client {
           7     public static void main(String[] args) {
           8         final Object obj1 = "test1";
           9         final Object obj2 = "test2";
          10         
          11         Thread td1 = new Thread(){
          12             public void run(){
          13                 synchronized(obj1){
          14                     System.out.println("td1得到了obj1的鎖");
          15                     try {
          16                         Thread.sleep(1000);
          17                     } catch (InterruptedException e) {
          18                         e.printStackTrace();
          19                     }
          20                     
          21                     synchronized(obj2){
          22                         System.out.println("td1企圖得到obj2的鎖");
          23                         
          24                         try {
          25                             Thread.sleep(1000);
          26                         } catch (InterruptedException e) {
          27                             e.printStackTrace();
          28                         }
          29                     }
          30                 }
          31             }
          32         };
          33         
          34 
          35         Thread td2 = new Thread(){
          36             public void run(){
          37                 synchronized(obj2){
          38                     System.out.println("td2得到了obj2的鎖");
          39                     try {
          40                         Thread.sleep(1000);
          41                     } catch (InterruptedException e) {
          42                         e.printStackTrace();
          43                     }
          44                     
          45                     synchronized(obj1){
          46                         System.out.println("td2企圖得到obj1的鎖");
          47                         
          48                         try {
          49                             Thread.sleep(1000);
          50                         } catch (InterruptedException e) {
          51                             e.printStackTrace();
          52                         }
          53                     }
          54                 }
          55             }
          56         };
          57         
          58         td1.start();
          59         td2.start();
          60     }
          61 
          62 }

          posted @ 2012-11-23 06:16 曬太陽 閱讀(364) | 評論 (0)編輯 收藏

          java 快速排序

           1 
           2 public class QuickSort {
           3     public static int partition(int[] arr, int low, int high){
           4         int pivot = arr[low];
           5         while(low<high){
           6             while(low<high && arr[high]>=pivot){
           7                 high--;
           8             }
           9             if(low<high){
          10                 arr[low] = arr[high];
          11                 low++;
          12             }
          13             
          14             while(low<high && arr[low]<=pivot){
          15                 low++;
          16             }
          17             if(low<high){
          18                 arr[high] = arr[low];
          19                 high--;
          20             }
          21         }
          22         arr[low] = pivot;
          23         return low;
          24     }
          25     
          26     public static void sort(int[] arr, int low, int high){
          27         int pivot;
          28         if(low<high){
          29             pivot = partition(arr,low,high);
          30             sort(arr,low,pivot-1);
          31             sort(arr,pivot+1,high);
          32         }
          33     }
          34     
          35     public static void quickSort(int[] arr){
          36         sort(arr,0,arr.length-1);
          37     }
          38     
          39     /*public static void main(String[] args) {
          40         int[] arr = {46,34,2,99,6,23,20,8};
          41         quickSort(arr);
          42         StringBuilder sb = new StringBuilder();
          43         for(int i=0;i<arr.length;i++){
          44             sb.append(arr[i]+",");
          45         }
          46         sb.deleteCharAt(sb.length()-1);
          47         System.out.println(sb);
          48     }*/    
          49 }
          50 

          posted @ 2012-11-01 12:06 曬太陽 閱讀(109) | 評論 (0)編輯 收藏

          一個非常好的java博客(master24)

          http://miaoxiaodong78.blog.163.com

          posted @ 2009-11-17 11:58 曬太陽 閱讀(54) | 評論 (0)編輯 收藏

          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海林市| 大化| 若尔盖县| 南宫市| 印江| 富民县| 财经| 宜春市| 曲水县| 哈巴河县| 常州市| 东至县| 岢岚县| 涡阳县| 萍乡市| 海安县| 临漳县| 禄丰县| 灵宝市| 合肥市| 芮城县| 延安市| 崇阳县| 手机| 县级市| 呼和浩特市| 全椒县| 乌拉特中旗| 潞城市| 璧山县| 江达县| 仙居县| 灌阳县| 离岛区| 无锡市| 青阳县| 慈利县| 克拉玛依市| 临猗县| 庆元县| 嘉定区|