Multithreading---cpu history and some key concepts

  • 1.CPU and Moore's Law:

摩尔定律是由英特尔(Intel)创始人之一戈登·摩尔(Gordon Moore)提出来的。其内容为:当价格不变时,集成电路上可容纳的元器件的数目,约每隔18-24个月便会增加一倍,性能也将提升一倍。换言之,每一美元所能买到的电脑性能,将每隔18-24个月翻一倍以上。这一定律揭示了信息技术进步的速度。这一定律自2004年以来,似乎已经失效,失效的原因是由于硅晶体的特性决定的,人们在制造cpu的工艺上已经达到了物理极限,在频率短期没有提高的情况下,为了来提高cpu的速度,人们在研究如何将多个独立的计算单元整合到单独的cpu里,也就是我们所说的多核cpu。
  • 2. Synchronous and Asynchronous

同步和异步通常用来形容一次方法调用。

同步方法调用一旦开始,调用者必须等到方法调用返回后,才能继续后续的行为。
异步方法调用更像一个消息传递,一旦开始,方法调用就会立即返回,调用者就可以继续后续的操作。而,异步方法通常会在另外一个线程中,“真实”地执行着。整个过程,不会阻碍调用者的工作。
  • 3. Concurrency and Parallelism

并发:在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行。(eg:我一边看书,一边敲代码)
单核cpu下的多线程和多进程是并发的,因为一个cpu一次只能执行一条指令,系统通过不断切换多任务来执行。
并行:在同一个时间段内,两个或多个程序执行,有时间上的重叠(宏观上是同时,微观上仍是顺序执行)。(eg:我负责看书,另外一个人负责敲代码)
  • 4. JMM (Java Memory Model, JAVA memory model)

JMM主要是为了规定了线程和内存之间的一些关系。根据JMM的设计,系统存在一个主内存(Main Memory),Java中所有变量都储存在主存中,对于所有线程都是共享的。每条线程都有自己的工作内存(Working Memory),工作内存中保存的是主存中某些变量的拷贝,线程对所有变量的操作都是在工作内存中进行,本地内存是JMM的一个抽象概念,并不真实存在。线程之间无法相互直接访问,变量传递均需要通过主存完成。JMM属于语言级的内存模型,它确保在不同的编译器和不同的处理器平台之上,通过禁止特定类型的编译器重排序和处理器重排序,为程序员提供一致的内存可见性保证。

  • 4.1 Reordering from source code to instruction sequence

    When executing a program, compilers and processors often reorder instructions to improve performance. There are 3 types of reordering:

  • 1. Compiler optimization and reordering: The compiler can rearrange the execution order of statements without changing the semantics of single-threaded programs
  • 2. Instruction-level parallel reordering: Modern processors use instruction-level parallelism to overlap multiple instructions. If there are no data dependencies, the processor can change the order in which the statements correspond to the machine instructions.
  • 3. Reordering of the memory system: Since the processor uses caches and read/write buffers, it is possible for load and store operations to appear to be performed out of order.

    The above 1 belongs to the compiler reordering, 2 and 3 belong to the processor reordering.
    For compilers, the JMM's compiler reordering rules prohibit certain types of compiler reordering.
    For processor reordering, the JMM's processor reordering rules require the Java compiler to insert certain types of memory barrier instructions to prohibit specific processor reorderings when generating instruction sequences.


  • 4.2 Instruction rearrangement rules: happens-before

    happens-before starting with JDK5, java uses the new JSR-133 memory model. JSR-133 proposes the concept of happens-before, through which the memory visibility between operations is explained. If the result of one operation's execution needs to be visible to the other, there must be a happens-before relationship between the two operations. The two operations mentioned here can be either within a thread or between different threads.

  • Program Order Rules: Semantic seriality is guaranteed within a thread.
  • Locking rules: Unlocking must happen before subsequent locking.
  • Volatile variable rules: The write of a volatile variable occurs first before the read of the volatile variable.
  • Transitivity: If A precedes B, and B precedes C, then A precedes C.
  • Thread start rules: The start() method of the Thread object occurs first for each action of this thread;
  • Thread interrupt rules: The call to the thread interrupt() method occurs first when the code of the interrupted thread detects the occurrence of the interrupt event;
  • Thread termination rule: All operations in the thread occur first in the thread termination detection. We can detect that the thread has terminated execution through the end of the Thread.join() method and the return value of Thread.isAlive().
  • Object finalization rules: the initialization of an object occurs first at the beginning of its finalize() method;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326533231&siteId=291194637