Browse Source

测试线程问题 gitee issues/I4S9U5

hubin 2 năm trước cách đây
mục cha
commit
01720ed0ee

+ 1 - 1
build.gradle

@@ -2,7 +2,7 @@ import java.time.LocalDateTime
 
 
 allprojects {
 allprojects {
     group = 'com.baomidou'
     group = 'com.baomidou'
-    version = "3.5.2.7-SNAPSHOT"
+    version = "3.5.2.8-SNAPSHOT"
 }
 }
 
 
 ext {
 ext {

+ 11 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/PostgresqlTest.java

@@ -2,6 +2,7 @@ package com.baomidou.mybatisplus.test.postgresql;
 
 
 import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
 import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
 import com.baomidou.mybatisplus.test.postgresql.mapper.PgtableMappper;
 import com.baomidou.mybatisplus.test.postgresql.mapper.PgtableMappper;
+import com.baomidou.mybatisplus.test.postgresql.service.IPgtableService;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -14,9 +15,10 @@ import java.util.List;
 @ExtendWith(SpringExtension.class)
 @ExtendWith(SpringExtension.class)
 @ContextConfiguration(locations = {"classpath:postgresql/spring-test-postgresql.xml"})
 @ContextConfiguration(locations = {"classpath:postgresql/spring-test-postgresql.xml"})
 public class PostgresqlTest {
 public class PostgresqlTest {
-
     @Resource
     @Resource
     private PgtableMappper mapper;
     private PgtableMappper mapper;
+    @Resource
+    private IPgtableService pgtableService;
 
 
     @Test
     @Test
     void test() {
     void test() {
@@ -28,4 +30,12 @@ public class PostgresqlTest {
         Assertions.assertNotNull(pgtableList);
         Assertions.assertNotNull(pgtableList);
         Assertions.assertTrue(pgtableList.size() > 0);
         Assertions.assertTrue(pgtableList.size() > 0);
     }
     }
+
+    @Test
+    void testTask() throws InterruptedException {
+        // 测试线程
+        pgtableService.testTask();
+        // 主线程休眠
+        Thread.sleep(10000);
+    }
 }
 }

+ 11 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/service/IPgtableService.java

@@ -0,0 +1,11 @@
+package com.baomidou.mybatisplus.test.postgresql.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
+import org.springframework.stereotype.Service;
+
+@Service
+public interface IPgtableService extends IService<Pgtable> {
+
+    void testTask();
+}

+ 30 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/postgresql/service/impl/PgtableServiceImpl.java

@@ -0,0 +1,30 @@
+package com.baomidou.mybatisplus.test.postgresql.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.baomidou.mybatisplus.test.postgresql.entity.Pgtable;
+import com.baomidou.mybatisplus.test.postgresql.mapper.PgtableMappper;
+import com.baomidou.mybatisplus.test.postgresql.service.IPgtableService;
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class PgtableServiceImpl extends ServiceImpl<PgtableMappper, Pgtable> implements IPgtableService {
+
+    @Override
+    public void testTask() {
+        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);
+        executorService.schedule(() -> {
+            try {
+                this.updateById(Pgtable.builder().id(1L).compName("testTask01").age(11).build());
+                baseMapper.updateById(Pgtable.builder().id(1L).compName("testTask02").age(12).build());
+            } catch (Exception exception) {
+                exception.getStackTrace();
+            } finally {
+                executorService.shutdown();
+            }
+        }, 3, TimeUnit.SECONDS);
+    }
+}

+ 1 - 0
mybatis-plus/src/test/resources/postgresql/spring-test-postgresql.xml

@@ -5,5 +5,6 @@
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
 
+    <context:component-scan base-package="com.baomidou.mybatisplus.test.postgresql.service"/>
     <context:component-scan base-package="com.baomidou.mybatisplus.test.postgresql.config"/>
     <context:component-scan base-package="com.baomidou.mybatisplus.test.postgresql.config"/>
 </beans>
 </beans>