package com.example.async.test.controller;
import com.example.async.test.service.TestService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private final TestService testService;
public TestController(TestService testService){
this.testService = testService;
}
@GetMapping(value = "/test", produces = {MediaType.APPLICATION_JSON_VALUE})
public void test() {
for(int i = 0; i < 1000; i++) {
//testService.asyncPrint(i);
testService.asyncPrint4(i);
}
}
}
package com.example.async.test.service;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@Service
public class TestService {
private final Executor executor;
@Autowired
public TestService(@Qualifier("asyncThreadTaskExecutor") Executor executor) {
this.executor = executor;
}
@Async
public void asyncPrint(int value) {
//System.out.println("value : " + value);
asyncPrint2(value);
//asyncPrint3(value);
}
private void asyncPrint2(int value) {
System.out.println("value : " + value);
}
public void asyncPrint3(int value) {
System.out.println("value : " + value);
}
public void asyncPrint4(int value) {
CompletableFuture.runAsync(()-> asyncPrint2(value),executor);
}
}
'Java' 카테고리의 다른 글
자바 Aspect, Annotation으로 Log 만들기 (0) | 2021.02.22 |
---|---|
자바 외부 dll 연동하기 opencv (0) | 2021.01.20 |
자바 Async2 (0) | 2020.11.08 |
자바 Async1 (0) | 2020.11.08 |
자바 Callback (0) | 2020.11.03 |