首页 > 其他分享 >SynchronousQueue的简单介绍

SynchronousQueue的简单介绍

时间:2022-11-28 23:45:31浏览次数:45  
标签:java SynchronousQueue 简单 boot 介绍 springframework org import blockingQueue

SynchronousQueue有点特殊,具备生产者和消费者,但是生产者生产后会执行阻塞,当产生的数据有人取走的时候,生产者的阻塞状态会解除,再次生产再次阻塞,再次等待消费者解锁,多个线程生产数据,对应的需要多个线程的消费者消费数据

同时该队列可以自定义锁的模式,及公平锁和非公平锁由设置决定

非公平模式放置的数据

 

 获取数据的顺序

 

 因为栈的数据结构

 

公平模式

 

 取出的数据顺序

 

 

 

 

 

 结构探究

 

 

 

 

 

 代码部分

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.java</groupId>
    <artifactId>test-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--tomcat容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <!--引入junit单元测试依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--判断空的用法  -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!--ThreadFactoryBuilder的依赖包,多线程使用-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>

        <!--Lists.partition要用的依赖-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
       <!--ListUtils.partition使用的依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>study</finalName>
    </build>


</project>

  

server.port=2001


logging.level.com.java.test=debug
logging.level.web=debug
spring.devtools.add-properties=false

  

package com.java.test.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/25 14:43
 */
@Component
public class ThreadPoolDemo {

    @Bean
    public ExecutorService createThreadPool(){
        /**
         * 线程资源必须通过线程池提供,不允许在应用中自行显式创建线程。
         * 说明:使用线程池的好处是减少在创建和销毁线程上所花的时间以及系统资源的开销,
         * 解决资源不足的问题。如果不使用线程池,有可能造成系统创建大量同类线程而导致消耗完内存或者“过度切换”的问题。
         */

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("qiuxie-pool-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 2,
                1000L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        return  singleThreadPool;
    }
}

  

package com.java.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/10/20 15:32
 */
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

  

package com.java.test.synchronousqueues.demo;

import com.java.test.config.ThreadPoolDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/22 17:37
 */
@Slf4j
@RestController
public class SynchronousQueueDemo {
    //http://127.0.0.1:2001/produceData

    //http://127.0.0.1:2001/consumerData


    BlockingQueue<String> blockingQueue=new SynchronousQueue<>(true);


    //BlockingQueue<String> blockingQueue=new LinkedBlockingQueue<>();
    @Autowired
   private ThreadPoolDemo threadPoolDemo;

    @GetMapping("/produceData")
    public String produceData() throws InterruptedException {
        threadPoolDemo.createThreadPool().execute(new SynchronousQueueThread(blockingQueue));
        return  null;
    }


    @GetMapping("/consumerData")
    public String consumerData() throws InterruptedException {
        threadPoolDemo.createThreadPool().execute(new SynchronousQueueTakeThread(blockingQueue));
        return  null;
    }

}

  

package com.java.test.synchronousqueues.demo;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/24 9:23
 */
@Slf4j
public class SynchronousQueueThread implements  Runnable {


    private BlockingQueue<String> blockingQueue;

    public SynchronousQueueThread(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {

        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            String format = simpleDateFormat.format(date);
            log.info("添加到队列的数据:{}",format);
            blockingQueue.put(format);
            Thread.State state = Thread.currentThread().getState();
            log.info("【添加到队列的数据】state:{}",state);
        } catch (Exception e) {
            log.error("队列放置数据失败:{}",e);
        }

    }
}

  

package com.java.test.synchronousqueues.demo;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/11/24 23:00
 */
@Slf4j
public class SynchronousQueueTakeThread implements Runnable {

    private BlockingQueue<String> blockingQueue;

    public SynchronousQueueTakeThread(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        try {
            String take = blockingQueue.take();
            log.info("从队列中取出的数据:{}",take);
            Thread.State state = Thread.currentThread().getState();
            log.info("【从队列中取出的数据】state:{}",state);
        } catch (Exception e) {
            log.error("队列数据获取异常:{}",e);
        }

    }
}

  

标签:java,SynchronousQueue,简单,boot,介绍,springframework,org,import,blockingQueue
From: https://www.cnblogs.com/q202105271618/p/16901848.html

相关文章

  • java 简单拼图小游戏的实现
    这个是游戏页面的源代码packagePuzzle;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event......
  • Promise的简单用法
    什么是Promise?Promise是异步编程的一种解决方案,其实是一个构造函数,自己身上有all、reject、resolve这几个方法,原型上有then、catch等方法。Promise对象有以下两个特点。......
  • HTTP-响应消息-响应头、Response-功能介绍
    HTTP-响应消息-响应头响应头: 1.格式:头名称:值 2.常见的响应头: 1.Content-Type:服务器告诉客户端本次响应体数据格式以及编码......
  • windows下OpenCV开发环境的搭建(最简单)
    为什么是做简单基于msys2,是用mingw-gcc编译。opencv用包管理器自动下载,无需手动配置。步骤下载安装msys2https://www.msys2.org/从官网下载msys2并安装安装gcc,OpenC......
  • Android Google开源库——Volley的简单使用
    介绍一下AndroidGoogle开源库——Volley的简单使用volley 项目地址 ​​https://github.com/smanikandan14/Volley-demo​​JSON,图像等的异步下载;网络请求的排序(sc......
  • 视图引擎Smarty的简单使用
    参考: ​​http://www.jb51.net/article/5091.htm​​参考:​​http://baike.baidu.com/link?url=FFySw2r-dsE-lTdQgGy2DpLhciXM-JqUagEIydE_aFY3rGMnlYrf4eOOWw-fK-MFNFMp8v......
  • 从 Java 到 Kotlin - 介绍 Kotlin
    B站视频:​​https://www.bilibili.com/video/BV1Ti4y197u9/?share_source=copy_web&vd_source=74448386fecd6b0affd408eabb573749​​语法层面可空对象(和C#的<Nullable>......
  • superblock介绍
    一、概述它是硬盘分区开头——开头的第一个byte是byte0,从byte1024开始往后的一部分数据。由于blocksize最小是1024 bytes,所以superblock可能是在block1中(此......
  • 介绍Spring名称生成策略接口BeanNameGenerator
    众所周知,Spring容器可以简单粗暴的把它理解为一个大大的Map,存储着容器所管理的所有的单实例对象。我们从使用getBean(StringbeanName)方法,根据bean名称就能获得容器内唯一......
  • 生活简单就是幸福
    人为财死,鸟为食亡,鸟为什么要为食亡?就因为它有一个肚子,不吃就只有饿死,饿死也是死,说来说去,都怪肚子。人也一样,想想人为什么而整日奔波?还不是因为要吃饭。人不比鸟......