feat(新增): 线程练习
This commit is contained in:
parent
ef0035dd5f
commit
ee1c005f44
|
@ -14,7 +14,7 @@
|
|||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,20 @@
|
|||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Demo1 {
|
||||
/**
|
||||
* * runAsync
|
||||
* 不返回返回值
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
System.out.println("main begin....");
|
||||
CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
|
||||
System.out.println("当前线程:" + Thread.currentThread().getId());
|
||||
int result = 1024;
|
||||
System.out.println("result:" + result);
|
||||
}, executorService);
|
||||
System.out.println("main over....");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Demo2 {
|
||||
/**
|
||||
* * supplyAsync
|
||||
* 可以指定返回值
|
||||
*/
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
System.out.println("main begin....");
|
||||
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
System.out.println("当前线程:" + Thread.currentThread().getId());
|
||||
int result = 1024;
|
||||
System.out.println("result:" + result);
|
||||
return result;
|
||||
}, executorService);
|
||||
|
||||
// 获取返回结果
|
||||
Integer value = completableFuture.get();
|
||||
System.out.println("main over...." + value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Demo3 {
|
||||
// **whenComplete 和 whenCompleteAsync 的区别:**
|
||||
// whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务。
|
||||
// whenCompleteAsync:是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
|
||||
// 方法不以Async结尾,意味着Action使用相同的线程执行,而Async可能会使用其他线程执行(如果是使用相同的线程池,也可能会被同一个线程选中执行)
|
||||
public static void main(String[] args) {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
System.out.println("main begin....");
|
||||
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
System.out.println("当前线程:" + Thread.currentThread().getId());
|
||||
int result = 1024;
|
||||
System.out.println("result:" + result);
|
||||
return result;
|
||||
}, executorService).whenComplete((result, exception) -> {
|
||||
System.out.println("结果:" + result);
|
||||
System.out.println(exception.getMessage());
|
||||
});
|
||||
System.out.println("main over....");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Demo4 {
|
||||
// 只要上面的任务执行完成,就开始执行thenRun,只是处理完任务后,执行 thenRun的后续操作
|
||||
public static void main(String[] args) {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
|
||||
// 现成的串行执行
|
||||
CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {
|
||||
int value = 1024;
|
||||
System.out.println("1===>" + value);
|
||||
return 1024;
|
||||
});
|
||||
|
||||
CompletableFuture<Integer> futureB = futureA.thenApplyAsync((result) -> {
|
||||
System.out.println("2===>" + result);
|
||||
return result;
|
||||
}, executorService);
|
||||
|
||||
CompletableFuture<Integer> futureC = futureB.thenApplyAsync((result) -> {
|
||||
System.out.println("3===>" + result);
|
||||
return result;
|
||||
}, executorService);
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@Slf4j
|
||||
public class Demo5 {
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
|
||||
// 线程一 的异步执行
|
||||
CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {
|
||||
int count = 100;
|
||||
log.info("线程一:{}", count);
|
||||
return count;
|
||||
}, executorService);
|
||||
|
||||
// 线程二 异步执行
|
||||
CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> {
|
||||
System.out.println(Thread.currentThread().getName() + "--begin..");
|
||||
int res = 30;
|
||||
System.out.println("二:" + res);
|
||||
System.out.println(Thread.currentThread().getName() + "--over..");
|
||||
return res;
|
||||
}, executorService);
|
||||
|
||||
CompletableFuture<Void> all = CompletableFuture.allOf(futureA, futureB);
|
||||
all.get();
|
||||
System.out.println("over....");
|
||||
}
|
||||
}
|
|
@ -3,8 +3,8 @@ server:
|
|||
|
||||
bunny:
|
||||
rabbitmq:
|
||||
# host: 192.168.1.4
|
||||
host: 192.168.3.98
|
||||
host: 192.168.1.4
|
||||
# host: 192.168.3.98
|
||||
port: 5672
|
||||
username: bunny
|
||||
password: "02120212"
|
||||
|
|
|
@ -3,15 +3,15 @@ server:
|
|||
|
||||
bunny:
|
||||
rabbitmq:
|
||||
# host: 192.168.1.4
|
||||
host: 192.168.3.98
|
||||
host: 192.168.1.4
|
||||
# host: 192.168.3.98
|
||||
port: 5672
|
||||
username: bunny
|
||||
password: "02120212"
|
||||
|
||||
elasticsearch:
|
||||
#uris: http://192.168.1.4:9200
|
||||
uris: http://192.168.3.98:9200
|
||||
uris: http://192.168.1.4:9200
|
||||
# uris: http://192.168.3.98:9200
|
||||
|
||||
nacos:
|
||||
server-addr: z-bunny.cn:8848
|
||||
|
|
Loading…
Reference in New Issue