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