您的位置:首页 > 其它

3.Thread中的静态方法

2015-11-18 10:55 323 查看
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()方法才是应用线程自己调用的。

public class Thread15 extends Thread{

public Thread15() {
System.out.println("thread15 ---->begin");
System.out.println("Thread.currentThread().getName()----->" + Thread.currentThread().getName());
System.out.println("this.getName()----->" + this.getName());
System.out.println("thread15---->end");
}

public void run() {
System.out.println("run------->begin");
System.out.println("Thread.currentThread.getName()---->" + Thread.currentThread().getName());
System.out.println("this.getName()------>" + this.getName());
System.out.println("run------>end");
}
public static void main(String[] args) {
Thread15 thread15 = new Thread15();
thread15.start();
}
}


运行结果:

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()中的内容,无非这个内容是休眠而已。

public class Thread16 extends Thread{
public void run() {
try{
System.out.println("run threadName= " + this.getName() + " begin");
Thread.sleep(20000);
System.out.println("run threadName= " + this.getName() + " end");
}catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread16 thread16 = new Thread16();
System.out.println("begin= " + System.currentTimeMillis());
thread16.start();
System.out.println("end= " + System.currentTimeMillis());
}

}


执行结果:

begin= 1447813479735
end= 1447813479736
run threadName= Thread-0 begin
run threadName= Thread-0 end


System.out.print是静态方法,因此输出的内容很快就会完成。启动的线程中有个休眠过程,thread-0 begin 和 thread-0 end 中间就会有等待。

3.yield()

public class Thread17 extends Thread{
public void run() {
long beginTime = System.currentTimeMillis();
int count = 0;
for(int i = 0; i < 50000000; i++) {
Thread.yield();//去掉之后明显整个程序运行时间剪短
count = count + i + 1;
}
long endTime = System.currentTimeMillis();
System.out.println("用时:" + (endTime - beginTime) + "毫秒" + count);
}

public static void main(String[] args) {
Thread17 thread17 = new Thread17();
thread17.start();
//        Thread.currentThread().interrupt();
//        System.out.println("1---" + Thread.interrupted());
//        System.out.println("2---" + Thread.interrupted());
}

}


yield()就是暂停当前执行的线程,并执行其他线程。这个暂停会是放弃cpu资源的,并且放弃cpu时间不确定,有可能刚放弃就又获得了。因此这里因为yield()方法中间消耗的时间也是不确定的。

4.interrupted()

public static void main(String[] args) {
//        Thread17 thread17 = new Thread17();
//        thread17.start();
Thread.currentThread().interrupt();
System.out.println("1---" + Thread.interrupted());
System.out.println("2---" + Thread.interrupted());
}


测试当前线程是否已经中断,执行后就将状态标示改为false。因此,两次调用后返回必然为false

执行结果:

1---true
2---false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: