synchronized
synchronized是一个同步锁关键字,作用就是让线程只能逐一执行,不能并行执行,至于底层怎么实现,不是我这种连用都不会的人改关注的。
用法
– 修饰代码快(非静态)
测试代码(不加synchronized):
public class Synchronized {
public static void main (String[] args) {
Teacher teacher = new Teacher();
Thread first = new SynchronizedThread("first", teacher);
Thread second = new SynchronizedThread("second", teacher);
first.start();
second.start();
}
static class SynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public SynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
teacher.say(name);
}
}
static class Teacher {
public void say(String name) {
System.out.println("我正在教" + name);
//System.out.println("线程进入睡眠");
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
}
执行结果:
我正在教first
我正在教second
second睡眠三秒
first睡眠三秒
两个睡眠三秒几乎是同时出现。
测试代码(加synchronized):
static class Teacher {
public void say(String name) {
System.out.println("我正在教" + name);
synchronized (this) {
//System.out.println("线程进入睡眠");
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
}
输出结果:
我正在教first
我正在教second
first睡眠三秒
second睡眠三秒
虽然看着是一样的,实际输出的时候,是先输出一个睡眠三秒,3秒之后,再输出一个睡眠三秒。
总结:在多个线程操作同一个对象,执行加锁的对象方法的时候,逐一执行同步代码块中的代码,锁的是对象实例
– 修饰对象方法
测试代码(加synchronized):
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
执行结果:
我正在教first
first睡眠三秒
我正在教second
second睡眠三秒
可以看到输出结果是先是一个线程(first)执行了方法,然后是另外一个线程(second)执行了方法。
测试代码(不加synchronized):
static class Teacher {
public void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
执行结果:
我正在教first
我正在教second
second睡眠三秒
first睡眠三秒
可以看到两个线程几乎同时执行了,这和代码块的测试是一样的结果。
测试代码(一个方法不加synchronized,一个方法加synchronized):
public class Synchronized {
public static void main (String[] args) {
Teacher teacher = new Teacher();
Thread first = new SynchronizedThread("first", teacher);
Thread second = new AnotherSynchronizedThread("second", teacher);
first.start();
second.start();
}
static class SynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public SynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
teacher.hello(name);
}
}
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
public void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
}
执行结果:
我正在教first
我会说hello
hello second
first睡眠三秒
可以看到,多线线程执行同一个对象的同步方法和非同步方法的时候,两个方法的执行相互不影响。
测试代码(一个方法加synchronized,另外一个方法也加synchronized):
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
public synchronized void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
执行结果:
我正在教first
first睡眠三秒
我会说hello
hello second
虽然结果不能明显看出输出顺序,但是执行时我能看到是先执行一个方法,执行完之后再执行另外一个方法。
测试代码(一个静态方法不加synchronized,一个对象方法加synchronized):
public class Synchronized {
public static void main (String[] args) {
Teacher teacher = new Teacher();
Thread first = new SynchronizedThread("first", teacher);
Thread second = new AnotherSynchronizedThread("second", teacher);
first.start();
second.start();
}
static class SynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public SynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
Teacher.hello(name);
}
}
static class Teacher {
public synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
public static void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
}
执行结果:
我正在教first
我会说hello
hello second
first睡眠三秒
从结果上可以看到,对象方法上加同步对静态方法没有影响。
另外静态方法加上了synchronized,两个方法的执行之间也不影响。
总结:1、首先静态方法加不加同步,对实例方法没什么影响;
2、加同步的实例方法,对不加同步的实例方法没什么影响;
3、只有都是实例方法并且都加同步的情况下,才会出现同步逐一执行的效果;
静态方法
– 静态方法
上面已经知道了对象方法加同步,对静态方法没有影响。
测试代码(两个静态方法加synchronized):
static class Teacher {
public static synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
public static synchronized void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
执行结果:
我正在教first
first睡眠三秒
我会说hello
hello second
可以看到,是先执行了一个方法,再执行另外一个方法。
测试代码(一个静态方法加synchronized,一个静态方法不加synchronized):
static class Teacher {
public static synchronized void say(String name) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
public static void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
执行结果:
我正在教first
我会说hello
hello second
first睡眠三秒
两个方法的执行没有影响。
总结:静态方法方法加同步只对加了XM代理申请www.fx61.com/brokerlist/xm.html同步的静态方法有影响。
以上都是常规的写法,实例方法给对象实例枷锁,静态方法给类枷锁。还有一些其它的写法。
其它写法
测试代码(在实例方法上加synchronized,加锁的是类):
static class Teacher {
public static void say(String name) {
synchronized(Teacher.class) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
public static synchronized void hello(String name) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
执行结果:
我正在教first
first睡眠三秒
我会说hello
hello second
执行效果,类似与两个加了同步的静态方法,这种写法是给类加锁,所有需要同步类的方法都会受影响。
测试代码(定义一个类变量(是一个对象),用来同步):
public class Synchronized {
private static Test lock = new Test();
public static void main (String[] args) {
Teacher teacher = new Teacher();
Thread first = new SynchronizedThread("first", teacher);
Thread second = new AnotherSynchronizedThread("second", teacher);
first.start();
second.start();
}
static class SynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public SynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
teacher.say(name);
}
}
static class AnotherSynchronizedThread extends Thread {
private String name;
private Teacher teacher;
public AnotherSynchronizedThread (String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;}
@Override
br/>}
@Override
Teacher.hello(name);
}
}
static class Teacher {
public void say(String name) {
synchronized(lock) {
System.out.println("我正在教" + name);
SleepUtil.sleep(3000);
System.out.println(name + "睡眠三秒");
}
}
public static void hello(String name) {
synchronized(lock) {
System.out.println("我会说hello");
System.out.println("hello " + name);
}
}
}
static class Test {
}
}
执行结果:
我正在教first
first睡眠三秒
我会说hello
hello second
这种写法等效于类加同步,或者实例加同步,不过同步的对象变为了自己定义的另外一个对象。
synchronized同步:
同步实例对象时,只对使用这个对象锁的方法有影响;
同步类时,只对使用这个类锁的方法有影响;
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。