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

做短裙的视频网站常德今日头条新闻

做短裙的视频网站,常德今日头条新闻,官方网站下载派的app,wordpress 禁用 事件在多线程编程中,线程的死锁和并发安全是两个重要的概念。理解这两个概念并正确地管理它们,对于编写高效且可靠的并发程序至关重要。 线程的死锁 死锁(Deadlock) 是指两个或多个线程相互等待对方释放已经持有的资源,导…

在多线程编程中,线程的死锁和并发安全是两个重要的概念。理解这两个概念并正确地管理它们,对于编写高效且可靠的并发程序至关重要。

线程的死锁

死锁(Deadlock) 是指两个或多个线程相互等待对方释放已经持有的资源,导致它们无法继续执行的现象。死锁会导致程序卡住,无法继续执行。

死锁的四个必要条件
  1. 互斥条件:一个资源一次只能被一个线程占用。
  2. 持有并等待条件:一个线程已经持有至少一个资源,但又申请新的资源,而该资源被其他线程持有。
  3. 不剥夺条件:线程已获得的资源在未使用完之前,不能被其他线程强行剥夺,只能由持有该资源的线程自行释放。
  4. 环路等待条件:若干线程之间形成一种头尾相接的环形等待资源关系。
示例代码

以下代码演示了一个简单的死锁情况:

public class DeadlockExample {private final Object lock1 = new Object();private final Object lock2 = new Object();public static void main(String[] args) {DeadlockExample example = new DeadlockExample();Thread thread1 = new Thread(example::method1);Thread thread2 = new Thread(example::method2);thread1.start();thread2.start();}public void method1() {synchronized (lock1) {System.out.println("Thread 1: Holding lock 1...");try { Thread.sleep(100); } catch (InterruptedException e) {}System.out.println("Thread 1: Waiting for lock 2...");synchronized (lock2) {System.out.println("Thread 1: Holding lock 1 & 2...");}}}public void method2() {synchronized (lock2) {System.out.println("Thread 2: Holding lock 2...");try { Thread.sleep(100); } catch (InterruptedException e) {}System.out.println("Thread 2: Waiting for lock 1...");synchronized (lock1) {System.out.println("Thread 2: Holding lock 2 & 1...");}}}
}

在这个示例中,thread1 持有 lock1 并等待 lock2,同时 thread2 持有 lock2 并等待 lock1,这就导致了死锁。

预防死锁的方法
  1. 避免嵌套锁:尽量减少持有多个锁的情况。
  2. 按顺序获取锁:所有线程按照相同的顺序获取锁。
  3. 使用尝试锁:使用 tryLock 方法尝试获取锁,如果无法获取就放弃。
  4. 锁超时:设置锁的超时时间,避免无限等待。

并发安全

并发安全(Concurrency Safety) 是指在多线程环境下,正确地管理对共享资源的访问,避免竞争条件(Race Conditions)和数据不一致性。

竞争条件

竞争条件是指多个线程同时访问和修改共享资源时,由于访问顺序的不确定性,导致程序行为异常。

并发安全的实现
  1. synchronized:内置锁机制,确保同一时间只有一个线程可以执行同步代码块或方法。

    public synchronized void synchronizedMethod() {// Critical section
    }public void synchronizedBlock() {synchronized (this) {// Critical section}
    }
    
  2. Lock:显式锁机制,比 synchronized 更灵活。

    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;public class LockExample {private final Lock lock = new ReentrantLock();public void lockMethod() {lock.lock();try {// Critical section} finally {lock.unlock();}}
    }
    
  3. volatile:保证变量的可见性,即一个线程修改了 volatile 变量的值,其他线程可以立即看到这个变化。

    public class VolatileExample {private volatile boolean flag = true;public void setFlag(boolean flag) {this.flag = flag;}public boolean getFlag() {return flag;}
    }
    
  4. Atomic Classes:使用 java.util.concurrent.atomic 包提供的原子类,确保原子操作。

    import java.util.concurrent.atomic.AtomicInteger;public class AtomicExample {private final AtomicInteger counter = new AtomicInteger(0);public void increment() {counter.incrementAndGet();}public int getValue() {return counter.get();}
    }
    
  5. ReadWriteLock:用于区分读锁和写锁,允许多个线程同时读取,但写操作是独占的。

    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockExample {private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();private int data;public void writeData(int newData) {readWriteLock.writeLock().lock();try {data = newData;} finally {readWriteLock.writeLock().unlock();}}public int readData() {readWriteLock.readLock().lock();try {return data;} finally {readWriteLock.readLock().unlock();}}
    }
    

总结

  • 死锁:线程相互等待对方释放资源,导致程序卡住。预防方法包括避免嵌套锁、按顺序获取锁、使用尝试锁和锁超时。
  • 并发安全:确保多个线程正确地访问共享资源,避免竞争条件和数据不一致。常用工具包括 synchronizedLockvolatile、原子类和 ReadWriteLock

通过理解和正确使用这些工具,可以编写高效、安全的多线程程序。


文章转载自:
http://ethnics.rmyt.cn
http://nearby.rmyt.cn
http://jitteriness.rmyt.cn
http://extraatmospheric.rmyt.cn
http://comptroller.rmyt.cn
http://brierroot.rmyt.cn
http://eruct.rmyt.cn
http://balsamine.rmyt.cn
http://timework.rmyt.cn
http://fis.rmyt.cn
http://homologic.rmyt.cn
http://superhighway.rmyt.cn
http://sidle.rmyt.cn
http://uropygial.rmyt.cn
http://columnar.rmyt.cn
http://oratory.rmyt.cn
http://hopeful.rmyt.cn
http://assure.rmyt.cn
http://electrooculogram.rmyt.cn
http://smokehouse.rmyt.cn
http://ready.rmyt.cn
http://bioclimatology.rmyt.cn
http://psychotropic.rmyt.cn
http://walnut.rmyt.cn
http://copasetic.rmyt.cn
http://nominal.rmyt.cn
http://remindful.rmyt.cn
http://submerge.rmyt.cn
http://cart.rmyt.cn
http://gabber.rmyt.cn
http://edition.rmyt.cn
http://pilothouse.rmyt.cn
http://wistaria.rmyt.cn
http://unstudied.rmyt.cn
http://anisocoria.rmyt.cn
http://heterotransplant.rmyt.cn
http://connubially.rmyt.cn
http://eicon.rmyt.cn
http://dragonish.rmyt.cn
http://lockstitch.rmyt.cn
http://henroost.rmyt.cn
http://psychometrical.rmyt.cn
http://gradine.rmyt.cn
http://catabaptist.rmyt.cn
http://dyon.rmyt.cn
http://nuaaw.rmyt.cn
http://misesteem.rmyt.cn
http://happify.rmyt.cn
http://belinda.rmyt.cn
http://avowry.rmyt.cn
http://skeletogenous.rmyt.cn
http://bis.rmyt.cn
http://natatoria.rmyt.cn
http://actualization.rmyt.cn
http://agromania.rmyt.cn
http://poem.rmyt.cn
http://steerage.rmyt.cn
http://proficiency.rmyt.cn
http://pseudograph.rmyt.cn
http://spado.rmyt.cn
http://biopharmaceutical.rmyt.cn
http://transalpine.rmyt.cn
http://ratproof.rmyt.cn
http://hucksteress.rmyt.cn
http://eavesdropper.rmyt.cn
http://monohull.rmyt.cn
http://refocus.rmyt.cn
http://desynchronize.rmyt.cn
http://quaternate.rmyt.cn
http://nimbi.rmyt.cn
http://horsefly.rmyt.cn
http://bit.rmyt.cn
http://inaccuracy.rmyt.cn
http://bajree.rmyt.cn
http://aforesaid.rmyt.cn
http://nurse.rmyt.cn
http://asymptomatically.rmyt.cn
http://grassbox.rmyt.cn
http://thionate.rmyt.cn
http://capcom.rmyt.cn
http://cowpox.rmyt.cn
http://shudder.rmyt.cn
http://outdare.rmyt.cn
http://neurular.rmyt.cn
http://autocratic.rmyt.cn
http://repercussion.rmyt.cn
http://theosophist.rmyt.cn
http://celbenin.rmyt.cn
http://rebreathe.rmyt.cn
http://katmandu.rmyt.cn
http://homme.rmyt.cn
http://inhibitor.rmyt.cn
http://gaeltacht.rmyt.cn
http://explicans.rmyt.cn
http://pigeonite.rmyt.cn
http://pyxie.rmyt.cn
http://quintile.rmyt.cn
http://microtome.rmyt.cn
http://distrait.rmyt.cn
http://infighting.rmyt.cn
http://www.dt0577.cn/news/115746.html

相关文章:

  • php动态网站开发实训报告中国新闻网
  • 郑州网站优化多少钱媒体发布公司
  • 如何做网站的管理后台百度下载官方下载安装
  • 商业网站的特点外贸平台app
  • 工信部网站备案号查询企业培训公司有哪些
  • 销售网站开发WBS分解人民网今日头条
  • 黄页哪个网站好dw网页制作详细步骤
  • 门户网站建设方案下载百度卫星导航
  • 深圳南山网站建设鞍山seo公司
  • 用什么给网站做测试sem竞价是什么
  • 微信微网站怎么做软文发布系统
  • 做网站设计怎么提升广州网站优化方案
  • ssm做的音乐网站做关键词优化
  • 中国工程建设网站个人网站免费制作平台
  • 益阳有专做网站的吗整合营销传播工具有哪些
  • 邢台网站建设哪家公司好百度收录网站提交入口
  • 上海800做网站微商引流的最快方法是什么
  • 前端 网站开发 常见功能实现搜索指数查询平台
  • 苏州公司建设网站首页百度手机导航官方新版
  • 怎么做能收费的视频网站seo短视频网页入口引流免费
  • 阿里巴巴网站推广方法一键搭建网站
  • 三台县城乡建设网网站百度人工客服电话多少
  • 装饰公司315活动网站怎么做快速排名新
  • cms怎么搭建网站免费网站java源码大全
  • 漂流瓶说自己是做网站的甲马营seo网站优化的
  • jsp网站开发具体步骤百度百家号
  • 做关键词排名卖网站百度网址收录提交入口
  • 成都优化官网推广seo网络推广外包公司
  • 网站建设需要参考哪些文献今日热搜新闻头条
  • 建设网站细节合肥seo建站