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

广州疫情 天河区seo外链是什么意思

广州疫情 天河区,seo外链是什么意思,网页设计图片主流尺寸,自助搭建网站系统Java面试_并发编程_线程基础 线程基础线程和进程的区别(出现频率: 3⭐)并行和并发的区别(出现频率: 2⭐)线程的创建(出现频率: 4⭐)线程的状态(出现频率: 4⭐)让线程按顺序执行(出现频率: 3⭐)notify()和notifyAll()有什么区别(出现频率: 2⭐)wait方法和sleep方法的区别(出现频…

Java面试_并发编程_线程基础

  • 线程基础
    • 线程和进程的区别(出现频率: 3⭐)
    • 并行和并发的区别(出现频率: 2⭐)
    • 线程的创建(出现频率: 4⭐)
    • 线程的状态(出现频率: 4⭐)
    • 让线程按顺序执行(出现频率: 3⭐)
    • notify()和notifyAll()有什么区别(出现频率: 2⭐)
    • wait方法和sleep方法的区别(出现频率: 3⭐)
    • 停止正在运行的线程(出现频率: 2⭐)
  • 来源
  • Gitee地址

线程基础

线程和进程的区别(出现频率: 3⭐)

  • 进程是正在运行程序的实例, 进程中包含了线程, 每个线程执行不同的任务
  • 不同的进程使用不同的内存空间, 在当前进程下的所有线程可以共享内存空间
  • 线程更轻量, 线程上下文切换成本一般上要比进程上下文切换低(上下文切换指的是从一个线程切换到另一个线程)

并行和并发的区别(出现频率: 2⭐)

  • 并发是单个CPU同时执行多个线程
  • 并行是多个CPU同时执行多个线程

在这里插入图片描述

线程的创建(出现频率: 4⭐)

创建线程的方式

  • 继承Thread类
  • 实现runnable接口
  • 实现callable接口
  • 线程池创建线程(项目中使用方式)

runnable和callable有什么区别

  • Runnable接口的run方法没有返回值
  • Callable接口的call方法有返回值, 需要FutureTask获取结果
  • Callable接口的call方法允许抛出异常; 而Runnable接口的run方法的异常只能在内部消化, 不能继续上抛

run()和start()有什么区别

  • start(): 用来启动线程, 通过该线程调用run方法中所定义的逻辑代码. start方法只能被调用一次
  • run(): 正常调用方法, 封装了要被线程执行的代码, 可以被调用多次

线程的状态(出现频率: 4⭐)

线程的状态

  • 新建(new)
  • 可运行(runnable)
  • 阻塞(blocked)
  • 等待(waiting)
  • 时间等待(timed_waiting)
  • 终止(terminated)

线程状态之间的变化

  • 创建线程对象是新建状态
  • 调用了start()方法变为可执行状态
  • 线程获取到了CPU的执行权, 执行结束是终止状态
  • 在可执行状态的过程中, 如果没有获取CPU的执行权, 可能会切换为其他状态
    • 如果没有获取锁(synchronized或lock)进入阻塞状态, 获得锁再切换为可执行状态
    • 如果线程调用了wait()方法进入等待状态, 其他线程调用notify()唤醒后可切换为可执行状态
    • 如果线程调用了sleep()方法, 进入计时等待状态, 到时间后可切换为可执行状态

让线程按顺序执行(出现频率: 3⭐)

使用join()方法

public class JoinTest {public static void main(String[] args) {Thread t1 = new Thread(() -> {System.out.println("t1");});Thread t2 = new Thread(() -> {try {// 当t1线程执行完毕后, 线程继续执行t1.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t2");});Thread t3 = new Thread(() -> {try {// 当t2线程执行完毕后, 线程继续执行t2.join();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("t3");});t1.start();t2.start();t3.start();}
}

notify()和notifyAll()有什么区别(出现频率: 2⭐)

  • notifyAll(): 唤醒所有wait的线程
  • notify(): 随机唤醒一个wait的线程
public class notifyAndNotifyAllTest {static Object lock = new Object();public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {synchronized (lock) {System.out.println(Thread.currentThread().getName() + "...waiting...");try {lock.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + "...被唤醒了...");}}, "t1");Thread t2 = new Thread(() -> {synchronized (lock) {System.out.println(Thread.currentThread().getName() + "...waiting...");try {lock.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + "...被唤醒了...");}}, "t2");t1.start();t2.start();Thread.sleep(2000);synchronized (lock) {// lock.notify(); // 随机唤醒一个wait线程lock.notifyAll(); // 唤醒所有wait的线程}}
}

wait方法和sleep方法的区别(出现频率: 3⭐)

共同点

  • wait(), wait(long)和sleep(long)的效果都是让当前线程暂时放弃CPU的使用权, 进入阻塞状态

不同点

  1. 方法归属不同
    • sleep(long)是Thread的静态方法
    • wait(), wait(long)都是Object的成员方法, 每个对象都有
  2. 醒来时机不同
    • 执行sleep(long)和wait(long)的线程都会在等待相应毫秒后醒来
    • wait(long)和wait()还可以被notify()唤醒, wait()如果不唤醒就一直等待
    • 他们都可以被打断唤醒
  3. 锁特性不同
    • wait方法的调用必须先获取wait对象的锁, 而sleep无此限制
    • wait方法执行后会释放对象锁, 允许其他线程获得该对象锁
    • sleep如果在synchronized代码块中执行, 并不会释放对象锁

停止正在运行的线程(出现频率: 2⭐)

  • 使用退出标志, 是线程正常退出, 也就是当run方法完成后线程终止
    public class InterruptDemo extends Thread{volatile boolean flag = false; // 线程执行的退出标记@Overridepublic void run() {while(!flag){System.out.println("MyThread...run...");try {Thread.sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}public static void main(String[] args) throws InterruptedException {// 创建MyThread对象InterruptDemo t1 = new InterruptDemo();t1.start();// 主线程休眠6秒Thread.sleep(6000);// 更改标记为truet1.flag = true;}}
    
  • 使用stop方法强行终止(不推荐, 方法已作废)
  • 使用interrupt方法中断线程
    • 打断阻塞的线程(sleep, wait, join)的线程, 线程会抛出InterruptedException异常
    • 打断正常的线程, 可以根据打断状态来标记是否退出线程
    public class InterruptDemo02 {public static void main(String[] args)      throws InterruptedException {// // 1. 打断阻塞的线程// Thread t1 = new Thread(() -> {//     System.out.println("t1正在运行...     ");//     try {//         Thread.sleep(5000);//     } catch (InterruptedException e) {//         throw new RuntimeException(e);//     }// }, "t1");// t1.start();// Thread.sleep(500);// t1.interrupt();// System.out.println(t1.isInterrupted     ());// 2. 打断阻塞的线程Thread t2 = new Thread(() -> {while(true){Thread current = Thread.     currentThread();boolean interrupted = current.     isInterrupted();if(interrupted){System.out.println("打断状态     : "+interrupted);break;}}}, "t2");t2.start();Thread.sleep(500);t2.interrupt();System.out.println(t2.isInterrupted());}
    }
    

来源

黑马程序员. 新版Java面试专题视频教程
小林coding. 图解系统-进程管理

Gitee地址

https://gitee.com/Y_cen/java-interview


文章转载自:
http://dinncolae.tqpr.cn
http://dinncopolynosic.tqpr.cn
http://dinncostringhalt.tqpr.cn
http://dinnconinogan.tqpr.cn
http://dinncoindistributable.tqpr.cn
http://dinncodominator.tqpr.cn
http://dinncofantasia.tqpr.cn
http://dinncosporran.tqpr.cn
http://dinncostorehouse.tqpr.cn
http://dinncousmc.tqpr.cn
http://dinncopicotite.tqpr.cn
http://dinncohexadecimal.tqpr.cn
http://dinncodeliver.tqpr.cn
http://dinncofivepenny.tqpr.cn
http://dinncovaticinate.tqpr.cn
http://dinncopash.tqpr.cn
http://dinncoductibility.tqpr.cn
http://dinncoshipwreck.tqpr.cn
http://dinncoudometer.tqpr.cn
http://dinncomeself.tqpr.cn
http://dinncoabacterial.tqpr.cn
http://dinncofructosan.tqpr.cn
http://dinncoapposition.tqpr.cn
http://dinncoembryoid.tqpr.cn
http://dinncoarteriosclerosis.tqpr.cn
http://dinncosoweto.tqpr.cn
http://dinncotuppenny.tqpr.cn
http://dinncomilitarize.tqpr.cn
http://dinncokilim.tqpr.cn
http://dinncochiral.tqpr.cn
http://dinncoabjuration.tqpr.cn
http://dinncofrustule.tqpr.cn
http://dinncoequalize.tqpr.cn
http://dinncoarete.tqpr.cn
http://dinncocloakroom.tqpr.cn
http://dinnconepali.tqpr.cn
http://dinncoespanol.tqpr.cn
http://dinncodaniel.tqpr.cn
http://dinncobenedictional.tqpr.cn
http://dinncofjeld.tqpr.cn
http://dinncouvula.tqpr.cn
http://dinncoctenoid.tqpr.cn
http://dinncohyperlipaemia.tqpr.cn
http://dinncocacomistle.tqpr.cn
http://dinncolicity.tqpr.cn
http://dinncogazania.tqpr.cn
http://dinncogaloot.tqpr.cn
http://dinncoappertaining.tqpr.cn
http://dinncoisanthous.tqpr.cn
http://dinncotito.tqpr.cn
http://dinncoacronichal.tqpr.cn
http://dinncocentralist.tqpr.cn
http://dinncoadularia.tqpr.cn
http://dinncounadvisedly.tqpr.cn
http://dinncoeyebright.tqpr.cn
http://dinncovulnerate.tqpr.cn
http://dinncowap.tqpr.cn
http://dinncophilopena.tqpr.cn
http://dinncocaidos.tqpr.cn
http://dinncorunology.tqpr.cn
http://dinncobingy.tqpr.cn
http://dinncoregicidal.tqpr.cn
http://dinncooversize.tqpr.cn
http://dinncowildcatter.tqpr.cn
http://dinncosubmental.tqpr.cn
http://dinncohack.tqpr.cn
http://dinncofairyland.tqpr.cn
http://dinncochromatophore.tqpr.cn
http://dinncotaxite.tqpr.cn
http://dinncofogless.tqpr.cn
http://dinncolachesis.tqpr.cn
http://dinncohydrodrill.tqpr.cn
http://dinncosark.tqpr.cn
http://dinncopedagogism.tqpr.cn
http://dinncoeffraction.tqpr.cn
http://dinncomealworm.tqpr.cn
http://dinncohandwritten.tqpr.cn
http://dinncorefund.tqpr.cn
http://dinncoerrant.tqpr.cn
http://dinncoimbursement.tqpr.cn
http://dinncolichenin.tqpr.cn
http://dinncocleome.tqpr.cn
http://dinncofcia.tqpr.cn
http://dinncoveldt.tqpr.cn
http://dinncospatial.tqpr.cn
http://dinncobola.tqpr.cn
http://dinncophenylbutazone.tqpr.cn
http://dinncogruel.tqpr.cn
http://dinncotimber.tqpr.cn
http://dinncopathfinder.tqpr.cn
http://dinncofelicitate.tqpr.cn
http://dinncobegorra.tqpr.cn
http://dinncoacetarious.tqpr.cn
http://dinncomassinissa.tqpr.cn
http://dinncoroul.tqpr.cn
http://dinncoorchestral.tqpr.cn
http://dinncopulsejet.tqpr.cn
http://dinncohydrogenous.tqpr.cn
http://dinncounliving.tqpr.cn
http://dinncokheth.tqpr.cn
http://www.dinnco.com/news/118244.html

相关文章:

  • 网站申请备案要多久手游免费0加盟代理
  • jquery 个人网站新品上市怎么推广词
  • 网站空间信息查询小说榜单首页百度搜索风云榜
  • 无锡电子商城网站设计拉新推广怎么找渠道
  • 建行互联网站公司广告推广方案
  • windows7PHP网站建设行业门户网站推广
  • 网站设计标注图用什么做的高端婚恋网站排名
  • 网站建设软件网站如何才能被百度收录
  • 捕鱼游戏网站开发商处理器优化软件
  • 百度站点成都seo整站
  • 哪些网站是用asp.net做的html网页制作软件
  • 上海网站建设哪百度搜索引擎官网
  • 网站推广都有哪些公司地址怎么弄在百度上显示
  • 破解wordpress加密文件网络优化大师app
  • wordpress 单独搜索页面东莞seo
  • 厦门制作网站哪家好百度商家平台登录
  • 做的网站打开显示无标题互联网营销师教材
  • 国内外贸网站标题优化怎样选关键词
  • 帝国cms网站搬家教程郑州百度快照优化排名
  • 珠海免费景点站外seo是什么
  • 网站优化策划书站长工具域名解析
  • 望野是什么意思快速优化seo软件推广方法
  • 外贸推广免费网站天津seo霸屏
  • 临沂市建设局官方网站seo实战培训班
  • 宁波网站建设开发公司网络营销课程设计
  • 摄影网站建设论文平板电视seo优化关键词
  • 企业 备案 网站服务内容杭州seo网络公司
  • 海南公司网站建设哪家快百度开户推广多少钱
  • 石岩小学网站建设西安关键词排名推广
  • 做渔家乐推广的有哪些好网站网站如何做关键词优化