对synchronized的一点认识

今天看了一下多线程。
对synchronized这个东东是研究了一上午啊!
首先做一下总结:
对synchronized的使用主要在两方面 
1,synchronized method()。线程安全,
2,synchronized(object )。“同步控制块”,不过我更喜欢“临界区”这个叫法。(操作系统里是这样写的,呵呵)!

synchronized method()。这个嘛,不用说是对类中的方法的锁定,在方法未执行完,但执有该方法线程时间片已到时,锁定该方法,其它线程将不能访问该方法,直到执有此方法的线程释放该方法,其它线程才能访问此方法。

synchronized(object )则从名字上可看出来是对对象的锁定。是对一个资源的锁定。也即:当一个线程在使用一个对象资源时,其它线程等待,或者说处于阻塞状态,只有该线程释放对(object)的锁定时,才再一次激活。
下面是我自己的测试例子:
java 代码
 
  1. package synchronizedTest;  
  2.   
  3. public class Student {  
  4.   
  5.     private int age;  
  6.   
  7.     public int getAge() {  
  8.         return age;  
  9.     }  
  10.   
  11.     public void setAge(int age) {  
  12.         this.age = age;  
  13.     }  
  14.       
  15.     public  void test(int age){  
  16.         try {         
  17.             this.setAge(age);  
  18.             System.out.println(Thread.currentThread().getName()+" Setage:"+age);  
  19.             Thread.sleep(500);  
  20.             System.out.println(Thread.currentThread().getName()+" Getage:"+this.getAge());  
  21.         } catch (InterruptedException e) {        
  22.             e.printStackTrace();  
  23.         }         
  24.     }     
  25. }  

java 代码
 
  1. package synchronizedTest;  
  2.   
  3. import java.util.Random;  
  4.   
  5. public class Test extends Thread {  
  6.   
  7.     private Student student;  
  8.     public Test(){  
  9.         this.student=new Student();  
  10.     }  
  11.       
  12.     public Test(Student student){  
  13.         this.student=student;  
  14.     }  
  15.       
  16.     public void run(){  
  17.         System.out.println(this.getName()+" start...");  
  18.         for(int i=0;i<5;i++){  
  19.             Random ran=new Random();  
  20.             int age=ran.nextInt(60);  
  21.             student.test(age);  
  22.         }  
  23.         System.out.println(this.getName()+" end...");  
  24.     }  
  25. }  
java 代码
 
  1. package synchronizedTest;  
  2.   
  3. public class Demo {  
  4.   
  5.     /** 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         Student student=new Student();  
  11.         Test t1=new Test(student);  
  12.         Test t2=new Test(student);  
  13.         t1.setName("A");  
  14.         t2.setName("B");  
  15.         t1.start();  
  16.         t2.start();  
  17.     }  
  18.   
  19. }  

结果如下:
A start...
A Setage:23
B start...
B Setage:53
A Getage:53
A Setage:56
B Getage:56
B Setage:52
A Getage:52
A Setage:2
B Getage:2
B Setage:19
A Getage:19
A Setage:47
B Getage:47
B Setage:2
A Getage:2
A Setage:19
B Getage:19
B Setage:3
B Getage:3
B end...
A Getage:3
A end...
明显上面的例子中发了资源冲突,下面我们对Student类的Test方法加一个线程同步( synchronized):
java 代码
 
  1. public synchronized void test(int age){  
  2.         try {         
  3.             this.setAge(age);  
  4.             System.out.println(Thread.currentThread().getName()+" Setage:"+age);  
  5.             Thread.sleep(500);  
  6.             System.out.println(Thread.currentThread().getName()+" Getage:"+this.getAge());  
  7.         } catch (InterruptedException e) {        
  8.             e.printStackTrace();  
  9.         }         
  10.     }     

结果如下:
A start...
A Setage:46
B start...
A Getage:46
B Setage:48
B Getage:48
A Setage:56
A Getage:56
B Setage:47
B Getage:47
A Setage:5
A Getage:5
B Setage:2
B Getage:2
A Setage:58
A Getage:58
B Setage:59
B Getage:59
A Setage:0
A Getage:0
A end...
B Setage:17
B Getage:17
B end...
看到我们想要结果了!

我们再继续讨论    synchronized(object)依然用上面的Student类;
java 代码
 
  1. import java.util.Random;  
  2.   
  3. public class StudentThread extends Thread {  
  4.   
  5.     private Student student;  
  6.       
  7.     public StudentThread(Student student){  
  8.         this.student=student;     
  9.     }  
  10.     public  StudentThread(){  
  11.         this.student=new Student();       
  12.     }  
  13.       
  14.     public void setStudentAge(int age){  
  15.         this.student.setAge(age);  
  16.         System.out.println(this.getName()+" set Studnet age:"+this.student.getAge());  
  17.     }  
  18.       
  19.     public void printAge(){  
  20.         System.out.println(this.getName()+" get Student age:"+this.student.getAge());  
  21.     }  
  22.       
  23.       
  24.      public void  run(){  
  25.         System.out.println(this.getName()+" start...");  
  26.          test();  
  27.     }  
  28.        
  29.      public synchronized void test(){  
  30.          try{  
  31.                 for(int i=0;i<5;i++){  
  32.           
  33.                     Random ran=new Random();  
  34.                     int age=ran.nextInt(50);  
  35.                     synchronized(student){  
  36.                         this.setStudentAge(age);  
  37.                         this.sleep(500);  
  38.                         this.printAge();                  
  39.                     }  
  40.                 }  
  41.             }  
  42.             catch(Exception e){  
  43.                 e.printStackTrace();  
  44.             }  
  45.      }  
  46. }  
java 代码
 
  1. public class ThreadDemo {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         Student student=new Student();  
  9.       
  10.         StudentThread thread1=new StudentThread(student);  
  11.         StudentThread thread2=new StudentThread(student);  
  12.           
  13.   
  14.         thread1.setName("a");  
  15.         thread2.setName("b");  
  16.           
  17.         thread1.start();          
  18.         thread2.start();  
  19.     }  
  20. }  
我们再看看结果:
a start...
a set Studnet age:13
b start...
a get Student age:13
b set Studnet age:7
b get Student age:7
a set Studnet age:11
a get Student age:11
b set Studnet age:22
b get Student age:22
a set Studnet age:43
a get Student age:43
b set Studnet age:49
b get Student age:49
a set Studnet age:49
a get Student age:49
b set Studnet age:46
b get Student age:46
a set Studnet age:36
a get Student age:36
b set Studnet age:1
b get Student age:1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值