Java Concurrency (a): gearing up


The title of this article was originally called the --Java concurrent programming (a): Introduction, the author called small two. But I think the title of this novel is not enough, not enough to attract the reader's eye, forcibly modify the title at the time of posting (not ye drop) when you receive a submission.

 

The second is a small Java programmer, worked for silent company, length of service is two years and one month and three days. It would just graduated and compared to programming capability has been greatly improved, but the leadership of the Pharaoh has dared to concurrent programming arrangements to develop small Second, it makes little two heart to heart.

This matter does not blame Pharaoh, small two was very clear: write correct programs is difficult to write correct concurrent programs more difficult. Not to their own skill on the part, if messed up, it is inevitable to always be cautious of Pharaoh matt surface.

After much deliberation small two, only one way, take the initiative to learn! To the Pharaoh to find a "Java Concurrency in combat", it said the book is in concurrent programming classic. Get the book, hand rolled, even found a love letter hidden inside: a small two excited bad, imagine Pharaoh write words of love, the way could not help but laugh sound.

(Halted)

Little background on the two first introduced to this. Next, we come to appreciate together under a small two finish after the first article in this book written.

Java Concurrency (a): Introduction

01, Why do we need an operating system

I like to listen to music (MP3 without a computer) at the time of writing (without pen and paper with the computer), and if the computer can only do one thing, I can only listen to music when the finished article, or listening to music when you start writing, so it is accurate - in the absence of an operating system, really it is so unhappy.

Once you have the operating system, the situation becomes quite different, the computer can run multiple programs at the same time. By TOPcommand can be viewed on a computer currently running processes (and program are closely related), see below.

Normally, a program corresponding to the at least one process. The figure above, "Google Chrome" This means that the three processes on My Computer to open with a program called Google browser.

Let us use some professional terms to describe the relationship between the procedures and processes:

程序是计算机为完成特定任务所执行的指令序列。 操作系统允许多道程序并发执行共享系统资源,而程序在并发执行时所产生的一系列特点使得传统的程序概念已经不足以对其进行描述,因此,引入了“进程(Process)”:可以更好的描述计算机程序的执行过程,反映操作系统的并发执行、资源共享及用户随机访问的特性,并以此作为资源分配的基本单位。

每当一个程序运行时,操作系统就为该程序创建了一个进程,并为它分配资源、调度其运行。程序执行结束后,进程也就消亡了。一个程序被同时执行多次,系统就会创建多个进程。因此,一个程序可以被多个进程执行,一个进程也可以同时执行多个程序。

当然了,对于现在的操作系统来说,进程并不是最小的调度单位,而是线程。线程也被称为轻量级进程。

由于同一个进程中的所有线程会共享进程的内存地址空间,因此这些线程都能访问相同的变量,如果没有明确的同步机制来协同对共享数据的访问,那么当一个线程正在使用某个变量时,另外一个线程可能同时访问这个变量,就会造成不可预测的结果。

02、多线程的优势

查看了一下,我这台电脑的物理 CPU(处理器)个数只有一个,但是核数(一块 CPU 上面能处理数据的芯片组的数量)是 4 个。

这意味着,我这台电脑能够在同一时间处理一个进程内的四个线程任务:线程 A 正在读取一个文件,线程 B 正在写入一个文件,线程 C 正在计算一个数值,线程 D 正在进行网络传输。

我们知道,进行文件读写或者网络传输通常会发生阻塞,这也是没办法的事。如果没有多线程的帮助,程序会按照顺序依次执行,也就意味着发生阻塞的时候其他任务只能干巴巴的等着,什么也做不了。

有了多线程,情况就完全不一样了,线程之间可以互不干扰,从而发挥处理器的多核能力。

说个有点让人难为情的事,我是 Eclipse 的(愚)忠实用户,至今没切换到 IDEA 阵营。在用 Eclipse 的时候经常会出现这样的情况,一个进度被另外一个卡住,下一个必须等待上一个执行完毕才开始执行。等待的时候几乎什么也干不成,点了取消也没用!

假如 Eclipse 采用多线程的话,每个任务放在单独的任务中执行,响应就会快很多。

03、多线程带来的风险

曾有这样一则耳熟能详的故事。

特洛伊人在城外的海滩上发现了一只巨大的木马,他们把它拉进了城里而不是把它烧掉或推到海里,以为这是天神给特洛伊人带来的赐福。于是,特洛伊人欢天喜地,庆祝胜利,他们跳着唱着,喝光了一桶又一桶的酒,以为希腊人被他们战败了。

而故事的结局大家也都知道了。希腊人把特洛伊城掠夺成空,烧成一片灰烬。海伦(宙斯之女,被称为“世上最美的女人”,她和特洛伊王子私奔,引发了特洛伊战争)也被墨涅依斯带回了希腊。

海伦海伦

多线程带来了无与伦比的好处,但也潜藏了巨大的风险(就像那个木马)。其中尤为突出的就是安全性问题。

public class Unsafe {
  private int chenmo;
  public int add() {
    return chenmo++;
  }
}

上面这段代码在单线程的环境中可以正确执行,但在多线程的环境中则不能。递增运算 chenmo++ 可以拆分为三个操作:读取 chenmo,将 chenmo 加 1,将计算结果赋值给 chenmo。两个线程可能交替执行,发生下图中的情况,于是两个线程就会返回相同的结果。这也是最常见的一种安全性问题。

其次,多线程还会引发活跃性问题:线程 B 需要等待线程 A 释放它们共有的资源,而线程 A 由于一些问题导致无法释放资源,那么线程 B 就只能苦苦地等下去。

再者,多线程还会引发性能问题(设计良好的多线程当然会提高性能):当线程调度器临时挂起一个活跃中的线程转而运行另外一个线程时,就会频繁地出现上下文切换(Context Switch)——开销很大(挣得多花的也多)。

04、单核 CPU 和多核 CPU

来思考一个问题吧。假如 CPU 只有一个,核数也只有一个,多线程还会有优势吗?

闭上眼,让思维旋转跳跃会。

来看答案吧。

单核 CPU 上运行的多线程程序,同一时间只有一个线程在跑,系统帮忙进行线程切换;系统给每个线程分配时间片(大概 10ms)来执行,看起来像是在同时跑,但实际上是每个线程跑一点点就换到其它线程继续跑。所以效率不会有所提高,线程的切换反到增加了系统开销。

那多核 CPU 呢?

当然有优势了!多核需要多线程才能发挥优势(不然巧妇难为无米之炊啊),同样,多线程要在多核上才能有所发挥(好马配好鞍啊)。

多核 CPU 多线程不仅善于处理 IO 密集型的任务(减少阻塞时间),还善于处理计算密集型的任务,比如加密解密、数据压缩解压缩(视频、音频、普通数据等),让每个核心都物尽其用。

05、最后

亲爱的读者朋友们,小二投稿的第一篇文章到此就结束了。你对此感到满意吗?或者说你期待下一篇吗?

(此时的小二正在翘首以盼)

 

Guess you like

Origin www.cnblogs.com/qing-gee/p/11433429.html