并发
是否_
保持自律!
展开
-
创建线程
/* * 练习线程池的使用 * * 1. 共有 4 种线程池 * 2. execute() 和 submit() 都是用来执行任务,区别在于 submit() 有返回值 * 3. shutdown(), shutdownNow() 和 awaitTermination() * 4. Callable 执行任务有返回值 * */ public class ExecutorServiceTest { public static void main(String[] args) throws Exec.原创 2020-08-20 16:47:30 · 109 阅读 · 0 评论 -
Interrupt
public class InterruptTest { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread() { @Override public void run() { while (true) { // 判断是否中断.原创 2020-08-19 17:50:56 · 110 阅读 · 0 评论 -
Long 在多线程
/* * * 原子性 * * 在32位的虚拟机中,Long的赋值不是原子性的,可能会出现以下输出: * 4294966852 -4294967185 4294966852 4294966852 4294966852 * * */ public class MultiThreadLong { static long t = 0; public static class ChangeT implements Runnable{ long to; public.原创 2020-08-19 17:40:09 · 230 阅读 · 0 评论 -
重入锁
在 JDK 5.0 的早期版本中,重入锁的性能远远好于 synchronized,但从 JDK 6.0 开始,JDK 在 synchronized 上做了大量的优化,使得两者的性能差距不大。 例子: package lock; import java.util.concurrent.locks.ReentrantLock; public class ReenterLock implements Runnable { public static ReentrantLock lock = new原创 2020-08-11 22:13:38 · 68 阅读 · 0 评论