线程(3)--线程中的方法setName(),getName(),sleep(),daemon(),join(),yield()等

1.获取线程名、设置线程名

package com.fenqing.duoxiancheng;

public class d4_threadMethod {

    public static void main(String[] args) {
        /*
         * 通过构造函数可以传入String类型的名字
         * 通过getName()方法获取线程对象的名字
         */
        new Thread("1---"){
            public void run(){
                System.out.println(this.getName()+"11111111111111111111");
            }
        }.start();

        new Thread("2---"){
            public void run(){
                System.out.println(this.getName()+"2222222222222222222");
            }
        }.start();
        /*
         * 通过setName(String)方法可以设置线程对象的名字
         */
        Thread t1=new Thread(){
            public void run(){
                System.out.println(this.getName()+"3333333333333333333333333");
            }
        };
        t1.setName("3---");     //设置线程名
        t1.start();             //最后不要忘记调用start()方法
        //或者可以这样写:
        new Thread(){
            public void run(){
                this.setName("4---");
                System.out.println(this.getName()+"4444444444444444444444444");
            }
        }.start();;

    }

}

2.抓获正在执行的线程对象

package com.fenqing.duoxiancheng;

public class d5_currentThread {

    public static void main(String[] args) {
        new Thread(){
            public void run(){
                System.out.println("aaaaaaa");
            }
        }.start();

        new Thread(new Runnable(){

            @Override
            public void run() {
                //Thread.currentThread()获取当前正在执行的线程(对象)
                System.out.println(Thread.currentThread().getName()+"bbbbbbb");
            }

        }).start();

        Thread.currentThread().setName("这是主线程哦");
        System.out.println(Thread.currentThread().getName()); //获取主线程的线程名
    }

}

3.sleep休眠线程

package com.fenqing.duoxiancheng;

public class d6_sleepMothod {
    /*
     * sleep() 是静态方法,可以直接调用,Thread.sleep()
     * sleep(参数1,参数2)  参数1表示毫秒,参数2表示纳秒,一般情况下不用纳秒,只用一个参数。
     * 程序执行时会等待参数1的时间间隔再继续执行程序。
     * 使用时,会出现中断异常,抛出即可
     */
    public static void main(String[] args) {
        new Thread(){
            public void run(){
                for(int i=0;i<100;i++){
                    System.out.println("线程"+this.getName()+"执行了"+(i+1)+"次");
                    try {
                        Thread.sleep(1000);         //1000毫秒=1秒
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                }

            }
        }.start();
    }

}

4.setDaemon()设置守护线程

设置一个线程为守护线程, 该线程不会单独执行, 当其他非守护线程都执行结束后, 自动也结束了

package com.fenqing.duoxiancheng;

public class d7_daemonMethod {
    public static void main(String[] args) {
        Thread t1=new Thread(){
            public void run(){
                for(int i=1;i<11;i++){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"执行了"+i+"次");
                }
            }
        };

        Thread t2=new Thread(){
            public void run(){
                for(int i=1;i<21;i++){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"执行了"+i+"次");
                    }
        };

        t2.setDaemon(true);     //设置t2为守护线程,当t1结束,t2也结束了,
                                //但是有时候会因为有时间缓冲,所以当t1结束时,t2的某些语句也会出现在控制台上
        t1.start();
        t2.start();
    }
}

5.join加入线程

相当于插队。

package com.fenqing.duoxiancheng;

public class d8_join {
    /*
     * join(), 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续
     * join(int), 可以等待指定的毫秒之后继续
     */
    public static void main(String[] args) {
        final Thread t1=new Thread(){
            public void run(){
                for(int i=1;i<11;i++){
                    System.out.println(this.getName()+"执行了"+i+"次"); 
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t2=new Thread(){
            public void run(){
                for(int i=1;i<21;i++){
                    if(i==6){
                        try {
                            //t1.join();        //不设置时间,就要等t1全部执行完
                            t1.join(50);        //设置插队的时间50ms,
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(this.getName()+"执行了");

                }
            }
        };

        t1.start();
        t2.start();
    }
}

6.礼让线程

package com.fenqing.duoxiancheng;

public class d9_yield {
    /*
     * 让出cpu让别的线程做
     */
    public static void main(String[] args) {
        myThread m1=new myThread();
        m1.start();

        new myThread().start();     //定义两个线程
    }

}
class myThread extends Thread{
    public void run(){
        for(int i=0;i<100;i++){
            if(i%10==0){
                Thread.yield();     //当条件满足时,礼让
            }
            System.out.println(this.getName()+"——————"+(i+1));  //否则打印
        }
    }
}

礼让线程结果:
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值