Java并发

This commit is contained in:
罗祥
2019-11-27 16:54:51 +08:00
parent fdb2e9404c
commit 7a257140b7
8 changed files with 472 additions and 22 deletions

View File

@ -8,6 +8,7 @@ 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);
@ -19,7 +20,7 @@ public class J2_CountDown {
// 假设这是一个耗时的任务
Thread.sleep(3000);
integer.incrementAndGet();
// 计数减一
// 计数器减1
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
@ -33,8 +34,8 @@ public class J2_CountDown {
for (int i = 0; i < number; i++) {
executorService.submit(task);
}
// 等待计数器为0时唤醒所有等待的线程
latch.await();
// 会等待所有任务执行完成再输出
System.out.println("integer" + integer);
executorService.shutdown();
}

View File

@ -4,6 +4,7 @@ import java.util.concurrent.Semaphore;
public class J1_Semaphore {
// 限制并发访问的线程的数量为5
private static Semaphore semaphore = new Semaphore(5);
static class IncreaseTask implements Runnable {

View File

@ -0,0 +1,26 @@
package com.heibaiying.stop;
/**
* 线程终止
*/
public class ThreadStop {
private static volatile boolean stopFlag = true;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (stopFlag) {
try {
Thread.sleep(100);
System.out.println("持续输出");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
Thread.sleep(3 * 1000);
stopFlag = false;
System.out.println("线程终止");
}
}

View File

@ -11,15 +11,22 @@ public class J1_ThreadPool {
static class Task implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行");
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + "正在执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
// 提交任务到线程池
executorService.submit(new Task());
}
// 关闭线程池此时不再接受新任务但仍会等待原有的任务执行完成如果想要立即关闭则可以使用shutdownNow()
executorService.shutdown();
}
}

View File

@ -33,12 +33,13 @@ public class J2_ScheduledTask {
}
public static void main(String[] args) {
// 为避免相互间的影响,以下各种场景最好分别测试:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
// 只执行一次
pool.schedule(new Task("schedule"), 2, TimeUnit.SECONDS);
// 指定2秒为固定周期执行如果项目执行耗时5秒项目结束立马执行下一次任务所以输出的时间间隔为5秒
// 指定2秒为固定周期执行因为项目执行耗时5秒此时项目结束立马执行下一次任务所以输出的时间间隔为5秒
pool.scheduleAtFixedRate(new Task("FixedRate"), 0, 2, TimeUnit.SECONDS);
// 总是在上一次项目结束后间隔指定周期执行,所以项目耗时5秒还需要间隔2秒执行所以输出的时间间隔为7秒
// 总是在上一次项目结束后间隔指定周期执行,因为项目耗时5秒还需要间隔2秒执行所以输出的时间间隔为7秒
pool.scheduleWithFixedDelay(new Task("WithFixedDelay"), 0, 2, TimeUnit.SECONDS);
// pool.shutdown();
}

View File

@ -7,7 +7,7 @@ public class J1_Normal {
private static int j = 0;
public static void main(String[] args) throws InterruptedException {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 100000; i++) {
j++;