色空大师 2024-09-05 22:49 采纳率: 42.3%
浏览 4

使用线程池跑线程,当第一步进行完后暂停一秒,然后去跑第二步,没有打印第二部信息

使用线程池跑线程,当第一步进行完后暂停一秒,然后去跑第二步,没有打印第二部信息
线程池配置如下:

package com.test.pool.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadConfig {

    @Qualifier("simpleExecutor")
    @Bean
    public ThreadPoolExecutor simpleExecutor() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(16,
                16,
                10,
                TimeUnit.MINUTES,
                new SynchronousQueue<>(),
                new CustomizableThreadFactory("simple-pool-"),
                new ThreadPoolExecutor.AbortPolicy());
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        return threadPoolExecutor;
    }
}


测试类如下


package com.test.pool;

import com.test.pool.thread.MyThreadTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SimpleThreadTest {

    @Resource
    ExecutorService simpleExecutor;

    @Test
    public void simpleThreadTest(){
        simpleExecutor.submit(()->{
            log.info("进入simpleThreadTest");
            try {
                first();
                Thread.sleep(100);
            }catch (Exception e){
                log.error("sleep error");
            }
            second();
        });
    }

    public void first(){
        log.info("first");
    }

    public void second(){
        log.info("second");
    }
}


运行结果如下:

img


问:第二步的信息呢?0.1秒应该很快啊

  • 写回答

2条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2025-02-18 14:10
    关注
    让【道友老李】来帮你解答,本回答参考gpt编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    问题分析: 根据给出的代码和运行结果,我们发现在SimpleThreadTest类中的simpleThreadTest方法中,使用线程池执行了一个任务,其中在first方法执行完毕后会休眠100毫秒,然后执行second方法,但是在运行结果中并没有打印出"second"信息。这是因为在休眠的时候,并没有在执行second方法前等待100毫秒,而是直接执行了second方法。 解决方法: 为了确保在第一步执行完后暂停1秒再执行第二步,可以在first方法后使用Thread.sleep(1000)来暂停1秒,而不是休眠100毫秒。这样在休眠1秒后才会执行second方法。 代码示例:
    package com.test.pool;
    import com.test.pool.thread.MyThreadTest;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import javax.annotation.Resource;
    import java.util.concurrent.ExecutorService;
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Slf4j
    public class SimpleThreadTest {
        @Resource
        ExecutorService simpleExecutor;
        @Test
        public void simpleThreadTest(){
            simpleExecutor.submit(() ->{
                log.info("进入simpleThreadTest");
                try {
                    first();
                    Thread.sleep(1000); // 休眠1秒
                } catch (Exception e){
                    log.error("sleep error");
                }
                second();
            });
        }
        public void first(){
            log.info("first");
        }
        public void second(){
            log.info("second");
        }
    }
    

    这样处理后再运行程序,就会在第一步执行完后暂停1秒再打印出"second"信息。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月5日