当前位置: 移动技术网 > IT编程>开发语言>Java > Java中线程的基本方法使用技巧

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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中线程的基本方法使用技巧,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网