首页 > 代码库 > java多线程基本概述(二)——Thread的一些方法
java多线程基本概述(二)——Thread的一些方法
在Thread类中有很多方法值得我们关注一下。下面选取几个进行范例:
1.1、isAlive()方法
java api 描述如下:
public final boolean isAlive() Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Returns: true if this thread is alive; false otherwise.
示例代码如下:
package soarhu; import java.util.concurrent.TimeUnit; /** * Created by huaox on 2017/4/17. * */ class ThreadTest extends Thread{ private int count = 5; @Override public void run() { System.out.println("3->"+Thread.currentThread().isAlive()); } } public class Test { public static void main(String[] args) throws InterruptedException { ThreadTest thread = new ThreadTest(); thread.setName("a"); System.out.println("1->"+thread.isAlive()); thread.start(); TimeUnit.SECONDS.sleep(3); System.out.println("2->"+thread.isAlive()); } }
输出结果:
1->false
3->true
2->false
在main线程休眠3秒后,子线程此时已经结束。那么isAlive()方法放回为假。
1.2:sleep()方法
api 文档如下
public static void sleep(long millis) throws InterruptedException 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. // 线程休眠后不会丢掉它的所属监视器,即如果有锁则不会释放锁。 Parameters: millis - the length of time to sleep in milliseconds Throws: IllegalArgumentException - if the value of millis is negative InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
//当休眠的线程被中断时,sleep()方法则会抛出InterruptedException.例如调用该线程的interrupt()方法,那么该异常将被抛出并且线程的中断状态将会被清除,
package soarhu; /** * Created by huaox on 2017/4/17. * */ class ThreadTest extends Thread{ private long count = 20; @Override public synchronized void run() { count<<=1; System.out.println("run: "+count); } synchronized void second(){ //和run方法的锁是同一个锁对象, try { count-=5; Thread.sleep(3000); System.out.println("second: "+count); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Test { public static void main(String[] args) throws InterruptedException { ThreadTest thread = new ThreadTest(); thread.start(); thread.second();//该方法会先于thread的run方法执行 } }
输出结果:
second: 15
run: 30
由于run和second方法都是同步方法,所以他们的监视器对象是一致的,那么就会进行阻塞访问。当second休眠的时候。run方法没有被执行。说明sleep()方法不会丢掉锁,如果丢掉的话,run()方法会立即得到执行。
package soarhu; import java.util.concurrent.TimeUnit; /** * Created by huaox on 2017/4/17. * */ class ThreadTest extends Thread{ private long count = 20; @Override public void run() { count<<=1; try { System.out.println("sleep before: "+Thread.currentThread().isInterrupted()); TimeUnit.SECONDS.sleep(10);//收到interrupt()时,会触发中断异常,并且将中断状态设置为true. } catch (InterruptedException e) { System.out.println("catch: "+Thread.currentThread().isInterrupted());//carch块中,中断状态将被清除。设置为false e.printStackTrace(); } } } public class Test { public static void main(String[] args) throws InterruptedException { ThreadTest thread = new ThreadTest(); thread.start(); Thread.sleep(2000); thread.interrupt();//给线程发一个中断标志 System.out.println("main: "+thread.isInterrupted()); } }
输出结果:
sleep before: false
main: false
catch: false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at soarhu.ThreadTest.run(Test.java:17)
Thread.interrupt()并不会真正的中断线程,只是给该线程发送一个中断标志为true的flag
- 如果该线程处于阻塞状态,例如在sleep(),wait(),join()阻塞中的线程时,那么该线程将会抛出InterruptedException异常。并且在异常块中将中断标志flag清除掉,即设为false.
- 如果该线程在
可中断的通道
上的 I/O 操作中受阻,则该通道将被关闭,该线程的中断状态将被设置并且该线程将收到一个
ClosedByInterruptException
。 - 如果该线程在一个
Selector
中受阻,则该线程的中断状态将被设置,它将立即从选择操作返回,并可能带有一个非零值,就好像调用了选择器的wakeup
方法一样。 - 如果以前的条件都没有保存,则该线程的中断状态将被设置。
- 中断一个非活动线程并不会产生任何的副作用。
1.3、 interrupt()方法
public void interrupt() Interrupts this thread. Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked,
which may cause a SecurityException to be thrown.如果当前线程没有中断它自己(这在任何情况下都是允许的),则该线程的checkAccess
方法就会被调用,这可能抛出SecurityException
。 If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class,
or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class,
then its interrupt status will be cleared and it will receive an InterruptedException. If this thread is blocked in an I/O operation upon an interruptible channel
then the channel will be closed, the thread‘s interrupt status will be set,
and the thread will receive a ClosedByInterruptException. If this thread is blocked in a 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 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
1.4、interrupted()方法
public static boolean interrupted() Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. //测试当前线程是否被中断,调用此方法会清除中断状态。
In other words, if this method were to be called twice in succession,
the second call would return false (unless the current thread were interrupted again,
after the first call had cleared its interrupted status and before the second call had examined it).//调用两次该方法会返回false A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false. Returns: true if the current thread has been interrupted; false otherwise. See Also: isInterrupted()
1.5、isInterrupted()
public boolean isInterrupted() Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.//调用该方法不会清除中断状态 A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false. Returns: true if this thread has been interrupted; false otherwise. See Also: interrupted()
java多线程基本概述(二)——Thread的一些方法