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

搜索热词

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

线程的状态图

15060750571.png?2017815101113

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.

* 中断这个线程

*

*

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.

* 如果不是当前线程中断自身,这经常是被允许的。不过要检查安全性,

* 可能会抛出安全异常的。

*

*

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,* methods of this class,then its interrupt status will be cleared and it

* will receive an {@link InterruptedException}.

* 如果中断的线程由于调用一个Object对象的多个wait方法或者当前对象的

* join,sleep方法而正处于阻塞状态(仅表示没有获得cpu的时间片执行,不表示

* 线程的BLOCKED状态)。那么它的中断状态将被清除(复位),而且它会收到中

* 断异常。

*

*

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}.

*

*

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.

*

*

If none of the prevIoUs conditions hold then this thread's interrupt

* status will be set.

* 如果上述的条件都没有比中的话,那么这个线程的中断标志位将被设置。

*

*

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中线程的基本方法使用技巧全部内容,希望文章能够帮你解决Java中线程的基本方法使用技巧所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值