您的位置:首页 > 编程语言 > Java开发

Java中线程的基本方法使用技巧

2017-09-15 10:02 696 查看

java中线程的基本方法的熟练使用是精通多线程编程的必经之路,线程相关的基本方法有wait,notify,notifyAll,sleep,join,yield等,本文浅要的介绍一下它们的使用方式。

线程的状态图

java将操作系统中的就绪和运行两种状态统称为可运行状态,java中线程的状态可以认为有以上六种。

wait

调用该方法的线程进入WAITING状态,只有等待另外线程的通知或被中断才会返回,需要注意的是调用wait()方法后,会释放对象的锁。

因此,wait方法一般用在同步方法或同步代码块中。

sleep

sleep导致当前线程休眠,与wait方法不同的是sleep不会释放当前占有的锁,sleep(long)会导致线程进入TIMED-WATING状态,而wait()方法会导致当前线程进入WATING状态

yield

yield会使当前线程让出CPU执行时间片,与其他线程一起重新竞争CPU时间片。一般情况下,优先级高的线程有更大的可能性成功竞争得到CPU时间片,但这又不是绝对的,有的操作系统对线程优先级并不敏感。

interrupt

中断一个线程,其本意是给这个线程一个通知信号,会影响这个线程内部的一个中断标识位。这个线程本身并不会因此而改变状态(如阻塞,终止等)。

1.调用interrupt()方法并不会中断一个正在运行的线程。也就是说处于Running状态的线程并不会因为被中断而被终止,仅仅改变了内部维护的中断标识位而已。

2.若调用sleep()而使线程处于TIMED-WATING状态,这时调用interrupt()方法,会抛出InterruptedException,从而使线程提前结束TIMED-WATING状态。

3.许多声明抛出InterruptedException的方法(如Thread.sleep(long mills方法)),抛出异常前,都会清除中断标识位,所以抛出异常后,调用isInterrupted()方法将会返回false。

4.中断状态是线程固有的一个标识位,可以通过此标识位安全的终止线程。比如,你想终止一个线程thread的时候,可以调用thread.interrupt()方法,在线程的run方法内部可以根据thread.isInterrupted()的值来优雅的终止线程。当然,你可以在线程内部自己维护一个boolean变量来控制线程的运行和终止。

现在,我们看一下源码里这个方法是怎么说明的。

/**
* Interrupts this thread.
* 中断这个线程
*
* <p> Unless the current thread is interrupting itself, which is
* always permitted, the {@link #checkAccess() checkAccess} method
* of this thread is invoked, which may cause a {@link
* SecurityException} to be thrown.
* 如果不是当前线程中断自身,这经常是被允许的。不过要检查安全性,
* 可能会抛出安全异常的。
*
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* 如果中断的线程由于调用一个Object对象的多个wait方法或者当前对象的
* join,sleep方法而正处于阻塞状态(仅表示没有获得CPU的时间片执行,不表示
* 线程的BLOCKED状态)。那么它的中断状态将被清除(复位),而且它会收到中
* 断异常。
*
* <p> If this thread is blocked in an I/O operation upon an {@link
* java.nio.channels.InterruptibleChannel InterruptibleChannel}
* then the channel will be closed, the thread's interrupt
* status will be set, and the thread will receive a {@link
* java.nio.channels.ClosedByInterruptException}.
*
* <p> If this thread is blocked in a {@link java.nio.channels.Selector}
* then the thread's interrupt status will be set and it will return
* immediately from the selection operation, possibly with a non-zero
* value, just as if the selector's {@link
* java.nio.channels.Selector#wakeup wakeup} method were invoked.
*
* <p> If none of the previous conditions hold then this thread's interrupt
* status will be set. </p>
* 如果上述的条件都没有比中的话,那么这个线程的中断标志位将被设置。
*
* <p> Interrupting a thread that is not alive need not have any effect.
* 中断一个不存活的线程(未启动或已结束)不会有任何影响
*
* @throws SecurityException
*  if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
public void interrupt() {
//检查当前线程对this线程的安全权限,如果不允许修改,会抛出异常
if (this != Thread.currentThread())
checkAccess();
//加锁同步
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0();  // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0(); //设置标识位,本地方法
}

基本很简单,首先检查当前线程对this线程的安全权限,如果不允许修改,会抛出异常。随后加锁同步设置中断标识位。尽管方法声明中有详细说明,但是代码中看不出来,处于wating状态的线程被中断后,中断标识会复位。我认为这是靠本地代码interrupt0实现的。

join

在线程A上下文中执行了线程B.join()语句,其含义是线程B执行结束后,join()方法才会返回,线程A才可继续执行。

举个例子,如果你创建了10个线程,同时调用start()方法执行线程,但是你想让它们有序执行,就可以使用join来辅助完成。代码如下:

/***
* 此示例中10个线程在执行时,,需要等待前一个线程执行完;
* 比如线程0要等待main线程执行完
* 线程9要等到线程8执行完
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
Thread previous = Thread.currentThread();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Dimon(previous), String.valueOf(i));
thread.start();
previous = thread;
}
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName() + "terminate");
}
static class Dimon implements Runnable{
private Thread thread;
public Dimon(Thread thread){
this.thread = thread;
}
@Override
public void run() {
try {
thread.join();
}catch (Exception e){
}
System.out.println( Thread.currentThread().getName() + ":terminate" );
}
}

总结

以上所述是小编给大家介绍的Java中线程的基本方法使用技巧,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 线程 使用