晒太阳

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 晒太阳 阅读(352) | 评论 (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 晒太阳 阅读(96) | 评论 (0)编辑 收藏

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

http://miaoxiaodong78.blog.163.com

posted @ 2009-11-17 11:58 晒太阳 阅读(50) | 评论 (0)编辑 收藏

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

导航

统计

常用链接

留言簿

随笔档案

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜