ip696
ip696

Reputation: 7104

ThreadPoolTaskScheduler not work with pool of threads

I create

@Bean
ThreadPoolTaskScheduler taskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setAwaitTerminationSeconds(1);
    threadPoolTaskScheduler.setThreadNamePrefix("Test-");
    threadPoolTaskScheduler.initialize();
    return threadPoolTaskScheduler;
}

And in my component I use it:

@PostConstruct
    public void test() {
        taskScheduler.scheduleWithFixedDelay(() -> {
            try {
                Thread.sleep(9000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("test");
        }, 1000L);
    }

I wait that each 1 second will start one thread from PoolSize(5), and after 5 tims pool will full and I will wait first free thread and it thread continue work.

But in real I see next:

2018-09-04 18:06:42.769  INFO 10128 --- [main] c.e.scheduling.SchedulingApplication : Started SchedulingApplication in 1.69 seconds (JVM running for 2.193)
2018-09-04 18:06:51.385  INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:01.387  INFO 10128 --- [Test-1] com.example.scheduling.MyScheduler : test
2018-09-04 18:07:11.389  INFO 10128 --- [Test-2] com.example.scheduling.MyScheduler : test

One thread print test each 9 seconds

EDIT:

I tested

scheduleAtFixedRate - the result is the same

EDIT2:

@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(this::test2, 1000L);
}

@Async
public void test2() {
    try {
        Thread.sleep(9000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("test");
}

@EnableAsync
@EnableScheduling
@Configuration
public class JavaConfig {

Not helped:

2018-09-05 10:31:40.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:31:49.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:31:58.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:07.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:16.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:25.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:34.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:43.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:32:52.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:01.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:10.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test
2018-09-05 10:33:19.160  INFO 13636 --- [         Test-3] com.example.scheduling.MyScheduler       : test

Upvotes: 0

Views: 1926

Answers (2)

ip696
ip696

Reputation: 7104

Thanks to prompts @Sun I found solution:

@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(testBean::test, 1000L);
}

and move method to another class because I use proxy by default:

@Slf4j
@Component
public class TestBean {

    @Async
    public void test(){
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("hz");
    }
}

And Put @EnableAsync on my configuration class

Upvotes: 0

xingbin
xingbin

Reputation: 28289

You need make it Async if you want to execute the task even when there is one running, for example:

@PostConstruct
public void test() {
    taskScheduler.scheduleAtFixedRate(this::makeLog, 1000);
}

@Async
public void makeLog() {
    try {
        Thread.sleep(9000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("test");
}

Upvotes: 1

Related Questions