当前位置: 首页 > wzjs >正文

乐陵市住房和城乡建设局网站百度浏览器

乐陵市住房和城乡建设局网站,百度浏览器,深圳做物流网站,网站后台配置文章目录 前言三个线程,交替打印A、B、C两个线程1~100交替输出奇数和偶数10个线程,每个线程1w,最终变量到达10w模拟死锁让三个线程怎么串行执行1.使用join方法2.使用CountDownLatch 前言 本文总结面试中常考的手撕多线程问题。 三个线程&am…

文章目录

  • 前言
  • 三个线程,交替打印A、B、C
  • 两个线程1~100交替输出奇数和偶数
  • 10个线程,每个线程+1w,最终变量到达10w
  • 模拟死锁
  • 让三个线程怎么串行执行
    • 1.使用join方法
    • 2.使用CountDownLatch

前言

本文总结面试中常考的手撕多线程问题。

三个线程,交替打印A、B、C

package com.fwedu.question_;/*** 三个线程交替打印A\B\C*/
public class Question3 {private static final Object LOCK = new Object();private static volatile int count = 0;private static final int MAX = 101;public static void main(String[] args) {new Thread(new Seq(0)).start();new Thread(new Seq(1)).start();new Thread(new Seq(2)).start();}static class Seq implements Runnable {private final int index;Seq(int index) {this.index = index;}@Overridepublic void run() {while (count < MAX) {synchronized (LOCK) {try {while (count % 3 != index) {LOCK.wait();}if (count <= MAX) {System.out.println("Thread-" + index + ":" + (char)('A' + count % 3));}count++;LOCK.notifyAll();} catch (InterruptedException e) {e.printStackTrace();}}}}}
}

两个线程1~100交替输出奇数和偶数

package com.fwedu.question_;import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;/*** 两个线程1~100交替输出奇数和偶数*/
public class Question2 {private static CountDownLatch cl = new CountDownLatch(2);private static final Lock lock = new ReentrantLock();private static int cnt = 1;private static final int maxCnt = 100;public static void main(String[] args) throws InterruptedException {ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1));threadPoolExecutor.execute(() -> {while (cnt <= maxCnt) {lock.lock();try {if (cnt <= maxCnt && cnt % 2 == 0) {System.out.println(Thread.currentThread() + " " + cnt);cnt++;}} finally {lock.unlock();}}cl.countDown();});threadPoolExecutor.execute(() -> {while (cnt <= maxCnt) {lock.lock();try {if (cnt <= maxCnt && cnt % 2 != 0) {System.out.println(Thread.currentThread() + " " + cnt);cnt++;}} finally {lock.unlock();}}cl.countDown();});cl.await();threadPoolExecutor.shutdown();}
}

10个线程,每个线程+1w,最终变量到达10w

package com.fwedu.question_;/*** 10个线程,每个线程+1w,最终变量到达10w*/
public class Question1 {public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 10; ++i) {new Thread(new T()).start();}Thread.sleep(100);  // 确保上面的代码都执行完了System.out.println(T.val);}
}class T extends Thread {static long val = 0;long cnt = 0;@Overridepublic void run() {while (cnt < 100) {cnt++;synchronized (T.class) {val++;}System.out.println(Thread.currentThread() + " " + cnt + " " + val);}}
}

模拟死锁

package com.fwedu.syn;/*** @author 冯威*/
public class DeadLock {public static void main(String[] args) {//模拟死锁现象DeadLockDemo A = new DeadLockDemo(true);A.setName("A 线程");DeadLockDemo B = new DeadLockDemo(false);B.setName("B 线程");A.start();B.start();}
}class DeadLockDemo extends Thread {static Object o1 = new Object();static Object o2 = new Object();boolean flag;public DeadLockDemo(boolean flag) {this.flag = flag;}@Overridepublic void run() {if (flag) {synchronized (o1) {System.out.println(Thread.currentThread().getName() + " 进入 1");synchronized (o2) {System.out.println(Thread.currentThread().getName() + " 进入 2");}}} else {synchronized (o2) {System.out.println(Thread.currentThread().getName() + " 进入 3");synchronized (o1) {System.out.println(Thread.currentThread().getName() + " 进入 4");}}}}
}

让三个线程怎么串行执行

1.使用join方法

public class SerialExecutionUsingJoin {public static void main(String[] args) throws InterruptedException {Thread thread1 = new Thread(() -> {System.out.println("线程 1 执行");});Thread thread2 = new Thread(() -> {try {thread1.join();System.out.println("线程 2 执行");} catch (InterruptedException e) {e.printStackTrace();}});Thread thread3 = new Thread(() -> {try {thread2.join();System.out.println("线程 3 执行");} catch (InterruptedException e) {e.printStackTrace();}});thread1.start();thread2.start();thread3.start();}
}

2.使用CountDownLatch

import java.util.concurrent.CountDownLatch;public class SerialExecutionUsingCountDownLatch {public static void main(String[] args) {CountDownLatch latch1 = new CountDownLatch(1);CountDownLatch latch2 = new CountDownLatch(1);Thread thread1 = new Thread(() -> {System.out.println("线程 1 执行");latch1.countDown();});Thread thread2 = new Thread(() -> {try {latch1.await();System.out.println("线程 2 执行");latch2.countDown();} catch (InterruptedException e) {e.printStackTrace();}});Thread thread3 = new Thread(() -> {try {latch2.await();System.out.println("线程 3 执行");} catch (InterruptedException e) {e.printStackTrace();}});thread1.start();thread2.start();thread3.start();}
}
http://www.dtcms.com/wzjs/194430.html

相关文章:

  • 建筑设计案例网站湖北seo网站推广
  • 沈阳市网站制作河南整站百度快照优化
  • 网络营销自学课程山东seo百度推广
  • 国外做外链常用的网站谷歌seo优化推广
  • 中国人在国外做赌博网站代理百度文库网页版登录入口
  • 网站如何做浏览量海外新闻app
  • wordpress下拉南京 seo 价格
  • 企业州建设银行网站美国婚恋网站排名
  • 装修网络接单平台seo网络排名优化方法
  • 化妆品网站建设网站互联网营销师国家职业技能标准
  • 黄山网站建设方案百度首页的ip地址
  • 网站的锚点链接怎么做网络广告策划书模板范文
  • html5的广泛应用网站页面seo
  • 广州网站建设外包建站系统cms
  • 蚌埠网站设计推广有奖励的app平台
  • 东营城镇建设规划网站网站关键词快速排名软件
  • 网络营销自己做网站快速建站平台
  • 网站建设氺金手指排名14韶关今日头条新闻
  • 深圳电信网络建站郑州网络营销公司哪个好
  • wordpress站点搬家推广平台app
  • 个人备案经营网站备案国外网站开发
  • 看室内设计案例的网站模板建站哪个平台好
  • 网站建设公司财务预算关键词是怎么排名的
  • 网页与网站的区别怎么在网上做广告
  • 合肥网站建设企业百度网站大全
  • 眼镜网站怎么做竞价班级优化大师使用指南
  • 邢台如何做企业网站全网投放广告的渠道有哪些
  • 网站建设报价表格式百度商店应用市场
  • 百度哪个网站做贸易上海关键词排名优化怎样
  • 做网站构思seo算法