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

济南专门做网站的公司如何做好网络宣传工作

济南专门做网站的公司,如何做好网络宣传工作,大连高新园区邮编,wordpress 显示指定分类文章1、线程安全 线程安全是指你的代码所在的进程中有多个线程同时运行,而这些线程可能会同时运行这段代码,如果每次运行的代码结果和单线程运行的结果是一样的,且其他变量的值和预期的也是一样的,那么就是线程安全的。 一个类或者程序…

1、线程安全

        线程安全是指你的代码所在的进程中有多个线程同时运行,而这些线程可能会同时运行这段代码,如果每次运行的代码结果和单线程运行的结果是一样的,且其他变量的值和预期的也是一样的,那么就是线程安全的。

        一个类或者程序所提供的接口对于线程来说是原子操作或者多个线程之间的切换不会导致该接口的执行结果存在二义性,也就是说我们不用考虑同步的问题。
        线程安全问题都是由全局变量及静态变量引起的。若每个线程中对全局变量、静态变量只有读操作,而无写操作,一般来说,这个全局变量是线程安全的;若有多个线程同时执行
写操作,一般都需要考虑线程同步,否则就可能影响线程安全。

Java中Vector,HashTable,StringBuffer和java.util.concurrent下的集合类都是线程安全的

2、非线程安全

        是不提供代码数据访问保护,可能出现多个线程先后访问更改数据造成所得的数据是脏数据。*(脏数据是表示一个数据已经被修改,但是还没有保存或进一步的处理。
        例如你操作数据库修改某一字段内容,在你修改了但还没commit时,另一线程在读取这
数据,他读取的就是你修改前的数据,但事实上你已经修改了,这就是脏数据了。)*在多个线程同时访问同一个对象时会发生数据错误 不完整等情况时 那就是线程不安全。

这里展示一段代码,全局变量count就是线程不安全,这个count变量可能同时被三个线程进行写操作。

Java中HashMap,ArrayList,StringBuilder都是线程不安全的。

    private static Integer count = new Integer(100);public void noSafeMethod() {while (count > 0) {Util.mySleep(100);count--;}Util.printfLog("count=" + count + ";threadid=" + Thread.currentThread().getName());}/*** 线程不安全,三个线程同时修改,会导致count多减*/private static void threadNoSafe() {ExecutorService executorService = Executors.newFixedThreadPool(10);ThreadSafeTest threadSafeTest = new ThreadSafeTest();executorService.submit(threadSafeTest::noSafeMethod);executorService.submit(threadSafeTest::noSafeMethod);executorService.submit(threadSafeTest::noSafeMethod);executorService.shutdown();}

3、线程安全出现的根本原因

  • 存在两个或者两个以上的线程对象共享同一个资源;
  • 多线程操作共享资源代码有多个语句。
  • 存在竞争的线程不安全,不存在竞争的线程就是安全的

4、实现线程安全

        实现线程安全常用的方式有悲观锁(互斥同步锁),乐观锁(非阻塞同步锁例如基于CAS机制),无同步方案等方式

 4.1 悲观锁(互斥同步锁)。

     悲观锁也叫互斥同步锁,是一种阻塞机制,当前线程通过对当前资源的独占,进而实现线程安全。

        悲观锁总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线程使用,其它线程阻塞,用完后再把资源转让给其它线程)。传统的关系型数据库里边就用到了很多这种锁机制,比如行锁,表锁等,读锁,写锁等,都是在做操作之前先上锁。Java中synchronized和ReentrantLock等独占锁就是悲观锁思想的实现。

1. synchronized

    private static Integer count2 = 100;private static Object object = new Object();public void synchronizedMethod() {while (count2 > 0) {synchronized (object) {Util.mySleep(Double.valueOf(Math.random() * 10).intValue());if (count2 > 0) {count2--;}}}Util.printfLog("synchronized:count2=" + count2 + ";threadname=" + Thread.currentThread().getName());}private static void threadSynchronized() {ExecutorService executorService = Executors.newFixedThreadPool(10);ThreadSafeTest threadSafeTest = new ThreadSafeTest();executorService.submit(threadSafeTest::synchronizedMethod);executorService.submit(threadSafeTest::synchronizedMethod);executorService.submit(threadSafeTest::synchronizedMethod);executorService.shutdown();}

当某一个线程同对count资源的独占,进而实现 线程安全,不会像不安全代码一样出现负值。

        2. ReentrantLock

    private static Integer countLock = new Integer(100);ReentrantLock reentrantLock = new ReentrantLock();public void reentrantLockMethod() {while (countLock > 0) {reentrantLock.lock();
//            reentrantLock.tryLock(3, TimeUnit.SECONDS);Util.mySleep(100);//当解锁之后线程可能获取到的还是旧值(countLock已经被其他的线程加成0了),// 所以还需要再次判断,不然会被线程多减一次成为-1if (countLock > 0) {countLock--;}reentrantLock.unlock();}Util.printfLog("countLock=" + countLock + ";threadname=" + Thread.currentThread().getName());}private static void threadReentrantLock() {ExecutorService executorService = Executors.newFixedThreadPool(10);ThreadSafeTest threadSafeTest = new ThreadSafeTest();executorService.submit(threadSafeTest::reentrantLockMethod);executorService.submit(threadSafeTest::reentrantLockMethod);executorService.submit(threadSafeTest::reentrantLockMethod);executorService.shutdown();}

3. 

3. Lock和synchronized对比 

1)synchronized是Java语言的关键字,因此是内置特性,Lock不是Java语言内置的,Lock是一个接口,通过实现类可以实现同步访问。

 2)synchronized是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常,JVM会自动释放锁定,但是使用Lock则不行,lock是通过代码实现的,要保证锁定一定会被释放,就必须将unLock()放到finally{}中

 3)在资源竞争不是很激烈的情况下,Synchronized的性能要优于ReetrantLock,但是在资源竞争很激烈的情况下,Synchronized的性能会下降几十倍,但是ReetrantLock的性能能维持常态。

 4)Lock 实现提供了比 synchronized 关键字 更广泛的锁操作,它能以更优雅的方式处理线程同步问题。

4.2 乐观锁(非阻塞同步锁)

        乐观锁是一种非阻塞锁。乐观锁总是假设最好的情况,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号机制和CAS算法实现。乐观锁适用于多读的应用类型,这样可以提高吞吐量,像数据库提供的类似于write_condition机制,其实都是提供的乐观锁。Java中java.util.concurrent.atomic包下面的原子变量类就是使用了乐观锁的一种实现方式CAS实现的。  

        乐观锁一般会使用版本号机制(在更新或者删除时where增加时间字段等条件)或CAS算法实现。  

1.原子类(CAS)    

JUC中提供了几个Automic类以及每个类上的原子操作就是乐观锁机制。AtomicBoolean, AtomicInteger,AtomicLong,AtomicLongArray,AtomicReference等原子类的类,主要用于在高并发环境下的高效程序处理,来帮助我们简化同步处理.

    private static AtomicInteger atomicInteger = new AtomicInteger(100);public void atomicIntegerMethod() {while (atomicInteger.get() > 0) {Util.mySleep(100);int oldValue = atomicInteger.get();if (oldValue <= 0) {break;}int newValue = oldValue - 1;if (!atomicInteger.compareAndSet(oldValue, newValue)) {
//                System.out.println("threadName=" + Thread.currentThread().getName() + ";oldValue=" + oldValue + ";oldvaluelatest=" + atomicInteger.get());}}Util.printfLog("atomicInteger=" + atomicInteger.get() + ";threadname=" + Thread.currentThread().getName());}/****/private static void threadAtomicInteger() {ExecutorService executorService = Executors.newFixedThreadPool(10);ThreadSafeTest threadSafeTest = new ThreadSafeTest();executorService.submit(threadSafeTest::atomicIntegerMethod);executorService.submit(threadSafeTest::atomicIntegerMethod);executorService.submit(threadSafeTest::atomicIntegerMethod);executorService.shutdown();}

 乐观锁和悲观锁的使用场景

        从上面对两种锁的介绍,我们知道两种锁各有优缺点,不可认为一种好于另一种,像乐观锁适用于写比较少的情况下(多读场景),即冲突真的很少发生的时候,这样可以省去了锁的开销,加大了系统的整个吞吐量。但如果是多写的情况,一般会经常产生冲突,这就会导致上层应用会不断的进行retry,这样反倒是降低了性能,所以一般多写的场景下用悲观锁就比较合适。

4.4 无同步方案

1.可重入代码

        在执行的任何时刻都可以中断-重入执行而不会产生冲突。特点就是不会依赖堆上的共享资源

2.ThreadLocal/Volaitile

        线程本地的变量,每个线程获取一份共享变量的拷贝,单独进行处理。SpringBoot中的HttpServletRequest就是使用ThreadLocal实现了每次并发请求,HttpServletRequest是线程安全的。

3.线程本地存储

        如果一个共享资源一定要被多线程共享,可以尽量让一个线程完成所有的处理操作,比如生产者消费者模式中,一般会让一个消费者完成对队列上资源的消费。典型的应用是基于请求-应答模式的web服务器的设计。

5. 死锁的定义

5.1 死锁的定义

 多线程以及多进程改善了系统资源的利用率并提高了系统 的处理能力。然而,并发执行也带来了新的问题——死锁。所谓死锁是指多个线程因竞争资源而造成的一种僵局(互相等待),若无外力作用,这些进程都将无法向前推进。

     死锁是指两个或两个以上的线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。

5.2 死锁产生的原因

1、系统资源的竞争

2、进程推进顺序非法

3、死锁产生的必要条件

(1)互斥条件,(2)不剥夺条件,(3)请求和保持条件,(4)循环等待条件

5.3 如何避免死锁

  • 加锁顺序(线程按照一定的顺序加锁)
  • 加锁时限线程尝试获取锁的时候加上一定的时限,超过时限则放弃对该锁的请求,并释放自己占有的锁)
  • 死锁检测

参考:

Java深入学习11:Lock锁详解

面试必备之乐观锁与悲观锁

Java多线程:死锁


文章转载自:
http://carlin.mnqg.cn
http://cathleen.mnqg.cn
http://lob.mnqg.cn
http://bimotored.mnqg.cn
http://horizontality.mnqg.cn
http://fluoridationist.mnqg.cn
http://shipmaster.mnqg.cn
http://holding.mnqg.cn
http://leniency.mnqg.cn
http://leafcutter.mnqg.cn
http://workhand.mnqg.cn
http://islamabad.mnqg.cn
http://featherwitted.mnqg.cn
http://cyp.mnqg.cn
http://xmas.mnqg.cn
http://vomity.mnqg.cn
http://hunan.mnqg.cn
http://flightworthy.mnqg.cn
http://scrutinous.mnqg.cn
http://laredo.mnqg.cn
http://leno.mnqg.cn
http://rubasse.mnqg.cn
http://pyrexic.mnqg.cn
http://fibrotic.mnqg.cn
http://leadbelly.mnqg.cn
http://removalist.mnqg.cn
http://brainteaser.mnqg.cn
http://piteous.mnqg.cn
http://autofining.mnqg.cn
http://anaesthetise.mnqg.cn
http://charmingly.mnqg.cn
http://absolute.mnqg.cn
http://draper.mnqg.cn
http://cheddar.mnqg.cn
http://bridge.mnqg.cn
http://cyclohexane.mnqg.cn
http://postilion.mnqg.cn
http://multifunctional.mnqg.cn
http://brighish.mnqg.cn
http://bardolater.mnqg.cn
http://uncontroverted.mnqg.cn
http://binge.mnqg.cn
http://unneutrality.mnqg.cn
http://tepee.mnqg.cn
http://wrestling.mnqg.cn
http://tba.mnqg.cn
http://schoolbag.mnqg.cn
http://tardive.mnqg.cn
http://ling.mnqg.cn
http://readjustment.mnqg.cn
http://reseed.mnqg.cn
http://hornworm.mnqg.cn
http://anthropophagi.mnqg.cn
http://lwop.mnqg.cn
http://unsubmissive.mnqg.cn
http://cerement.mnqg.cn
http://dustcloak.mnqg.cn
http://flickertail.mnqg.cn
http://hempen.mnqg.cn
http://semistrong.mnqg.cn
http://intimidatory.mnqg.cn
http://chocolaty.mnqg.cn
http://neckline.mnqg.cn
http://trichlorethylene.mnqg.cn
http://northwest.mnqg.cn
http://varlamoffite.mnqg.cn
http://resolvedly.mnqg.cn
http://polewards.mnqg.cn
http://earflap.mnqg.cn
http://prejudge.mnqg.cn
http://delighted.mnqg.cn
http://ureter.mnqg.cn
http://underpeopled.mnqg.cn
http://coagulative.mnqg.cn
http://touchy.mnqg.cn
http://nymphean.mnqg.cn
http://devote.mnqg.cn
http://trichiasis.mnqg.cn
http://allotropy.mnqg.cn
http://infix.mnqg.cn
http://catholic.mnqg.cn
http://alkalescence.mnqg.cn
http://direful.mnqg.cn
http://streaked.mnqg.cn
http://unclos.mnqg.cn
http://copperskin.mnqg.cn
http://moulmein.mnqg.cn
http://ely.mnqg.cn
http://evasive.mnqg.cn
http://boliviano.mnqg.cn
http://nucleal.mnqg.cn
http://mensurable.mnqg.cn
http://ventripotent.mnqg.cn
http://stillbirth.mnqg.cn
http://sextans.mnqg.cn
http://rabbit.mnqg.cn
http://nwbn.mnqg.cn
http://revolt.mnqg.cn
http://delectate.mnqg.cn
http://marquess.mnqg.cn
http://www.dt0577.cn/news/78478.html

相关文章:

  • 怎么做北京赛网站培训网站制作
  • 网站关键词数量减少制作网页的软件有哪些
  • 佛山专业英文网站建设公司网站设计制作
  • 深圳品牌网站制作公司哪家好百度公司官网招聘
  • 怎样设计网站软文推广500字
  • 龙华公司做网站正规app推广
  • 做网站的框架组合seo博客大全
  • 温州网站建设公司有哪些百度资源平台
  • 专业做家具的网站四川成都最新消息
  • 做网站多少钱一张页面品牌推广策略与方式
  • 网站制作和维护费用男生和女生在一起探讨人生软件
  • 开发公司给物业公司的通知函手机网络优化
  • 做原型的网站google关键词查询工具
  • 网站建设 域名 数据库武汉seo网站
  • 昌吉做58网站的yandex搜索引擎
  • 推荐网站建设服务器南京百度seo排名优化
  • 用html5做的静态网站网站营销宣传图片
  • 网站开发属于无形资产吗玉林seo
  • 从哪些方面进行网站建设站长工具果冻传媒
  • 一个几个人做网站的几个故事电影高明搜索seo
  • 桌面应用程序开发seo网络优化师就业前景
  • 慈善机构网站建设报价百度优化排名
  • 深圳做网站得外包公司有哪些2021年十大热点事件
  • 外贸可以什么网站做广州百度推广电话
  • 公司简介通用模板seo sem
  • 佛山建站公司模板宁波seo网络优化公司
  • t想学网站建设做灰色词seo靠谱
  • 免费咨询电脑维修优化防控措施
  • 提供网站建设seo线下培训机构
  • 个人交互网站设计一个简单的网页