Java修炼——暂停线程的四个方法
阅读原文时间:2023年07月08日阅读:1

线程的获取方法:Thread.currentThread() 后面可以加上获取线程的名字 .getName() 这样就成功获取到了线程的名字。

Sleep会导致当前线程休眠一定时间,进入阻塞状态

Join会导致调用他的线程进入阻塞状态

Yield会导致让他线程进入就绪状态

Stop() 方法   The method stop() from the type Thread is deprecated  已经被淘汰 了。机会不使用。

暂停线程之 Sleep() 方法:

package com.bjsxt.MythreadSleep;

public class MythreadSleep extends Thread{
    @Override
    public void run() {

        try {
            System.out.println(Thread.currentThread().getName()+"线程开始休眠");
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName()+"线程结束休眠");
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName()+"线程休眠异常");

        }
    }
}



package com.bjsxt.MythreadSleep;

import com.bjsxt.MythreadYield.MythreadYield;

public class TestThreadSleep {
    public static void main(String[] args) throws InterruptedException {
        MythreadSleep mythreads=new MythreadSleep();
        Thread t=new Thread(mythreads);
        t.start();
        Thread threads=Thread.currentThread();
        System.out.println("主线程"+threads.getName()+"开始休眠");
        Thread.sleep(4000);
        System.out.println("主线程"+threads.getName()+"结束休眠");
    }
}

运行结果:

暂停线程之 join() 方法 :

join() 方法是唯一一个将调用的他的线程进入阻塞状态,然后等待其他进程完成后,它在进去就绪状态,等待CPU的资源调用。

package com.bjsxt.MythreadJoin;

public class MythreadJoin extends Thread{
    @Override
    public void run() {
        for (int i = 0; i <10; i++) {
            System.out.println("---------"+Thread.currentThread().getName()+"----->"+i);
        }
    }
}



package com.bjsxt.MythreadJoin;

public class TestMythread {
    public static void main(String[] args) throws InterruptedException {
        MythreadJoin mythread=new MythreadJoin();
        Thread thread=new Thread(mythread);
        thread.start();
        for (int i = 0; i <10; i++) {
            if (i==4) {
                thread.join();
            }
            System.out.println(Thread.currentThread().getName()+"---->"+i+"------------------");
        }
    }
}

运行结果:

暂停线程之  yield()  方法:

package com.bjsxt.MythreadYield;

public class MythreadYield extends Thread{
    @Override
    public void run() {
        for (int i = 0; i <10; i++) {
            if (i==3) {
                Thread.yield();
                System.out.println("当前"+Thread.currentThread().getName()+"调用yield()方法,礼让一次");
            }
            System.out.println(Thread.currentThread().getName()+"******************i="+i);
        }
    }
}



package com.bjsxt.MythreadYield;

public class TestMythreadYield {
    public static void main(String[] args) {
        MythreadYield mythread=new MythreadYield();
        Thread t=new Thread(mythread);
        t.start();

        for (int i = 0; i <10; i++) {
            if (i==5) {
                Thread.yield();
                System.out.println("当前"+Thread.currentThread().getName()+"调用yield()方法,礼让一次");
            }
            System.out.println(Thread.currentThread().getName()+"---------i="+i);
        }
    }
}

运行结果:

暂停线程之  Stop()  方法:

package com.bjsxt.MythreadStop;

public class MythreadStop extends Thread{
    @Override
    public void run() {
        for (int i = 0; i <10; i++) {
                System.out.println(Thread.currentThread().getName()+"-----------------"+i);
            }
        }
    }



package com.bjsxt.MythreadStop;

public class TestMythreadStop {
    public static void main(String[] args) {
        MythreadStop mythreadStop=new MythreadStop();
        Thread thread=new Thread(mythreadStop);
        thread.start();
        for (int i = 0; i <10; i++) {
            if (i==3) {
                thread.stop();
            }
            System.out.println(Thread.currentThread().getName()+"---"+i);
        }
    }
}

运行结果: