多线程编程

This commit is contained in:
罗祥
2019-11-18 09:31:03 +08:00
parent 2ffc0a3a94
commit 910790be8c
39 changed files with 1605 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.heibaiying.threadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程池的基本使用
*/
public class J1_ThreadPool {
static class Task implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在执行");
}
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
executorService.submit(new Task());
}
executorService.shutdown();
}
}

View File

@ -0,0 +1,46 @@
package com.heibaiying.threadPool;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 计划任务
*/
public class J2_ScheduledTask {
private static long cacheTime = System.currentTimeMillis();
static class Task implements Runnable {
private String type;
Task(String type) {
this.type = type;
}
@Override
public void run() {
try {
Thread.sleep(5000);
long nowTime = System.currentTimeMillis();
System.out.println(type + Thread.currentThread().getId() + "执行耗时" + (nowTime - cacheTime) + "毫秒");
cacheTime = nowTime;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
// 只执行一次
pool.schedule(new Task("schedule"), 2, TimeUnit.SECONDS);
// 指定2秒为固定周期执行如果项目执行耗时5秒则项目结束后立马执行下一次任务所以输出的时间间隔为5秒
pool.scheduleAtFixedRate(new Task("FixedRate"), 0, 2, TimeUnit.SECONDS);
// 总是在上一次项目结束后间隔指定周期执行所以项目耗时5秒还需要间隔2秒执行所以输出的时间间隔为7秒
pool.scheduleWithFixedDelay(new Task("WithFixedDelay"), 0, 2, TimeUnit.SECONDS);
// pool.shutdown();
}
}

View File

@ -0,0 +1,46 @@
package com.heibaiying.threadPool;
import java.util.concurrent.*;
public class J3_CustomThreadPool {
private static int i = 0;
private static CountDownLatch latch = new CountDownLatch(1000);
static class Task implements Runnable {
@Override
public void run() {
increase();
}
private void increase() {
synchronized (this) {
i++;
}
System.out.println(Thread.currentThread().getName() + "输出:" + i);
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
// 自定义线程池
ExecutorService executorService = new ThreadPoolExecutor(10, 20, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
r -> {
Thread thread = new Thread(r);
thread.setDaemon(true);
System.out.println("create" + thread.getName());
return thread;
});
Task task = new Task();
for (int i = 0; i < 1000; i++) {
executorService.submit(task);
}
latch.await();
System.out.println("最后的结果是" + i);
executorService.shutdown();
}
}

View File

@ -0,0 +1,55 @@
package com.heibaiying.threadPool;
import java.util.concurrent.*;
public class J4_ExtendedThreadPool {
private static int i = 0;
private static CountDownLatch latch = new CountDownLatch(1000);
static class Task implements Runnable {
@Override
public void run() {
increase();
}
private void increase() {
synchronized (this) {
i++;
}
System.out.println(Thread.currentThread().getName() + "输出:" + i);
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
// 自定义线程
ExecutorService executorService = new ThreadPoolExecutor(10, 20, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>()) {
@Override
protected void beforeExecute(Thread t, Runnable r) {
System.out.println("线程" + t.getName() + "准备执行");
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
System.out.println("线程" + r + "执行结束");
}
@Override
protected void terminated() {
System.out.println("线程池退出");
}
};
Task task = new Task();
for (int i = 0; i < 1000; i++) {
executorService.submit(task);
}
latch.await();
System.out.println("最后的结果是" + i);
executorService.shutdown();
}
}