多线程编程
This commit is contained in:
@ -0,0 +1,41 @@
|
||||
package com.heibaiying.countDown;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class J2_CountDown {
|
||||
|
||||
private static int number = 100;
|
||||
private static CountDownLatch latch = new CountDownLatch(number);
|
||||
private static AtomicInteger integer = new AtomicInteger(0);
|
||||
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 假设这是一个耗时的任务
|
||||
Thread.sleep(3000);
|
||||
integer.incrementAndGet();
|
||||
// 计数减一
|
||||
latch.countDown();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
IncreaseTask task = new IncreaseTask();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(100);
|
||||
for (int i = 0; i < number; i++) {
|
||||
executorService.submit(task);
|
||||
}
|
||||
latch.await();
|
||||
// 会等待所有任务执行完成再输出
|
||||
System.out.println("integer:" + integer);
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.heibaiying.countDown;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class j1_Normal {
|
||||
|
||||
private static AtomicInteger integer = new AtomicInteger(0);
|
||||
|
||||
static class IncreaseTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 假设这是一个耗时的任务
|
||||
Thread.sleep(3000);
|
||||
integer.incrementAndGet();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
IncreaseTask task = new IncreaseTask();
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(100);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
executorService.submit(task);
|
||||
}
|
||||
// 不会等待所有任务完成就输出,此时通常为0
|
||||
System.out.println("integer:" + integer);
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user