synchronized同步语句块
public class ObjectService { public void serviceMethod() {
try {
synchronized (this) {
System.out.println("begin time = " + System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("end time = " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadA extends Thread { private ObjectService service;
public ThreadA(ObjectService service) {
this.service = service;
}
@Override
public void run() {
super.run();
service.serviceMethod();
}
}
public class ThreadB extends Thread { private ObjectService service;
public ThreadB(ObjectService service) {
this.service = service;
}
@Override
public void run() {
super.run();
service.serviceMethod();
}
}
public class Run { public static void main(String[] args) {
ObjectService service = new ObjectService();
ThreadA aThread = new ThreadA(service);
ThreadB bThread = new ThreadB(service);
aThread.setName("a");
bThread.setName("b");
aThread.start();
bThread.start();
}
}
一半异步,一半同步
不在synchronized块中就是异步执行,在synchronized块中就是同步执行。
public class Task { public void doLongTimeTask() {
for (int i = 0; i < 100; i++) {
System.out.println("no synchronized thread name = " + Thread.currentThread().getName() + " i = " + (i + 1));
}
System.out.println();
synchronized (this) {
for (int i = 0; i < 100; i++) {
System.out.println("synchronized thread name = " + Thread.currentThread().getName() + " i = " + (i + 1));
}
}
}
}
synchronized代码块间的同步性
在使用synchronized(this)代码块需要注意的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对同一个object中所有其他synchronized(this)同步代码块的访问将被阻塞,这说明synchronized使用的“对象监视器”是一个。
public class ObjectService { public void serviceMethodA() {
try {
synchronized (this) {
System.out.println("A begin time = " + System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("A end time = " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void serviceMethodB() {
synchronized (this) {
System.out.println("B begin time = " + System.currentTimeMillis());
System.out.println("B end time = " + System.currentTimeMillis());
}
}
}
public class ThreadA extends Thread { private ObjectService service;
public ThreadA(ObjectService service) {
this.service = service;
}
@Override
public void run() {
super.run();
service.serviceMethodA();
}
}
public class ThreadB extends Thread { private ObjectService service;
public ThreadB(ObjectService service) {
this.service = service;
}
@Override
public void run() {
super.run();
service.serviceMethodB();
}
}
public class Run { public static void main(String[] args) {
ObjectService service = new ObjectService();
ThreadA aThread = new ThreadA(service);
ThreadB bThread = new ThreadB(service);
aThread.setName("a");
aThread.start();
bThread.setName("b");
bThread.start();
}
}
synchronized(this)代码块和synchronized方法一样,是锁定当前对象的。
将任意对象作为对象监视器
多个线程调用同一个对象中的不同名称的synchronized同步方法或 synchronized(this)同步代码块时,调用的效果是按顺序执行的,也就是同步的,阻塞的。
1).在多个线程持有“对象监视器”为同一个对象的前提下,同一时间只有一个线程可以执行 synchronized(非this对象x)同步代码块中的代码。
2).当持有“对象监视器”为同一对象的前提下,同一时间只有一个线程可以执行synchronized(非this对象x)同步代码块中的代码。
以上是 synchronized同步语句块 的全部内容, 来源链接: utcz.com/z/513515.html