java多线程学习之 类级别同步锁

j

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class Thread1 implements Runnable {

    public void print(Object lockSingal) {
        synchronized (lockSingal) {
            int i = 0;
            while (true) {

                try {
                    Thread.sleep(3000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("thread_11111111 print i="+i++);

            }
        }
    }

    @Override
    public void run() {
        this.print(TestSynchronized.class);
    }
}

——————————————————————————

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class Thread2 implements Runnable{

    public void print(Object lockSingal) {
        synchronized (lockSingal) {
            int j = 0;
            while (true) {
                try {
                    Thread.sleep(2000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread_2 print j="+j++);

            }
        }

    }


    @Override
    public void run() {
        this.print(TestSynchronized.class);
    }
}

 ————————————————————

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class TestSynchronized {
    public static Object lockSignal = new Object();

    public static void main(String[] args) {


        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        new Thread(thread1).start();
        new Thread(thread2).start();


    }


}

运行结果先取到同一个类级别锁的线程一直运行,另一个线程得不到锁了,因为先取到锁的本例中是个永远的循环不会释放锁

——————————————————————————

第二种类级别同步锁的写法:

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class TestSynchronized {
    public static Object lockSignal = new Object();

    public static void main(String[] args) {


        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        new Thread(thread1).start();
        new Thread(thread2).start();


    }


}

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class Thread1 implements Runnable {

    public void print(Object lockSingal) {
        synchronized (lockSingal) {
            int i = 0;
            while (true) {

                try {
                    Thread.sleep(3000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("thread_11111111 print i="+i++);

            }
        }
    }

    @Override
    public void run() {
        this.print(TestSynchronized.lockSignal);
    }
}

 

/**
 * @author zhaoyong
 * @Date 2022/4/24
 * @Description
 */
public class Thread2 implements Runnable{

    public void print(Object lockSingal) {
        synchronized (lockSingal) {
            int j = 0;
            while (true) {
                try {
                    Thread.sleep(2000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread_2 print j="+j++);

            }
        }

    }


    @Override
    public void run() {
        this.print(TestSynchronized.lockSignal);
    }
}

 

 

运行效果是一样的 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值