特性1. synchronized(非this对象x)同步代码块格式进行同步操作时,对象监视器必须为同一个,如果不是同一个,运行结果就是异步调用了,就会交叉运行;
示例1:
package thread;
/**
* @Auther: 13213
* @Date: 2020/9/25 17:24
* @Description:
*/
public class Service {
private String usernameParam;
private String passwordParam;
private String anyString = new String();
public void setUsernamePassword(String username, String password){
try {
synchronized (anyString){
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "进入同步块");
usernameParam = username;
Thread.sleep(3000);
passwordParam = password;
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "离开同步块");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package thread;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:06
* @Description:
*/
public class ThreadA extends Thread{
private Service service;
public ThreadA(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.setUsernamePassword("a","aa");
}
}
package thread;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:06
* @Description:
*/
public class ThreadB extends Thread{
private Service service;
public ThreadB(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.setUsernamePassword("b","bb");
}
}
package thread;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:10
* @Description:
*/
public class Run {
public static void main(String[] args) {
Service service = new Service();
ThreadA threadA = new ThreadA(service);
threadA.setName("A");
threadA.start();
ThreadB threadB = new ThreadB(service);
threadB.setName("B");
threadB.start();
}
}
结果:
service.java文件更改如下:
package thread;
/**
* @Auther: 13213
* @Date: 2020/9/25 17:24
* @Description:
*/
public class Service {
private String usernameParam;
private String passwordParam;
// private String anyString = new String();
public void setUsernamePassword(String username, String password){
try {
String anyString = new String();
synchronized (anyString){
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "进入同步块");
usernameParam = username;
Thread.sleep(3000);
passwordParam = password;
System.out.println("线程名称为:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis() + "离开同步块");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结果为:
示例2:
package thread.demo2;
/**
* @Auther: 13213
* @Date: 2020/9/25 17:24
* @Description:
*/
public class Service {
private String anyString = new String();
public void a(){
try {
synchronized (anyString){
System.out.println("a begin");
Thread.sleep(3000);
System.out.println("a end");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public void b(){
System.out.println("b begin");
System.out.println("b end");
}
}
package thread.demo2;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:06
* @Description:
*/
public class ThreadA extends Thread{
private Service service;
public ThreadA(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.a();
}
}
package thread.demo2;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:06
* @Description:
*/
public class ThreadB extends Thread{
private Service service;
public ThreadB(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.b();
}
}
package thread.demo2;
/**
* @Auther: 13213
* @Date: 2020/9/25 21:10
* @Description:
*/
public class Run {
public static void main(String[] args) {
Service service = new Service();
ThreadA threadA = new ThreadA(service);
threadA.setName("A");
threadA.start();
ThreadB threadB = new ThreadB(service);
threadB.setName("B");
threadB.start();
}
}