java-- 线程(一):讲解

转载注明来源:http://blog.csdn.net/yinwenjie

1、JAVA中线程的状态

这里写图片描述

1-1、#对象锁

这里写图片描述

  • 在JAVA中每一个对象都有一把‘锁’,这个‘锁’可以是开放状态;也可以由某一个线程(或者多个线程)持有‘钥匙’;一旦在系统中某个对象的‘锁’存在至少一把‘钥匙’,那么任何线程操作这个对象,都必须验证是否有‘钥匙’,如果没有则会报IllegalMonitorStateException异常。

  • 可是‘锁芯’(对象独占权)只有一个,那么可以打开这把锁的多个‘钥匙’同一时间内只能有一把‘钥匙’进行操作;其他持有‘钥匙’的线程(或者没有持有钥匙的线程)都要进入等待状态;直到某把‘钥匙’从‘锁眼’中退出,操作系统会决定哪一把‘钥匙’重新插入‘锁芯’(操作系统线程切换)。

  • 某一个线程在执行过程中,可以随时为任何一个对象‘加锁’;也可以随时归还这个对象锁的‘钥匙’。如果归还了这个对象锁的‘钥匙’,那么这个线程(在对象加了锁的状态下)肯定就不再具有这个对象的操作权了。

  • 需要注意:某一个线程拥有一个对象的‘锁’的‘钥匙’,并不代表这个线程的‘钥匙’是插入了‘锁芯’的(有这个对象的控制权);但是,有权抢占‘锁芯’控制权的线程,必定拥有这个对象的‘钥匙’。

  • 线程中,‘锁芯’代表对象的独占控制权;需要对某个对象的锁状态进行检测的关键字为‘synchronized’(临界区)。能将钥匙拔出锁芯,但是又不归还锁的调用方法(释放对象独占状态,并能在后续参与对象独占竞争的方法),只有wait和wait(time)。归还钥匙的方式包括:线程中针对某个对象的临界区正常完成、有异常抛出到临界区外(临界区异常完成)

请一定注意wait(time)的用法。很多初学者(包括我本人在很长一段时间)的理解都是:‘等待一段时间time,然后该线程激活继续工作’;但是如果您按照上文的描述,就会发现这样的理解是有问题的。实际上time更准确的含义应该是:到时检查。而整个wait(time)更准确的理解应该是:释放这个线程独占的X对象的锁芯(独占权),以便其它可以抢占‘锁芯’(独占权)的线程能够进行抢占,但是本线程继续持有X对象的锁的钥匙,等待time的时间后,重新参与‘锁芯’抢占(不一定能够抢占得到)。

1-2、对象锁的工作过程

下图表示了线程中,对象锁的工作过程:

这里写图片描述


1-3、synchronized可标注的位置

在JAVA中synchronized关键字可以加载很多位置。您可以在一个方法定义上加synchronized关键字、也可以在方法体中加synchronized关键字、还可以在static块中加synchronized关键字。但是不同位置的synchronized的关键字,代表的含义是不一样的。synchronized(){}这个写法,开发人员可以指定需要检查的对象锁。但是当synchronized加载在方法上时,有的读者就感觉有点混淆了。这里详细说明一下:

  • synchronized关键字加载在非静态方法上时: 
    其代表的含义和synchronized(this){}的意义相同。即对所拥有这个方法的对象进行对象锁状态检查。

  • synchronized关键字加载在静态方法上时: 
    其代表的含义和synchronized(Class.class)的意义相类似。即对所拥有这个方法的类的对象进行对象锁状态检查(类本身也是一个对象哦 ^_^)。

2-1、notify和notifyAll操作

在JAVA JDK中,对于notify方法和notifyAll方法的解释分别是:

  • notify:

Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

  • notifyAll:

Wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

为了说明notify方法和notifyAll方法的工作现象,下面我会为这两个方法分别给出一段代码,并进行详细解释。

2-2、interrupt信号

interrupt,单词本身的含义是中断、终止、阻断。当某个线程收到这个信号(命令)的时候,会将自生的状态属性置为“interrupted”,但是线程本身并不会立刻终止。程序员需要根据这个状态属性,自行决定如何进行线程的下一步活动。

2-2-1、interrupt和InterruptedException

这里写图片描述

上图是文章中已出现无数次的线程状态变化图,我们已经知道线程从创建后可以处于多种不同状态:就绪(可运行)、运行中、阻塞(等待中)、死亡。并不是线程处于任何状态,都可以接收interrupt信号。如果在收到interrupt信号时,线程处于阻塞状态(wait()、wait(time)或者sleep引起的),那么线程将会抛出InterruptedException异常:

Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception.

这里写图片描述

上图已经清楚说明了当Thread收到interrupt信号时,可能的两种结果:要么其线程对象中的isinterrupt属性被置为true;要么抛出InterruptedException异常。注意,如果抛出了InterruptedException异常,那么其isinterrupt属性不会被置为true

2-3、join操作

2-3-1、基本操作

join操作会使两个线程的执行过程具有先后顺序,具体来说:如果A线程调用B线程的join操作,A线程就会一直等待(或者等待某个指定的时间长度),直到B线程执行完成后(死亡状态)A线程才会继续执行。如下图所示:

这里写图片描述

2-4、sleep操作

sleep操作是大家在工作中最喜欢使用的方法。什么原因呢?在我来看,不是因为大多数使用者理解了sleep的使用场景,而是因为sleep操作是JAVA基本线程操作中操作意义“看似”最明显的一个方法。

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

sleep将“当前线程”进入阻塞状态,并且不会释放这个线程所占用的任何对象锁的独占状态。这里请注意:

  • 当前线程,是指当前调用sleep方法的线程。而不是指被调用的目标线程。

猜你喜欢

转载自blog.csdn.net/qq_39949109/article/details/80209372