java线程之lock

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test{

    private ArrayList<Integer> list = new ArrayList<>();

    private int num = 5;


    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Test test = new Test();
        new Thread(){
            public void run(){
                try {
                    System.out.println("线程0开始执行");
                    test.isStart(this,lock);
                    System.out.println("线程0结束执行");
                }catch (Exception e){
                    e.printStackTrace();
                }

            };
        }.start();

        new Thread(){
            public void run(){
                try {
                    System.out.println("线程1开始执行");
                    test.isStart(this,lock);
                    System.out.println("线程1结束执行");
                   /* this.sleep(3000);*/
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /*
    * lock非阻塞
    * */
    public void lockStart(Thread thread,Lock lock){
        lock.lock();
        try {
            while (num > 0){
                num=num-1;
                System.out.println(thread.getName()+"花费1元");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 可以保证发生异常 锁可以得到释放 避免死锁的发生
            lock.unlock();
        }
    }

    /*
    * 阻塞
    * */
    public void synStart(Thread thread){

        synchronized (this){
           while (num > 0){
               num=num-1;
               System.out.println(thread.getName()+"花费1元");
           }
        }
    }

    /*
    * 返回true 和 false
    * */
    public void isStart(Thread thread,Lock lock) {
            try {
                if (lock.tryLock()) {
                while (num > 0) {
                    num = num - 1;
                    System.out.println(thread.getName() + "花费1元");
                }
                }else {
                    System.out.println("没有拿到"+lock.tryLock());
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
    }

    /*
     * lockInterruptibly()响应中断的使用方法:
     * */
    public void interruptibly() {


    }

}

 

 

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test02 {


    public static void main(String[] args) {
        Lock lock = new ReentrantLock();

        new Thread() {
            @Override
            public void run() {
                String tName = Thread.currentThread().getName();

                if (lock.tryLock()) {
                    System.out.println(tName + "获取到锁!");
                } else {
                    System.out.println(tName + "获取不到锁!");
                    return;
                }

                try {

                    for (int i = 0; i < 5; i++) {
                        System.out.println(tName + ":" + i);
                    }

                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println(tName + "出错了!!!");
                } finally {
                    System.out.println(tName + "释放锁!!");
                    lock.unlock();
                }

            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                String tName = Thread.currentThread().getName();

                if (lock.tryLock()) {
                    System.out.println(tName + "获取到锁!");
                } else {
                    System.out.println(tName + "获取不到锁!");
                    return;
                }

                try {
                    for (int i = 0; i < 5; i++) {
                        System.out.println(tName + ":" + i);
                    }

                } catch (Exception e) {
                    System.out.println(tName + "出错了!!!");
                } finally {
                    System.out.println(tName + "释放锁!!");
                    lock.unlock();
                }
            }
        }.start();


    }

}

 

 

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test3 {

    class Mythread extends Thread{

        private  Test3 test3;

        public Mythread(Test3 test3){
            this.test3 = test3;
        }

        @Override
        public void run(){
            try {
                test3.insert(Thread.currentThread());
            }catch (Exception e){
                System.out.println(Thread.currentThread().getName()+"被中断");
            }
        }

    }


    private Lock lock = new ReentrantLock();


    public void insert(Thread thread)throws InterruptedException{
        lock.lockInterruptibly();
        try {
            System.out.println(thread.getName()+"得到了锁");
            long starTime = System.currentTimeMillis();
            for(int i =0;i<5;i++){
                if( System.currentTimeMillis() - starTime >= Integer.MAX_VALUE  ){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println(Thread.currentThread().getName()+"执行finaly");
            lock.unlock();
            System.out.println(thread.getName()+"释放了锁");
        }

    }


    public static void main(String[] args) {
        Test3 test3 = new Test3();
        Thread thread1 = new Mythread(test3);
        Mythread thread2 = new Mythread(test3);
        thread1.start();
        thread2.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.interrupt();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值