• 让天下没有难学的技术
    多数学员都来自推荐,这就是口碑的力量

synchronized同步代码块示例

synchronized同步代码块示例
修改Task.java中的doLongTimeTask()如下:

public void doLongTimeTask() {
try {
System.out.println(“begin task”);
Thread.sleep(3000);
//            getData1 = “长时间处理任务后从远程返回的值1 threadName=” + Thread.currentThread().getName();
//            getData2 = “长时间处理任务后从远程返回的值2 threadName=” + Thread.currentThread().getName();
String privateGetData1 = “长时间处理任务后从远程返回的值1 threadName=” + Thread.currentThread().getName();
String privateGetData2 = “长时间处理任务后从远程返回的值2 threadName=” + Thread.currentThread().getName();
synchronized (this) {
getData1 = privateGetData1;
getData2 = privateGetData2;
}
System.out.println(getData1);
System.out.println(getData2);
System.out.println(“end task”);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

运行结果:

begin task
begin task
长时间处理任务后从远程返回的值1 threadName=Thread-0
长时间处理任务后从远程返回的值2 threadName=Thread-0
end task
长时间处理任务后从远程返回的值1 threadName=Thread-1
长时间处理任务后从远程返回的值2 threadName=Thread-1
end task
耗时:3

synchronized(object)代码块间使用

MyObject.java

public class MyObject {
}

SynObjThread1.java

public class SynObjThread1 extends Thread {

private TestSynObject testSynObject;
private MyObject object;

public SynObjThread1(TestSynObject testSynObject, MyObject object) {
super();
this.testSynObject = testSynObject;
this.object = object;
}

@Override
public void run() {
super.run();
testSynObject.testMethod(object);
}
}

SynObjThread2.java

public class SynObjThread2 extends Thread{

private TestSynObject testSynObject;
private MyObject object;

public SynObjThread2(TestSynObject testSynObject, MyObject object) {
super();
this.testSynObject = testSynObject;
this.object = object;
}

@Override
public void run() {
super.run();
testSynObject.testMethod(object);
}
}

TestSynObject.java

public class TestSynObject {

public void testMethod(MyObject object) {

synchronized (object) {
try {
System.out.println(“testMethod ____getLock time=” + System.currentTimeMillis() + ” run ThreadName=” + Thread.currentThread().getName());
Thread.sleep(2000);
System.out.println(“testMethod releaseLock time=” + System.currentTimeMillis() + ” run ThreadName=” + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
TestSynObject testSynObject = new TestSynObject();
MyObject object = new MyObject();
SynObjThread1 synObjThread1 = new SynObjThread1(testSynObject, object);
synObjThread1.setName(“a”);
synObjThread1.start();
SynObjThread2 synObjThread2 = new SynObjThread2(testSynObject, object);
synObjThread2.setName(“b”);
synObjThread2.start();
}
}

运行结果:

testMethod ____getLock time=1547021705247 run ThreadName=a
testMethod releaseLock time=1547021707248 run ThreadName=a
testMethod ____getLock time=1547021707248 run ThreadName=b
testMethod releaseLock time=1547021709248 run ThreadName=b

两个线程使用了同一个对象监视器object,所以运行结果是同步的。那么,如果使用不同的对象监视器会出现什么效果呢?

修改TestSynObject.java中的main()如下:

运行结果:

testMethod ____getLock time=1547023045744 run ThreadName=a
testMethod ____getLock time=1547023045744 run ThreadName=b
testMethod releaseLock time=1547023047745 run ThreadName=a
testMethod releaseLock time=1547023047745 run ThreadName=b

两个线程使用了不同的对象,所以运行结果不同步。

当一个对象访问synchronized(this)代码块时,其他线程对同一个对象中所有其他synchronized(this)代码块代码块的访问将被阻塞,这说明synchronized(this)代码块使用的“对象监视器”是一个。也就是说和synchronized方法一样,synchronized(this)代码块也是锁定当前对象的。

其他线程执行对象中synchronized同步方法和synchronized(this)代码块时呈现同步效果;

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注