34 lines
659 B
Java
34 lines
659 B
Java
|
|
package mmk.test.SpringMultiThread;
|
||
|
|
|
||
|
|
import java.util.concurrent.CompletableFuture;
|
||
|
|
|
||
|
|
import org.springframework.scheduling.annotation.Async;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
|
||
|
|
@Component
|
||
|
|
public class AppRepo1 {
|
||
|
|
|
||
|
|
@Async
|
||
|
|
public CompletableFuture<Integer> task() {
|
||
|
|
int result = 0;
|
||
|
|
|
||
|
|
for (int i = 0; i < 50; i++) {
|
||
|
|
System.out.println("Thread-1: " + i);
|
||
|
|
|
||
|
|
try {
|
||
|
|
Thread.sleep(1_000);
|
||
|
|
} catch (InterruptedException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
|
||
|
|
result += i;
|
||
|
|
}
|
||
|
|
|
||
|
|
System.out.println("Thread-1: " + result);
|
||
|
|
|
||
|
|
return CompletableFuture.completedFuture(result);
|
||
|
|
//return new AsyncResult<Integer>(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|