曬太陽

          2012年11月23日

          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)編輯 收藏

          2012年11月1日

          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 曬太陽 閱讀(108) | 評論 (0)編輯 收藏

          2009年9月23日

          關(guān)于instanceof的用法

          先看兩個小程序:
          1、
          1public class IntegerTypeTest {
          2    public static void main(String[] args){
          3        String str = "abc";
          4        boolean myBoolean = (str instanceof Integer); //compile time error
          5        System.out.println(myBoolean);
          6    }

          7}
          2、
           1     import java.util.*;
          public class InstanceOfDemo {
           2
           3    public static void main(String[] args) {
           4        System.out.println(new InstanceOfDemo() instanceof String); //compile time error
           5        System.out.println(new InstanceOfDemo() instanceof Exception); //compile time error
           6        System.out.println(new InstanceOfDemo() instanceof Object); //compilation and output true
           7        
           8        System.out.println(new InstanceOfDemo() instanceof List); //compilation and output false
           9    }

          10}

          11
          這兩個程序的編譯結(jié)果和我們預(yù)想的不一樣,第一個程序編譯結(jié)果是:

          第二個程序編譯結(jié)果是:

          產(chǎn)生這種錯誤的原因在于,instanceof運算符第一個操作數(shù)的類型應(yīng)該是第二個操作數(shù)的父類、子類或者與第二個操作數(shù)的類型相同。否則編譯會出錯。

          posted @ 2009-09-23 08:39 曬太陽 閱讀(332) | 評論 (0)編輯 收藏

          僅列出標題  下一頁
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 扎赉特旗| 志丹县| 和林格尔县| 原阳县| 丹江口市| 彰化市| 永兴县| 永平县| 曲沃县| 北宁市| 枣阳市| 孝感市| 峡江县| 金华市| 建德市| 仙居县| 牙克石市| 耒阳市| 新安县| 集安市| 台中市| 儋州市| 龙门县| 萝北县| 色达县| 彰武县| 荣昌县| 佳木斯市| 革吉县| 鄂伦春自治旗| 安庆市| 大足县| 海淀区| 绍兴县| 上林县| 古蔺县| 崇州市| 屯昌县| 石棉县| 泸西县| 高尔夫|