1.currentThread()
public class Thread14 extends Thread{ static { System.out.println("静态块的打印:" + Thread.currentThread().getName()); } public Thread14() { System.out.println("构造方法的打印: " + Thread.currentThread().getName()); } public void run() { System.out.println("run()方法的打印:" + Thread.currentThread().getName()); } //test public static void main(String[] args) { Thread14 thread14 = new Thread14(); thread14.start(); } }
运行结果:
静态块的打印:main
构造方法的打印: main
run()方法的打印:Thread-0
这个例子说明了,线程的构造方法、静态块是被main线程调用的,而线程类的run()方法才是应用线程自己调用的。
1 public class Thread15 extends Thread{ 2 3 public Thread15() { 4 System.out.println("thread15 ---->begin"); 5 System.out.println("Thread.currentThread().getName()----->" + Thread.currentThread().getName()); 6 System.out.println("this.getName()----->" + this.getName()); 7 System.out.println("thread15---->end"); 8 } 9 10 public void run() { 11 System.out.println("run------->begin"); 12 System.out.println("Thread.currentThread.getName()---->" + Thread.currentThread().getName()); 13 System.out.println("this.getName()------>" + this.getName()); 14 System.out.println("run------>end"); 15 } 16 public static void main(String[] args) { 17 Thread15 thread15 = new Thread15(); 18 thread15.start(); 19 } 20 }
运行结果:
thread15 ---->begin Thread.currentThread().getName()----->main this.getName()----->Thread-0 thread15---->end run------->begin Thread.currentThread.getName()---->Thread-0 this.getName()------>Thread-0 run------>end
当在执行Thread15 thread15 = new Thread15()的时候,this.getName也就是当前线程却是Thread-0.
2.sleep(long millions)
该方法的作用是在指定的时间内让当前正在执行的线程Thread.currentThread()暂停执行,也就是休眠。值得注意的一点是,该方法并不让出cpu资源,换句话说,也就是CPU依然在执行run()中的内容,无非这个内容是休眠而已。
1 public class Thread16 extends Thread{ 2 public void run() { 3 try{ 4 System.out.println("run threadName= " + this.getName() + " begin"); 5 Thread.sleep(20000); 6 System.out.println("run threadName= " + this.getName() + " end"); 7 }catch (InterruptedException e) { 8 e.printStackTrace(); 9 } 10 } 11 public static void main(String[] args) { 12 Thread16 thread16 = new Thread16(); 13 System.out.println("begin= " + System.currentTimeMillis()); 14 thread16.start(); 15 System.out.println("end= " + System.currentTimeMillis()); 16 } 17 18 }
执行结果:
1 begin= 1447813479735 2 end= 1447813479736 3 run threadName= Thread-0 begin 4 run threadName= Thread-0 end
System.out.print是静态方法,因此输出的内容很快就会完成。启动的线程中有个休眠过程,thread-0 begin 和 thread-0 end 中间就会有等待。
3.yield()
1 public class Thread17 extends Thread{ 2 public void run() { 3 long beginTime = System.currentTimeMillis(); 4 int count = 0; 5 for(int i = 0; i < 50000000; i++) { 6 Thread.yield();//去掉之后明显整个程序运行时间剪短 7 count = count + i + 1; 8 } 9 long endTime = System.currentTimeMillis(); 10 System.out.println("用时:" + (endTime - beginTime) + "毫秒" + count); 11 } 12 13 public static void main(String[] args) { 14 Thread17 thread17 = new Thread17(); 15 thread17.start(); 16 // Thread.currentThread().interrupt(); 17 // System.out.println("1---" + Thread.interrupted()); 18 // System.out.println("2---" + Thread.interrupted()); 19 } 20 21 }
yield()就是暂停当前执行的线程,并执行其他线程。这个暂停会是放弃cpu资源的,并且放弃cpu时间不确定,有可能刚放弃就又获得了。因此这里因为yield()方法中间消耗的时间也是不确定的。
4.interrupted()
1 public static void main(String[] args) { 2 // Thread17 thread17 = new Thread17(); 3 // thread17.start(); 4 Thread.currentThread().interrupt(); 5 System.out.println("1---" + Thread.interrupted()); 6 System.out.println("2---" + Thread.interrupted()); 7 }
测试当前线程是否已经中断,执行后就将状态标示改为false。因此,两次调用后返回必然为false
执行结果:
1---true 2---false
时间: 2024-11-08 22:29:05