一文带你了解java多线程 | 软件测试 | java培训|web前端培训|UI设计培训|大数据培训|2017年最好的金牌IT培训机构-海文国际官网
  • <tfoot id="hfbgb"><i id="hfbgb"><col id="hfbgb"></col></i></tfoot>
      <label id="hfbgb"><optgroup id="hfbgb"></optgroup></label>

        <fieldset id="hfbgb"><dd id="hfbgb"></dd></fieldset>
        <map id="hfbgb"></map>
        1. <bdo id="hfbgb"><sup id="hfbgb"><div id="hfbgb"><bdo id="hfbgb"></bdo></div></sup></bdo>

              首页 > IT知识 > 一文带你了解java多线程

              一文带你了解java多线程

              2017年10月26日10:26:21来源:海文国际         643
              分享到:
              IT知识文章: 

              Java内存模型(JMM)定义了:how and when different threads can see

              values written to shared variables by other threads,

              and how to synchronize access to shared variables when necessary.

              Java堆和栈中的对象存储位置:

              Java内存模型与硬件模型:

              线程读取主内存的数据到CPU缓冲中,当数据放在不同位置时,会有两个问题:可见性与静态条件

              A synchronized block in Java is synchronized on some object.

              All synchronized blocks synchronized on the same object can only

              have one thread executing inside them at the same time.

              All other threads attempting to enter the synchronized block are blocked

              until the thread inside the synchronized block exits the block.

              The synchronized keyword can be used to mark four different types of blocks:

              Instance methods -> on the instance (object) owning the method

              Static methods -> on the class object of the class belongs to …

              Code blocks inside instance methods

              Code blocks inside static methods

              Synchronized Instance methods(实例方法的同步):

              静态方法的同步:

              代码块的同步:

              用jstack查看,同一个监视器对象只允许有一个线程访问:

              实例方法的同步加上代码块this的同步,仍然针对同一个实例对象:

              自定义监视器对象:

              同一个实例对象的加锁:

              不同实例对象的加锁:

              Volatile keyword guarantees visibility of changes to variables across threads.

              every read of a volatile variable will be

              read from the computer’s main memory,

              and not from the CPU cache.

              every write to a volatile variable will be

              written to main memory,

              and not just to the CPU cache.

              If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable, will also be visible to Thread B after it has read the volatile variable.

              The reading and writing instructions of volatile variables cannot be reordered by the JVM. Instructions before and after can be reordered, but the volatile read or write cannot be mixed with these instructions. Whatever instructions follow a read or write of a volatile variable are guaranteed to happen after the read or write.

              volatile变量不保证事务:

              volatile变量仍然会存在竞态条件:

              volatile变量会禁止重排序:

              如果变量在volatile变量更新之后,不保证写到主存:

              为了保证可见性,不需要为每个变量都定义为volatile类型:

              volatile变量是个内存屏障,在这之前和之后的指令可以重排序:

              本地线程的示例:

              下面的上图没有使用本地线程,下图使用了本地线程:

              线程的信号量实现方式–busy waiting:

              或者可以用volatile变量:

              wait和notify的示例:

              notify与notifyAll的示例:

              等待线程有可能意外被唤醒,需要用while循环继续判断是否被唤醒线程notify:

              一次唤醒所有线程,或者每次一个个地唤醒:

              不同线程之间采用字符串作为监视器锁,会唤醒别的线程:

              不同线程之间的信号没有共享,等待线程被唤醒后继续进入wait状态:

              不同线程的等待与唤醒示例:

              3544.com威尼斯人投注 |威尼斯手机投注平台 |威尼斯3544.com官方网站登录 | |手机版 | | 3544.com威尼斯人投注,威尼斯手机投注平台,威尼斯3544.com官方网站登录-m.53701.com|威尼斯6799.com平台,6799.com威尼斯城官网,威尼斯官方网站登录-1068.com|威尼斯86087.com手机版,威尼斯官方网站86087.com,澳门在线威尼斯官方-0369.com|威尼斯澳门官方73099.com,www.73099.com,威尼斯官方娱乐73099.com登陆-5003.cc|www.4886.com,澳门威尼斯娱乐www.4886.com,4886.com威尼斯官方网站-88330.com|澳门威利斯人手机版,www.4886.com官方入口,澳门威利斯人在线娱乐-379.cc|