多线程
Java多线程相关
李_杰
这个作者很懒,什么都没留下…
展开
-
取消一个正在运行中的任务(ThreadPoolExecutor)
文章目录代码结果代码public class TestThreadPoolExecutor2 { public static void main(String[] args) { /**********************************参数begin**********************************/ // 核心线程数 int corePoolSize = 5; // 最大线程数 i原创 2022-04-22 17:47:45 · 939 阅读 · 0 评论 -
构造一个使用特定名称线程的线程池(ThreadPoolExecutor)
文章目录代码结果代码public class TestThreadPoolExecutor { public static void main(String[] args) { /**********************************参数begin**********************************/ // 核心线程数 int corePoolSize = 5; // 最大线程数 in原创 2022-04-20 22:36:56 · 913 阅读 · 0 评论 -
基于阻塞队列实现生产者&消费者模式
文章目录代码代码public class TestProsumer { // 货架可存放商品数量 private Integer goodsShelfSize = 10 ; // 阻塞队列 private ArrayBlockingQueue queue = new ArrayBlockingQueue<Integer>(goodsShelfSize); public static void main(String[] args) {原创 2022-04-17 15:40:23 · 295 阅读 · 0 评论 -
基于CountDownLatch实现多个子任务执行结果汇总
文章目录1、未使用 CountDownLatch结果2、使用 CountDownLatch结果1、未使用 CountDownLatchpublic class TestCountDownLatch { public static void main(String[] args) throws InterruptedException { TestCountDown testCountDown = new TestCountDown(); Thread threa原创 2022-04-15 23:35:50 · 588 阅读 · 0 评论 -
基于原子类实现线程安全的计数器操作
文章目录1、普通变量线程不安全代码结果不符合预期2、AtomicInteger 实现代码结果符合预期1、普通变量线程不安全代码public class TestAtomicInteger { public static int count = 0; public static void main(String[] args) throws InterruptedException { TestCount testCount = new TestCount();原创 2022-04-15 22:26:10 · 398 阅读 · 0 评论 -
死锁Demo
文章目录1、demo2、结果1、demopublic class TestDeadLock { private static String a = "A"; private static String b = "B"; public static void deadLock () { Thread threadA = new Thread(new Runnable() { @Override public void原创 2022-03-31 12:01:10 · 103 阅读 · 0 评论 -
synchronized/ReentrantLock-实现多个线程间的数据同步Demo
文章目录1、未用锁情况2、使用synchronized3、使用ReentrantLock公平锁非公平锁1、未用锁情况demo public static void main(String[] args) { TestTicket testTicket = new TestTicket(); Thread threadA = new Thread(testTicket); Thread threadB = new Thread(testTicke原创 2022-03-31 11:54:50 · 117 阅读 · 0 评论 -
Java多线程(基础、线程安全、线程间通信)
文章目录一、基础1、多线程的优势和问题1. 优势2. 问题2、线程基础1. 启动方式2. Thread核⼼方法3. 扩展:sleep()和wait()的区别4. 线程状态3、线程安全1. 死锁2、synchronized1、synchronized依赖于底层的操作系统的Mutex Lock来实现的, JDK1.6之后做了较大的优化-锁升级机制。2、锁的内存语义3、锁释放和锁获取的内存语义4、synchronized用法5、扩展4、线程间通信1、volatile一、基础1、多线程的优势和问题1. 优势原创 2022-02-25 22:30:58 · 197 阅读 · 0 评论