Thread类的常用方法_设置线程名称的方法,Thread类的常用方法_sleep

Thread类的常用方法_设置线程名称的方法:

public string getname() :获取当前线程名称

public class Demo01setThreadName {
     public static void main(string[]args) {
            //开启多线程
            My Thread mt = new MyThread();
             mt.setName("小强");
             mt.start();


            //开启多线程
            new My Thread("老枪")
    }
}

 

 

 

Thread类的常用方法_sleep:

public static void sleep(long millis) :使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行)

public class Demo01Sleep{
     public static void main(String[] args) {
          //模拟秒表
          for (int i = 1; i <=60 ; i++) {
               system.out.print1n(i);

              //使用Thread类的sleep方法让程序睡眠1秒钟
             try {
                   Thread.sleep( millis: 1000) ;
            } catch (InterruptedException e) {
                    e.printstackTrace( );
            }
        }
}

 

posted @ 2022-07-07 21:31  冰灵IT  阅读(36)  评论(0)    收藏  举报