currentThread():
作用:返回代码段正在被那个线程调用的信息。
/** * Returns a reference to the currently executing thread object. *返回当前执行线程对象的引用 * * @return the currently executing thread. */ public static native Thread currentThread();
isAlive()
作用:放回当前线程是否处于存活状态
/** * Tests if this thread is alive. A thread is alive if it has * been started and has not yet died. *判断这个线程状态是否激活,如果这个线程已经启动并且没有死,那他状态就是活得 * @return <code>true</code> if this thread is alive; * <code>false</code> otherwise. */ public final native boolean isAlive();
示例:
public class ThreadTest { public static void main(String[] args) { try { MyThread thread1 = new MyThread(); System.out.println("线程启动之前的状态"+thread1.isAlive()); thread1.start(); System.out.println("线程启动后的状态"+thread1.isAlive()); thread1.sleep(1000); System.out.println("线程睡眠1000毫秒状态为:"+thread1.isAlive()); }catch (Exception e){ e.printStackTrace(); } } } class MyThread extends Thread{ @Override public void run() { System.out.println("run方法中线程的状态为:"+this.isAlive()); } }
运行结果如下:
线程启动之前的状态false
线程启动后的状态true
run方法中线程的状态为:true
线程睡眠1000毫秒状态为:false
Process finished with exit code 0
sleep()
作用(休眠期间锁资源不会得到释放,其他线程需要等待):
在指定毫秒数内让当前“正在执行的线程”休眠
/** * 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. * * @param millis * the length of time to sleep in milliseconds * * @throws IllegalArgumentException * if the value of {@code millis} is negative * * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ public static native void sleep(long millis) throws InterruptedException;
示例:
public class ThreadTest { public static void main(String[] args) { try { MyThread thread1 = new MyThread(); MyThread thread2 = new MyThread(); thread1.start(); System.out.println("thread1当前的状态为:"+thread1.isAlive()); thread1.sleep(100000); thread2.start(); System.out.println("thread2当前的状态为:"+thread2.isAlive()); }catch (Exception e){ e.printStackTrace(); } } } class MyThread extends Thread{ @Override public void run() { synchronized (this){ System.out.println("执行run方法的线程名称为:"+Thread.currentThread().getName()); } } }
getId()
作用:取得线程的唯一标识符ID
/** * Returns the identifier of this Thread. The thread ID is a positive * <tt>long</tt> number generated when this thread was created. * The thread ID is unique and remains unchanged during its lifetime. * When a thread is terminated, this thread ID may be reused. * * @return this thread's ID. * @since 1.5 */ public long getId() { return tid; }
示例:
public class ThreadTest { public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.start(); System.out.println(thread1.getId()); } } class MyThread extends Thread{ @Override public void run() { synchronized (this){ System.out.println("执行run方法的线程名称为:"+Thread.currentThread().getId()); } } }
运行结果如下:
12
执行run方法的线程名称为:12
yield()
作用:放弃当前cpu资源,让其他任务去占用cpu执行时间,但放弃时间不确定,很少适合使用,会过度使用cpu资源
示例:
public class ThreadTest { public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.start(); } } class MyThread extends Thread{ @Override public void run() { int count = 0; long beginTime = System.currentTimeMillis(); for(int i=0;i<5000000;i++){ //Thread.yield(); count = count+i; } long endTime = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+"用时:"+(endTime-beginTime)+"毫秒"); } }
运行结果:
Thread-0用时:5毫秒
将上面的注释放开后运行结果如下:
Thread-0用时:1495毫秒
Process finished with exit code 0