美文网首页
springboot线程安全处理方法记录

springboot线程安全处理方法记录

作者: 杨少侠_Y | 来源:发表于2018-09-05 15:01 被阅读0次

Semaphore并发包

semaphore是基于计数的记号量,它可以设定一个资源的总数量,基于这个总数量,多线程竞争获取许可信号,做自己的申请后,返回许可,并释放资源.超过总数量后,线程申请许可,信号将被阻塞,待有资源后,继续执行
下面是单线程实现:

package cn.easyAccess.core.cache.local;

import java.util.concurrent.Semaphore;

/**
 * @author lqyang
 * @Title: ${file_name}
 * @date 2018/9/511:29
 */
public class semaphoreDemo {

    Semaphore semaphore = new Semaphore(1);

    public void TestSemaphore(){
        try {
            //获取资源许可数量
            int var = semaphore.availablePermits();
            if (var <= 0){
                System.out.println(Thread.currentThread().getName()+"资源许可被占用");
            }
            //获取许可
            semaphore.acquire();
            System.out.println(Thread.currentThread().getName()+"资源许可拿到-->start");
            Thread.sleep(10);
            System.out.println(Thread.currentThread().getName()+"资源处理完毕-->stop");
            //将占用信号归还
            semaphore.release();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
package cn.easyAccess.core.cache.local;

/**
 * @author lqyang
 * @Title: ${file_name}
 * @date 2018/8/510:49
 */
public class semaphoreTest extends Thread{

    semaphoreDemo semaphoreDemo;

    public semaphoreTest(semaphoreDemo semaphoreDemo){
        super();
        this.semaphoreDemo = semaphoreDemo;
    }

    @Override
    public void run() {
        //TODO
        semaphoreDemo.TestSemaphore();
    }
}
package cn.easyAccess.core.cache.local;

/**
 * @author lqyang
 * @Title: ${file_name}
 * @date 2018/9/511:54
 */
public class demoTest {

    public static void main(String[] args) {
        semaphoreDemo semaphoreDemo = new semaphoreDemo();
        new semaphoreTest(semaphoreDemo).start();
    }
}

执行结果如下:



多线程实现:

for (int i= 1; i< 5; i++){
            new semaphoreTest(semaphoreDemo).start();
}

执行结果如下:



下面是springboot具体实现:

 @GetMapping("/getOrderBuyerInfo")
    public RespApi getOrderBuyerInfo(String keyword){
        //可用资源数
        int availablePermits = semaphore.availablePermits();
        List<BuyersInfoModel>  buyersInfoModels = new ArrayList<>();
        if (availablePermits > 0){
            logger.info("抢到资源");
        }else {
            logger.info("资源已被占用");
            return RespApi.failure("No resources");
        }
        try{
            //请求占用一个资源
            semaphore.acquire(1);
            String userId = getUser().getCreateUserId();
            buyersInfoModels = orderService.getOrderShippingInfo(userId,keyword);
        }catch (Exception e){
            e.printStackTrace();
            return RespApi.failure("error");
        }finally {
            //释放资源
            semaphore.release(1);
        }
        return RespApi.success(buyersInfoModels);
    }

通过semaphore来实现线程安全。

相关文章

  • springboot线程安全处理方法记录

    Semaphore并发包 semaphore是基于计数的记号量,它可以设定一个资源的总数量,基于这个总数量,多线程...

  • SpringBoot-@Async异步注解

    使用@Async可以帮助我们快速将@Async标注的方法在一个异步线程池中处理 SpringBoot-线程池使用 ...

  • 什么是线程安全

    线程安全的概念和处理方法https://zhuanlan.zhihu.com/p/73899015

  • 7.2 Handler消息传递机制

    简介:handler消息处理者,用来发生和处理消息,用于解决线程安全问题; 线程安全问题:多线程导致线程安全问题,...

  • Java多线程学习二 synchronized

    前面讲过,线程共享变量是非线程安全的,synchronized关键字可使方法变为线程安全的方法 一、线程安全问题 ...

  • HashMap和HashTable的区别

    1、线程是否安全:HashMap是非线程安全的,HashTable是线程安全的;HashTable内部的方法基本上...

  • ThreadLocal使用与原理

    在处理多线程并发安全的方法中,最常用的方法,就是使用锁,通过锁来控制多个不同线程对临界区的访问。 但是,无论是什么...

  • 超强解析:ThreadLocal的使用与原理,关键点都在里面

    点赞再看,养成习惯! 在处理多线程并发安全的方法中,最常用的方法,就是使用锁,通过锁来控制多个不同线程对临界区的访...

  • ThreadLocal原理

    在处理多下线程并发安全的方法中,最常用的方法就是使用锁,通过锁来控制多个线程对临界区的访问。但是不管什么锁,乐观锁...

  • SpringBoot 异常处理

    SpringBoot 异常处理机制,按照下面这4部分进行记录,讲解: SpringBoot 默认异常处理机制 自定...

网友评论

      本文标题:springboot线程安全处理方法记录

      本文链接:https://www.haomeiwen.com/subject/ejaowftx.html