同步代码块和同步方法

package cn.test.dxc.syc;

public class huizong {
    //同步方法默认用this或者当前类class对象作为锁
    public synchronized void methodA(){//默认当前对象
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"方法A愉快的打印了"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void methodB(){//锁当前对象
        synchronized (this){
            for (int i = 0; i <5 ; i++) {
                System.out.println(Thread.currentThread().getName()+"方法B愉快的打印了"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void methodC(){
        Object obj = new Object();//锁为自定义的对象
        synchronized (obj){
            for (int i = 0; i <5 ; i++) {
                System.out.println(Thread.currentThread().getName()+"方法C愉快的打印了"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void methodD(){
        synchronized (huizong.class){//同步代码块用当前类信息对象
            for (int i = 0; i <5 ; i++) {
                System.out.println(Thread.currentThread().getName()+"方法D愉快的打印了"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    public static synchronized  void methodE(){//默认当前的类信息对象
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"方法E愉快的打印了"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        final huizong test = new huizong();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.methodA();
            }
        });
        thread1.start();


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.methodB();
            }
        });
        thread2.start();

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.methodC();
            }
        });
        thread3.start();


        Thread thread4 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.methodD();
            }
        });
        thread4.start();

        Thread thread5 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.methodA();
            }
        });
        thread5.start();

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值