java线程的几个状态和锁的作用范围

线程的生命周期

java线程的几个状态和锁的作用范围

 

 线程锁的作用范围:

1、对象实例锁,锁住同一个对象

demo1:

当t1获取线程时,t2会等待t1睡两秒后执行,这也说明了Thread.sleep()不会释放锁资源

/**
 * 锁的作用范围
 */
public class SynchronizedTest {

    public static void main(String[] args) {
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        new Thread(()->{
            synchronizedTest.test1();
        },"t1").start();

        new Thread(()->{
            synchronizedTest.test1();
        },"t2").start();
    }

    synchronized void test1(){
        System.out.println(Thread.currentThread().getName()+"获取锁");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

demo2:当我们再new一个SynchronizedTest 时,synchronizedTest ,synchronizedTest2 是不同的实例,t1,t2线程几乎是同时输出结果,这也说明了

synchronized 修饰实例方法时,锁住得是实例对象,相当于synchronized(this) void test1(){
public class SynchronizedTest {

    public static void main(String[] args) {
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        SynchronizedTest synchronizedTest2 = new SynchronizedTest();
        new Thread(()->{
            synchronizedTest.test1();
        },"t1").start();

        new Thread(()->{
            synchronizedTest2.test1();
        },"t2").start();
    }

    synchronized void test1(){
        System.out.println(Thread.currentThread().getName()+"获取锁");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

demo3:synchronized这次修饰代码块,其执行结果是和demo1是一样的,这三种都是锁住的对象实例

public class SynchronizedTest {

    public static void main(String[] args) {
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        new Thread(()->{
            synchronizedTest.test1();
        },"t1").start();

        new Thread(()->{
            synchronizedTest.test1();
        },"t2").start();
    }

     void test1(){
         synchronized(this){
             System.out.println(Thread.currentThread().getName()+"获取锁");
             try {
                 Thread.sleep(2000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
    }
}

2、静态方法,类对象:锁住的是Class,类锁

demo4:当t1获取线程时,t2会等待t1睡两秒后执行,这里的静态方法锁住的是类对象

public class SynchronizedTest {

    public static void main(String[] args) {
new Thread(()->{
            SynchronizedTest.test1();
        },"t1").start();

        new Thread(()->{
            SynchronizedTest.test1();
        },"t2").start();
    }

    synchronized static void test1(){
             System.out.println(Thread.currentThread().getName()+"获取锁");
             try {
                 Thread.sleep(2000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
    }
}

demo5:当我们new了两个不同的实例,当t1线程获取锁是,t2线程会等待两秒后执行,这说明了锁住了类对象,

synchronizedTest,synchronizedTest2 是两个不同的实例,当是同一个类对象。
public class SynchronizedTest {

    public static void main(String[] args) {
        SynchronizedTest synchronizedTest = new SynchronizedTest();
        SynchronizedTest synchronizedTest2 = new SynchronizedTest();
        new Thread(()->{
            synchronizedTest.test1();
        },"t1").start();

        new Thread(()->{
            synchronizedTest2.test1();
        },"t2").start();
    }

   void test1(){
        synchronized (SynchronizedTest.class) {
            System.out.println(Thread.currentThread().getName() + "获取锁");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

java线程的几个状态和锁的作用范围

上一篇:Java 内存模型


下一篇:【如何在Eclipse中导入JavaDoc】