当前位置:   article > 正文

java暂停线程的方法_Java修炼——暂停线程的四个方法

thread.currentthread()暂定线程

线程的获取方法: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()+"结束休眠");

}

}

运行结果:

0d723cd88192459324a5875baaecb143.gif

暂停线程之 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+"------------------");

}

}

}

运行结果:

7d86cbc87f3a6296a3e27e4fc80135b5.png

暂停线程之  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);

}

}

}

运行结果:

cb6efeb152f3289bcb1cfcafe4b2d755.png

暂停线程之  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);

}

}

}

运行结果:

e2f08b69a6a64a28879107d3232a730d.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1004250
推荐阅读
相关标签
  

闽ICP备14008679号