java的八锁问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的八锁问题相关的知识,希望对你有一定的参考价值。


java的锁有对象锁和类锁两种,但不同的锁在不同的场景下是不同的,区别在于

1.对于普通同步方法,锁是当前实例对象(this)。
2.对于静态同步方法,锁是当前类的Class对象。
3.对于同步方法块,锁是Synchonized括号里配置的对象

讨论到java的八锁问题,我们一般讨论的是java锁住了类还是对象,或者或者是静态方法块

同步方法块之间的讨论

package day2.八锁问题;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class test
@Test
public void test1() throws InterruptedException
Eat eat=new Eat();
new Thread(()->
try
TimeUnit.SECONDS.sleep(1);
eat.eat_meat("1");

catch (InterruptedException e)
e.printStackTrace();

).start();

new Thread(()->
try

TimeUnit.SECONDS.sleep(1);
eat.eat_rice("2");

catch (Exception e)
e.printStackTrace();


).start();

Eat eat2=new Eat();

eat2.eat_meat("3");
eat2.eat_rice("4");
TimeUnit.SECONDS.sleep(10);





class Eat
synchronized void eat_rice(String name)
System.out.println("吃饭"+name);


synchronized void eat_meat(String name) throws InterruptedException
TimeUnit.SECONDS.sleep(3);
System.out.println("吃肉" +name);









java的八锁问题_Test

从这个结果可以看出,如果使用同步语法synchronized,将会锁住对象,但不会锁住类

非同步语法块的讨论

package day2.八锁问题;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class test2
@Test
public void test1() throws InterruptedException
Eat eat=new Eat();
new Thread(()->
try
TimeUnit.SECONDS.sleep(3);
eat.eat_meat();
catch (InterruptedException e)
e.printStackTrace();

).start();

new Thread(()->
try
TimeUnit.SECONDS.sleep(3);
eat.eat_rice();
catch (Exception e)
e.printStackTrace();


).start();

TimeUnit.SECONDS.sleep(10);








class Eat
void eat_rice()
System.out.println("吃饭");


synchronized void eat_meat() throws InterruptedException

TimeUnit.SECONDS.sleep(3);
System.out.println("吃肉");









java的八锁问题_Test_02


从输出可以到,非同步语法块不会受到同步语法块的限制,提前结束

静态同步语法块之间的讨论

package day2.八锁问题;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class test4
public static void main(String args[] ) throws InterruptedException
Eat eat=new Eat();
Eat eat1=new Eat();
new Thread(()->
try
eat.eat_meat();
TimeUnit.SECONDS.sleep(3);
catch (InterruptedException e)
e.printStackTrace();

).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->
try

eat1.eat_rice();
TimeUnit.SECONDS.sleep(3);
catch (Exception e)
e.printStackTrace();


).start();
TimeUnit.SECONDS.sleep(3);












package day2.八锁问题;

import java.util.concurrent.TimeUnit;

class Eat2
static synchronized void eat_rice()
System.out.println("吃饭");



synchronized void eat_meat() throws InterruptedException
TimeUnit.SECONDS.sleep(3);
System.out.println("吃肉");



java的八锁问题_Test_03


从输出的结果看,同步语法的静态语法块会被锁住顺序输出结果

非静态的同步语法块之间的讨论

package day2.八锁问题;

import java.util.concurrent.TimeUnit;

public class test5
public static void main(String args[] ) throws InterruptedException
Eat2 eat=new Eat2();
Eat2 eat1=new Eat2();
new Thread(()->
try
eat.eat_meat();
catch (InterruptedException e)
e.printStackTrace();

).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->
try

eat1.eat_rice();
catch (Exception e)
e.printStackTrace();


).start();
TimeUnit.SECONDS.sleep(3);


package day2.八锁问题;

import java.util.concurrent.TimeUnit;

class Eat2
static synchronized void eat_rice()
System.out.println("吃饭");



synchronized void eat_meat() throws InterruptedException
TimeUnit.SECONDS.sleep(3);
System.out.println("吃肉");



java的八锁问题_Test_04


从输出的结果,可以看到静态语法会锁住类,直到方法执行完毕,释放锁


以上是关于java的八锁问题的主要内容,如果未能解决你的问题,请参考以下文章

八锁问题详解

八锁问题详解

java多线程(十四)八锁CopyOnWriteArrayListCopyOnWriteArraySetConcurrentHashMapCallable

java多线程 线程八锁

java多线程 -- 线程八锁

线程八锁