Spring 来完成多线程,能不能写个demo
时间: 2023-03-07 15:36:53 浏览: 66
可以!我可以给你一个基于Spring的多线程示例:public class MyThread extends Thread {
public void run(){
System.out.println("多线程启动");
}
}public class Demo {
public static void main(String[] args){
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
}
}
相关问题
写一个多线程demo,使用ThreadPoolTaskExecutor()
以下是一个使用ThreadPoolTaskExecutor实现多线程的demo:
```java
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class MultiThreadDemo {
private ThreadPoolTaskExecutor taskExecutor;
public MultiThreadDemo(ThreadPoolTaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void execute() {
taskExecutor.execute(() -> {
// task1
System.out.println("Task1 is running on thread " + Thread.currentThread().getName());
});
taskExecutor.execute(() -> {
// task2
System.out.println("Task2 is running on thread " + Thread.currentThread().getName());
});
}
}
```
在上面的代码中,我们首先定义了一个MultiThreadDemo类,它接受一个ThreadPoolTaskExecutor对象作为构造函数的参数。然后我们定义了一个execute()方法,在这个方法中,我们使用taskExecutor.execute()方法来提交两个任务,分别是task1和task2。这两个任务会在不同的线程中执行。
如果你想要使用这个demo,你需要先创建一个ThreadPoolTaskExecutor对象,并将它传递给MultiThreadDemo的构造函数。然后你可以调用MultiThreadDemo的execute()方法来执行这个demo。
写一个springboot多数据源配置的demo
好的,以下是一个基于 Spring Boot 的多数据源配置示例:
首先,在 `application.properties` 文件中配置数据源属性:
```properties
# 主数据源配置
spring.datasource.primary.url=jdbc:mysql://localhost:3306/primary?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
# 第二个数据源配置
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/secondary?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
```
然后,创建多数据源配置类 `MultipleDataSourceConfig`:
```java
@Configuration
public class MultipleDataSourceConfig {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
public DynamicDataSource dynamicDataSource(@Qualifier("primaryDataSource") DataSource primaryDataSource,
@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DatabaseType.PRIMARY, primaryDataSource);
targetDataSources.put(DatabaseType.SECONDARY, secondaryDataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(primaryDataSource);
return dataSource;
}
}
```
其中,`primaryDataSource()` 和 `secondaryDataSource()` 方法分别创建主数据源和第二个数据源,`dynamicDataSource()` 方法则创建一个动态数据源,将两个数据源加入到其中。
最后,创建 `DatabaseType` 枚举类表示数据库类型:
```java
public enum DatabaseType {
PRIMARY, SECONDARY
}
```
并创建 `DynamicDataSource` 类实现动态数据源:
```java
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContextHolder.getDatabaseType();
}
}
```
在上面的类中,`determineCurrentLookupKey()` 方法根据当前线程的数据源类型,返回对应的数据源。
最后,创建一个 `DatabaseContextHolder` 类用于设置当前线程的数据源类型:
```java
public class DatabaseContextHolder {
private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<>();
public static void setDatabaseType(DatabaseType databaseType) {
contextHolder.set(databaseType);
}
public static DatabaseType getDatabaseType() {
return contextHolder.get();
}
public static void clearDatabaseType() {
contextHolder.remove();
}
}
```
在使用多数据源的时候,需要在代码中手动设置当前线程的数据源类型,例如:
```java
// 切换到主数据源
DatabaseContextHolder.setDatabaseType(DatabaseType.PRIMARY);
// 执行查询语句
List<User> userList = userMapper.select();
// 切换到第二个数据源
DatabaseContextHolder.setDatabaseType(DatabaseType.SECONDARY);
// 执行查询语句
List<Department> departmentList = departmentMapper.select();
// 切换回主数据源
DatabaseContextHolder.setDatabaseType(DatabaseType.PRIMARY);
```
以上就是一个基于 Spring Boot 的多数据源配置示例。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)