今天看了一下多线程。
对synchronized这个东东是研究了一上午啊!
首先做一下总结:
对synchronized的使用主要在两方面
1,synchronized method()。线程安全,
2,synchronized(object )。“同步控制块”,不过我更喜欢“临界区”这个叫法。(操作系统里是这样写的,呵呵)!
synchronized method()。这个嘛,不用说是对类中的方法的锁定,在方法未执行完,但执有该方法线程时间片已到时,锁定该方法,其它线程将不能访问该方法,直到执有此方法的线程释放该方法,其它线程才能访问此方法。
synchronized(object )则从名字上可看出来是对对象的锁定。是对一个资源的锁定。也即:当一个线程在使用一个对象资源时,其它线程等待,或者说处于阻塞状态,只有该线程释放对(object)的锁定时,才再一次激活。
下面是我自己的测试例子:
结果如下:
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):
结果如下:
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类;
我们再看看结果:
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
对synchronized这个东东是研究了一上午啊!
首先做一下总结:
对synchronized的使用主要在两方面
1,synchronized method()。线程安全,
2,synchronized(object )。“同步控制块”,不过我更喜欢“临界区”这个叫法。(操作系统里是这样写的,呵呵)!
synchronized method()。这个嘛,不用说是对类中的方法的锁定,在方法未执行完,但执有该方法线程时间片已到时,锁定该方法,其它线程将不能访问该方法,直到执有此方法的线程释放该方法,其它线程才能访问此方法。
synchronized(object )则从名字上可看出来是对对象的锁定。是对一个资源的锁定。也即:当一个线程在使用一个对象资源时,其它线程等待,或者说处于阻塞状态,只有该线程释放对(object)的锁定时,才再一次激活。
下面是我自己的测试例子:
java 代码
- package synchronizedTest;
- public class Student {
- private int age;
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void test(int age){
- try {
- this.setAge(age);
- System.out.println(Thread.currentThread().getName()+" Setage:"+age);
- Thread.sleep(500);
- System.out.println(Thread.currentThread().getName()+" Getage:"+this.getAge());
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
java 代码
- package synchronizedTest;
- import java.util.Random;
- public class Test extends Thread {
- private Student student;
- public Test(){
- this.student=new Student();
- }
- public Test(Student student){
- this.student=student;
- }
- public void run(){
- System.out.println(this.getName()+" start...");
- for(int i=0;i<5;i++){
- Random ran=new Random();
- int age=ran.nextInt(60);
- student.test(age);
- }
- System.out.println(this.getName()+" end...");
- }
- }
java 代码
- package synchronizedTest;
- public class Demo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Student student=new Student();
- Test t1=new Test(student);
- Test t2=new Test(student);
- t1.setName("A");
- t2.setName("B");
- t1.start();
- t2.start();
- }
- }
结果如下:
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 代码
- public synchronized void test(int age){
- try {
- this.setAge(age);
- System.out.println(Thread.currentThread().getName()+" Setage:"+age);
- Thread.sleep(500);
- System.out.println(Thread.currentThread().getName()+" Getage:"+this.getAge());
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
结果如下:
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 代码
- import java.util.Random;
- public class StudentThread extends Thread {
- private Student student;
- public StudentThread(Student student){
- this.student=student;
- }
- public StudentThread(){
- this.student=new Student();
- }
- public void setStudentAge(int age){
- this.student.setAge(age);
- System.out.println(this.getName()+" set Studnet age:"+this.student.getAge());
- }
- public void printAge(){
- System.out.println(this.getName()+" get Student age:"+this.student.getAge());
- }
- public void run(){
- System.out.println(this.getName()+" start...");
- test();
- }
- public synchronized void test(){
- try{
- for(int i=0;i<5;i++){
- Random ran=new Random();
- int age=ran.nextInt(50);
- synchronized(student){
- this.setStudentAge(age);
- this.sleep(500);
- this.printAge();
- }
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
java 代码
- public class ThreadDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Student student=new Student();
- StudentThread thread1=new StudentThread(student);
- StudentThread thread2=new StudentThread(student);
- thread1.setName("a");
- thread2.setName("b");
- thread1.start();
- thread2.start();
- }
- }
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