Java中Synchronized关键字用来修饰一个方法或者一个代码块,以保证多线程的情况下,一次只能有一个线程来访问用Synchronized标记的方法或者代码块。
Synchronized方法不同用法代表增加的何种类型的锁:
对象锁:Synchronized void methodA() Synchronized (this){}(主要对class类生成的对象进行加锁)
类锁: Synchronized static void methodA() Synchronized (XXX.classs)(对整个类进行加锁)
私有锁:在类内部生命一个私有属性如:pricate Object lock,在需要加锁的代码段Synchronized(lock)(对私有属性加锁)
由以上锁的位置可以总结出:
类锁与对象锁不会产生竞争,对象锁与对象锁会产生竞争,对象锁与私有锁也不会产生竞争
推测锁竞争条件并验证:
前提条件:thread1,thread2并发访问同一个object对象(下文Synchronized标记代表对象锁,static Synchronized标记代表类锁)
case1:thread1,thread2访问同一个无Synchronized关键字标识的方法,两个线程可以并发的得到执行
case2:thread1,thread2访问同一个Synchronized关键字标识的方法,必须有一个线程先得到执行,另外一个线程等先执行的线程执行完毕才能得到执行
case3:thread1访问Synchronized标记的方法,thread2访问无Synchronized标记的方法,两个线程可以并发的得到执行
case4:thread1访问Synchronized标记的方法A,thread2访问Synchronized标记的方法B,两个线程必须有个线程先得到执行,另外一个线程等先执行的线程执行完毕才能得到执行。
case5:thread1访问Synchronized标记的方法A,thread2访问static Synchronized标记的方法B,两个方法可以并发的得到执行
case6:thread1访问static Synchronized标记的方法A,thread2访问static Synchronized标记的方法B,两个方法必须有个线程先得到执行,另外一个线程等先执行的线程执行完毕才能得到执行
验证:case1
public class TestSynchronized {
public void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test2:4
Thread_Test2:3
Thread_Test1:3
Thread_Test2:2
Thread_Test1:2
Thread_Test2:1
Thread_Test1:1
Thread_Test2:0
Thread_Test1:0
验证case2:
public class TestSynchronized {
public synchronized void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test1:3
Thread_Test1:2
Thread_Test1:1
Thread_Test1:0
Thread_Test2:4
Thread_Test2:3
Thread_Test2:2
Thread_Test2:1
Thread_Test2:0
验证case3:
public class TestSynchronized {
public synchronized void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test2();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test2:4
Thread_Test1:3
Thread_Test2:3
Thread_Test1:2
Thread_Test2:2
Thread_Test1:1
Thread_Test2:1
Thread_Test1:0
Thread_Test2:0
验证case4:
public class TestSynchronized {
public synchronized void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test2();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test1:3
Thread_Test1:2
Thread_Test1:1
Thread_Test1:0
Thread_Test2:4
Thread_Test2:3
Thread_Test2:2
Thread_Test2:1
Thread_Test2:0
验证case5:
public class TestSynchronized {
public synchronized void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test2();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test2:4
Thread_Test1:3
Thread_Test2:3
Thread_Test1:2
Thread_Test2:2
Thread_Test1:1
Thread_Test2:1
Thread_Test1:0
Thread_Test2:0
验证case6:
public class TestSynchronized {
public static synchronized void test1(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void test2(){
int i=5;
while(i-- >0){
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws InterruptedException {
TestSynchronized testSynchronized = new TestSynchronized();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test1();
}
},"Thread_Test1").start();
new Thread(new Runnable() {
@Override
public void run() {
testSynchronized.test2();
}
},"Thread_Test2").start();
}
结果:
Thread_Test1:4
Thread_Test1:3
Thread_Test1:2
Thread_Test1:1
Thread_Test1:0
Thread_Test2:4
Thread_Test2:3
Thread_Test2:2
Thread_Test2:1
Thread_Test2:0