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

线程的获取方法: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);
		}
	}
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值