【发布时间】:2017-05-04 07:23:58
【问题描述】:
我有以下代码:
@SpringBootApplication
@Component
public class ProductApiApplication2 implements CommandLineRunner{
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public void run(String... args) throws Exception {
for (int i=0;i<5;i++){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("start time:" + System.currentTimeMillis() / 1000L);
String value = stringRedisTemplate.opsForList().rightPop("aaa", 5, TimeUnit.SECONDS);
System.out.println("get value, time :" + System.currentTimeMillis() / 1000L);
}
}).start();
}
}
public static void main(String[] args) {
SpringApplication.run(ProductApiApplication2.class, args);
}
}
使用 Spring Boot 和 Spring Data Redis 构建。 我希望线程将同时完成,但事实并非如此。我得到了这个输出:
start
start
start
start
start
get value, time :1493881455
get value, time :1493881460
get value, time :1493881466
get value, time :1493881472
get value, time :1493881477
看起来处理不是并行进行的。这是为什么呢?
这是我的配置:
spring :
profiles : dev-company
redis :
cluster :
nodes :
- 192.168.3.171:7001
- 192.168.3.171:7002
- 192.168.3.168:7003
- 192.168.3.168:7004
- 192.168.3.169:7005
- 192.168.3.169:7006
pom.xml
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
标签: spring-boot spring-data-redis