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

易班网站建设的意义七牛镜像存储wordpress

易班网站建设的意义,七牛镜像存储wordpress,网易企业邮箱官网入口,wordpress php 链接地址文章目录 前言三个线程,交替打印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/802699.html

相关文章:

  • 企业网站开发技术有哪些wordpress 主机
  • 泰州网站整站优化重庆网站空间费用
  • 山东专业网站seo常德小学报名网站
  • 沈阳制作网站软文广告经典案例300字
  • 手机企业网站开发怎么登陆网站后台管理系统
  • 做论坛网站能赚钱吗龙岩网红隧道在哪
  • 5 电子商务网站建设的步骤两个网站共用一个数据库
  • 宿迁做企业网站校园交易网站建设论文
  • 网站访问速度慢的原因页面制作多少钱
  • h5网站开发工具有哪些在线课堂网站开发
  • 网站做的好有什么用提升学历的重要性与意义
  • 要怎么做自己的网站视频教学网站集约化建设报告
  • 化妆品品牌网站建设设计网站的制作框架
  • 济南建站公司哪有装修公司网站建设
  • 做旅行网站的意义百度网页制作
  • 昌平网站开发公司一键logo生成器
  • 同城做推广哪个网站好如何进行搜索引擎优化
  • ps做网站的视频自己制作游戏的软件
  • 网站制作五个界面门户网站建设自评报告
  • 做的好的学校网站上海市建筑工程有限公司
  • 南昌网站搭建公司 赣ICP免费咨询的英文
  • 电脑编程网站接网站建设单子的网站
  • 网站后台改版面做网站都需要什么人团
  • 网站辅助导航关于公司网站开发的事项
  • 网站建设指的是什么网站建设点击打开指定网页
  • 公司网站上传图片动易医院网站管理系统
  • 网站建设ppt方案如何是网站排名上升
  • 沭阳县建设局网站自己做的网站能干站什么
  • 如何自己做的网站网页视频怎么下载到电脑桌面
  • 个人博客网站制作论文免费海报设计网站有哪些