java多线程锁同步控制

一、使用关键字:synchronized 

1、synchronized可以修饰方法,(其实也是锁的是当前方法的对象),也可以修饰代码块,修饰代码也需要传入一个对象。 synchronized(object){}

2、两种方式都是获取的是synchronized修饰对象的monitor锁!这才是真正的锁!

3、synchronized所修饰的锁不可以为null!

public class Main{
    private static int commonTag = 0;
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable);
        Thread t4 =  new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

   public static synchronized void add(long ID){
        System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++);
    }

//   public static  void add(long ID){
//        synchronized(object){  //也需要传入一个对象
//            System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++);
//
//        }
//    }
}
当前线程ID:10执行结果=0
当前线程ID:11执行结果=1
当前线程ID:12执行结果=2
当前线程ID:13执行结果=3
当前线程ID:10执行结果=4
当前线程ID:12执行结果=5
当前线程ID:13执行结果=6
当前线程ID:11执行结果=7
当前线程ID:10执行结果=8
当前线程ID:13执行结果=9
当前线程ID:12执行结果=10
当前线程ID:11执行结果=11
当前线程ID:10执行结果=12
当前线程ID:13执行结果=13
当前线程ID:12执行结果=14
当前线程ID:11执行结果=15

如果不用则结果如下:

当前线程ID:12执行结果=3
当前线程ID:10执行结果=1
当前线程ID:13执行结果=2
当前线程ID:11执行结果=0
当前线程ID:12执行结果=5
当前线程ID:11执行结果=7
当前线程ID:13执行结果=6
当前线程ID:10执行结果=4
当前线程ID:11执行结果=8
当前线程ID:10执行结果=9
当前线程ID:13执行结果=9
当前线程ID:12执行结果=8

二、死锁的原因和如何避免!

对于程序员需要关注两个方向:一个是内存不足引起的死锁,另一个是交叉锁,各持对方的排它锁导致的死锁,谁都不肯放!

public class Main{
    private static int commonTag = 0;
    private static Object object1 = new Object();
    private static Object object2 = new Object();
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add2(Thread.currentThread().getId());

                }
            }
        };

        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable2);
        Thread t4 =  new Thread(runnable2);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

   public static  void add(long ID){
        synchronized(object1){
            synchronized(object2){
                System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++);

            }
        }
    }
    public static  void add2(long ID){
        synchronized(object2){
            synchronized(object1){
                System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++);

            }
        }
    }

}

三、LOCK(java.util.concurrent.locks)

1、读写锁?

2、自旋锁

import java.util.concurrent.locks.ReentrantLock;
public class Main{
    private static int commonTag = 0;
    private  static ReentrantLock lock = new ReentrantLock();

    private static   Object object = new Object();
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable);
        Thread t4 =  new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
   public static  void add(long ID){
        try{
            lock.lock();
            System.out.println("当前线程ID:"+ID+"执行结果="+commonTag++);
        }finally {
            lock.unlock();
        }

    }
}

 

转载于:https://my.oschina.net/u/3697586/blog/3007873

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值