通俗易懂happens-before理解

10 篇文章 0 订阅

在JMM(JAVA内存模型)中,如果一个操作执行的结果需要对另一个操作可见,那么这两个操作之间必须存在happens-before关系。

JMM可以通过happens-before关系向程序员提供跨线程的内存可见性保证(如果A线程的写操作a与B线程的读操作b之间存在happensbefore关系,尽管a操作和b操作在不同的线程中执行,但JMM向程序员保证a操作将对b操作可见)。 

happens-before原则:

  • 如果一个操作happens-before另一个操作,那么第一个操作的执行结果将对第二个操作可见,而且第一个操作的执行顺序排在第二个操作之前。 
  • 两个操作之间存在happens-before关系,并不意味着一定要按照happens-before原则制定的顺序来执行。如果重排序之后的执行结果与按照happens-before关系来执行的结果一致,那么这种重排序并不非法。

happens-before规则:

  • 程序次序规则:一个线程内,按照代码顺序,书写在前面的操作先行发生于书写在后面的操作;
  • 锁定规则:一个unLock操作先行发生于后面对同一个锁额lock操作;
  • volatile变量规则:对一个volatile域的写,happens-before于任意后续对这个volatile域的读
  • 传递规则:如果A happens-before B,且B happens-before C,那么A happens-before C
  • 线程启动规则:如果线程A执行操作ThreadB.start()(启动线程B),那么A线程的ThreadB.start()操作happens-before于线程B中的任意操作
  • 线程中断规则:对线程interrupt()方法的调用先行发生于被中断线程的代码检测到中断事件的发生;
  • 线程终结规则:线程中所有的操作都先行发生于线程的终止检测,我们可以通过Thread.join()方法结束、Thread.isAlive()的返回值手段检测到线程已经终止执行;
  • 对象终结规则:一个对象的初始化完成先行发生于他的finalize()方法的开始;

通俗解释:

第一次接触该概念是在 方腾飞写的<<JAVA并发编程的艺术>>这本书,  初次接触感觉整章概念比较多, 有些晦涩, 在反复阅读和求助大神后有了一些理解, 记录在这

这条happens-before规则让人费解的地方就是happens-before规则的描述到底和谁在前边谁先执行有什么区别

<<JAVA并发编程的艺术>>有一句注意: 两个操作之间具有happens-before关系,并不意味着前一个操作必须要在后一个操作之前执行happens-before仅仅要求前一个操作(执行的结果)对后一个操作可见,且前一个操作按顺序排在第二个操作之前

这句话就是对这个问题的解释, 但也是让我最费解的一句话, 我们拆开理解:  

  • happens-before仅仅要求前一个操作(执行的结果)对后一个操作可见的意思是前一个操作的结果计算完成并刷新到主内存, 后一个操作的线程也就能看到前一个操作的执行结果, 也就是对后一个操作可见
  • 不意味着前一个操作必须要在后一个操作之前执行, 这句话的意思就是我们刚才说的谁在前边谁执行,  关键在于,执行这句话包含的不是一个时间点, 执行一个操作在cpu中是一个过程, 一个时间段,  一个操作必须要在后一个操作之前执行说的操作的开始,也就是这个执行时间段的开始, 总结起来这半句话的翻译就是: 不意味着前一个操作要在后一个操作之前开始执行
  • 且前一个操作按顺序排在第二个操作之前, 这个操作顺序指的是操作的结果发生的顺序, 前一个比后一个先执行结束

总结一下这句话:  happens-before不要求前一个操作和后一个操作的发生顺序,  仅仅要求前一个操作的执行完成并刷新回内存发生在后一个操作结果之前

为啥要区别操作的开始和结束呢?

大家应该也能猜到, 先开始的操作不一定先结束,JMM可以通过重排序等手段让代码顺序里靠后的指令先执行, 至于为什么要这样要去理解重排序的意义: 为啥要指令重排序

为啥要强调完成并刷新回内存(对后操作可见)呢?

这个是因为JAVA内存模型有本地缓存的抽象概念, 指令执行完结果放在本地缓存其他线程是看不到的, 要刷新回主内存对其他线程才可见(大家看到内存的结果才知道这个指令执行完了) , 这要大家去看JAVA内存模型(JMM)

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PE iDentifier v0.95 (2008.10.21) by snaker, Qwerton, Jibz & xineohP ------------------------------------------------------ PEiD detects most common packers, cryptors and compilers for PE files. I t can currently detect more than 600 different signatures in PE files. PEiD is special in some aspects when compared to other identifiers already out there! 1. It has a superb GUI and the interface is really intuitive and simple. 2. Detection rates are amongst the best given by any other identifier. 3. Special scanning modes for *advanced* detections of modified and unknown files. 4. Shell integration, Command line support, Always on top and Drag'n'Drop capabilities. 5. Multiple file and directory scanning with recursion. 6. Task viewer and controller. 7. Plugin Interface with plugins like Generic OEP Finder and Krypto ANALyzer. 8. Extra scanning techniques used for even better detections. 9. Heuristic Scanning options. 10. New PE details, Imports, Exports and TLS viewers 11. New built in quick disassembler. 12. New built in hex viewer. 13. External signature interface which can be updated by the user. There are 3 different and unique scanning modes in PEiD. The *Normal Mode* scans the PE files at their Entry Point for all documented signatures. This is what all other identifiers also do. The *Deep Mode* scans the PE file's Entry Point containing section for all the documented signatures. This ensures detection of around 80% of modified and scrambled files. The *Hardcore Mode* does a complete scan of the entire PE file for the documented signatures. You should use this mode as a last option as the small signatures often tend to occur a lot in many files and so erroneous outputs may result. The scanner's inbuilt scanning techniques have error control methods which generally ensure correct outputs even if the last mode is chosen. The first two methods produce almost instantaneous outputs but the last method is a bit slow due to obvious reasons! Command line Options -------------------- PEiD now fully supports commandline parameters. peid -time // Show statistics before quitting peid -r // Recurse through subdirectories peid -nr // Don't scan subdirectories even if its set peid -hard // Scan files in Hardcore Mode peid -deep // Scan files in Deep Mode peid -norm // Scan files in Normal Mode peid <file1> <file2> <dir1> <dir2> You can combine one or more of the parameters. For example. peid -hard -time -r c:\windows\system32 peid -time -deep c:\windows\system32\*.dll Task Viewing / Control Module ----------------------------- You can scan currently running tasks with PEiD. The files are scanned from memory. Processes can also be terminated. You can also optionally dump a module and scan the dumped image. You can also view all dependant modules of the processes. Multiple File Scan Module ------------------------- You can scan multiple files at one go with PEiD. Just drag and drop the files on the PEiD main dialog and the Multiple File Scan Dialog will popup displaying the results. You can keep dragging and dropping files onto this dialog as well. It also offers you to choose from the different scanning modes and optionally load a single file in PEiD. It allows you to skip the non PE files so that the list looks clean. You can also scan the contents of a directory choosing files of custom extension if required. MFS v0.02 now supports recursive directory scanning. Disassembler Module ------------------- You can have a quick disassembly of the file loaded in PEiD. Double click to follow JMPs and CALLs and use the Back button to trace back to the original positions. You can copy disassembled output to the clipboard. A new CADT core with custom String Reference Finder has been cooked up. CADT is coded by Ms-Rem. Hex Viewer Module ------------------- You can have a quick hex view of the file loaded in PEiD. A modified version of 16Edit by y0da is used for this purpose. We intend to update the signatures quite often to keep pace with this ever evolving scene :) Please report bugs, ideas, new signatures or packer info to: pusher -> sir.pusher(at)gmail(dot)com ( Administration / Coder ) snaker -> snaker(at)myrealbox(dot)com Jibz -> peid(at)ibsensoftware(dot)com Qwerton -> qwaci(at)gmx(dot)net ALL SUGGESTIONS, IDEAS, BUG REPORTS AND CRITICS ARE WELCOME. History ------- 0.7 Beta -> First public release. 0.8 Public -> Added support for 40 more packers. OEP finding module. Task viewing/control module. GUI changes. General signature bug fixes. Multiple File and Directory Scanning module. 0.9 Recode -> Completely recoded from scratch. New Plugin Interface which lets you use extra features. Added more than 130 new signatures. Fixed many detections and general bugs. 0.91 Reborn -> Recoded everything again. New faster and better scanning engine. New internal signature system. MFS v0.02 now supports Recursive Scanning. Commandline Parser now updated and more powerful. Detections fine tuned and newer detections added. Very basic Heuristic scanning. 0.92 Classic -> Added support for external database, independent of internal signatures. Added PE details lister. Added Import, Export, TLS and Section viewers. Added Disassembler. Added Hex Viewer. Added ability to use plugins from Multiscan window. Added exporting of Multiscan results. Added ability to abort MultiScan without loosing results. Added ability to show process icons in Task Viewer. Added ability to show modules under a process in Task Viewer. Added some more detections. 0.93 Elixir -> Added sorting of Plugin menu items. Submenus are created based on subfolders in the directory. Added Brizo disassembler core. Added some more detections. Fixed documented and undocumented vulnerability issues. Fixed some general bugs. Removed mismatch mode scanner which needs further improvements. 0.94 Flux -> Too much is new to remember. MFS, Task Viewer and Disassembler windows maximizable. New smaller and lighter disassembler core CADT. New KANAL 2.90 with much more detections and export features. Added loads of new signatures. Thanks to all the external signature collections online. String References integrated into disassembler. Fixed documented and undocumented crashes. Fixed some general bugs. 0.95 Phoenix -> Fixed some crashing bugs. Minor Core update. Greets ------ Qwerton, Jibz, CHRiST0PH, uno, DAEMON, MackT, VAG, SAC, Gamumba, SnowP and all the rest at uG, Michael Hering, tE!, pusher, {igNo}, Maxx, CoDE, BaND, Snacker, skamer, HypnZ, ParaBytes, Clansman, BuLLeT, Devine9, innuendo, Corby, cokine, AiRW0lF, fxfighter, GodsJiva, Carpathia, _death, artik, r!sc, NoodleSPA, SiR_dReaM, CHoRDLeSS, NeOXQuiCk, un4Giv3n, RZX, 7xS, LibX and all who helped with PEiD :) snaker, Jibz, cokine, Iczelion, Clansman, Z-Wing, Unknown One/TMG, PeeWee, DnNuke, sinny/BAFH, all the other nice people in CiA, uG and all of you who helped us develope PEiD. Thanks. snaker, Qwerton, DAEMON, VaG, Parabytes, bse, f0dder, Stone, Michael Hering, Iczelion, Steve Hutchesson, Eugene Suslikov, and everybody in #unpacking and #compression. Qwerton - Hope you get time someday again, was nice working with you :) Jibz - You rock evil friend. Thanks for all your help. It's a pleasure working with you. Hope things work out! Michael Hering - FILE INFO is still the absolute best. Your suggestions rock :) uG2oo6 - Delicious Slumber! MackT - Thanks for all your help and for ImpREC of course ;) Unknown One - Spend more time with us :) BaND - Thanks for all your testing and help. pusher - Thanks for your help and all the testing and the constant encouragment ;) Maxx - Thanks for the encouragment, your code and suggestions should be added next time :) Kaparo & Aaron - Thanks for your sites :) BoB - Thanks for taking over the PEiD project, and the contribution. We would also like to thank the *few* people who sent us their comments and feedback about PEiD. Also greetings to everyone who has supported PEiD till date. Without you this new release would never be possible. You can check out the PEiD homepage at http://www.peid.info and the PEiD Forums at http://www.peid.info/forum snaker, Qwerton, Jibz & xineohP Productions

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值