sleep()-T15
程序员文章站
2022-03-13 21:25:25
package 多线程技能1;/** *sleep()方法:在指定时间(毫秒)内让当前“正在执行的线程休眠(暂停执行), * 这个正在执行的线程是指由this.currentThread()返回的线程。 */class MyThreadSleep extends Thread{ @Override public void run() { try { System.out.println("run threadName="+currentThr...
package 多线程技能1;
/**
*sleep()方法:在指定时间(毫秒)内让当前“正在执行的线程休眠(暂停执行),
* 这个正在执行的线程是指由this.currentThread()返回的线程。
*/
class MyThreadSleep extends Thread{
@Override
public void run() {
try {
System.out.println("run threadName="+currentThread().getName()+"begin");
Thread.sleep(2000);
System.out.println("run threadName="+currentThread()+"end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyThreadSleep1 extends Thread{
@Override
public void run() {
try {
System.out.println("run threadName="+currentThread().getName()+"begin="+System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("run threadName="+currentThread()+"end="+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class T15 {
public static void main(String[] args) {
MyThreadSleep myThreadSleep=new MyThreadSleep();
System.out.println("begin="+System.currentTimeMillis());
myThreadSleep.run();
System.out.println("end="+System.currentTimeMillis());
System.out.println("---------------------");
MyThreadSleep1 myThreadSleep1=new MyThreadSleep1();
System.out.println("begin="+System.currentTimeMillis());
myThreadSleep1.run();
System.out.println("end="+System.currentTimeMillis());
/*
由于main线程与MyThreadSleep1线程是异步执行的,所以首先输出的信息为begin和end
而MyThreadSleep线程是后运行的,在最后两行间隔2S输出run--begin和run---end相关信息。
*/
}
}
本文地址:https://blog.csdn.net/qq_43834878/article/details/111962138
上一篇: 茶点和甜点的区别