Include a plurality of threads on the synchronization mechanism and the effect of race synchronized code region of the same object, to verify this case, a plurality of threads synchronization, the synchronized plurality of objects
First clear, synchronized synchronization mechanism is based on the object, different objects that will not be affected, which is consistent with the practical application scenarios.
Multi-threaded multiple objects synchronized
public class ThreadDemo2 {
/**
* 竞态方法 method1
*
* */
public synchronized void method1(){
try{
System.out.println(Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" over");
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 普通方法 method2
*
* */
public void method2(){
System.out.println(Thread.currentThread().getName()+" normal method");
}
/**
* 竞态方法 method3
*
* */
public synchronized void method3(){
try{
System.out.println(Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" over");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
final ThreadDemo2 threadDemo1 = new ThreadDemo2();
final ThreadDemo2 threadDemo2 = new ThreadDemo2();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
threadDemo1.method1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
threadDemo2.method1();
}
},"t2");
t1.start();
t2.start();
}
}
operation result:
t1
t2
t2 over
t1 over
As can be seen the two threads execute different objects race method, if implemented would like to different objects in a multi-thread synchronization, it would need to lock the class level (or region plus modified static race), the performance of severely ruthless poor, no matter who is the equivalent of calling this class no matter where objects are required to obtain permission to lock. I think the price increase this level of lock man is either drunk, or is too timid.
race or synchronized synchronization code block size to maintain a good
public class ThreadDemo3 {
/**
* method1
*
* */
public void method1(){
try{
//TODO 这里是公共区域 不具有竞争属性 可以做很多准备工作
System.out.println(Thread.currentThread().getName()+"---out the door");
synchronized(this) {//上锁 关门
//TODO 这里是竞态区域 需要获取锁才能进来 同样是锁的是对象
System.out.println(Thread.currentThread().getName()+"---in the door");
Thread.sleep(5000);
}
System.out.println(Thread.currentThread().getName()+" over");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
final ThreadDemo3 threadDemo1 = new ThreadDemo3();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
threadDemo1.method1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
threadDemo1.method1();
}
},"t2");
t1.start();
t2.start();
}
}
operation result
t1---out the door
t1---in the door
t2---out the door
t2---in the door
t1 over
t2 over
As can be seen from the results, although t1 and t2 have entered method1 but to wait outside the door t1 t2 in finishing things inside the door to get in, and on the previous synchronization method, the particle size is smaller, can be handled in public areas in front of the door logic
synchronized Although easy to use but also pay attention to size, pay attention to the occasion!