java多线程---停止、暂停一个线程

1.使用线程的stop方法,但是这个方法已经被deprecated了,因为使用它会释放锁并停掉线程,但是会出现数据不一致以及重要资源不能正确释放的问题

2.使用interrupt方法:调用线程的这个方法会修改线程的中断标志位真,可以通过interrupted和isInterrupted这两个方法来判断线程是否已经被中断,区别是第一个方法在调用之后会把状态改为false,第二个方法则不会

使用interrupt方法来中断一个线程执行的代码实现如下:

package com.zcj.thread01;

public class StopThread {
    public static void main(String[] args) {
		Thread thread = new Thread(new Runnable01());
		thread.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread.interrupt();
	}
}

class Runnable01 implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try{
			while(true){
			System.out.println("线程一直执行中");
			//有二种方式来中断线程的执行,不采取任何措施的话,线程会进行到执行完
			//第一种:使用抛出异常的办法
				if(Thread.currentThread().interrupted()){
					System.out.println("线程被中断了");
					throw new InterruptedException();
				}
			//第二种:使用return;但是一般推荐第一种
//				if(Thread.currentThread().interrupted()){
//					System.out.print("线程被中断了");
//					return;
//				}
			}
		}catch(InterruptedException e){
			e.printStackTrace();
			return;
		}
	}
	
}
3.暂停线程:已经废弃的suspend和resume方法

会出现独占的情况:因为即使使用suspend暂停了线程,但是线程并不会释放自身所占用的锁,这样会导致资源的独占,甚至会出现死锁的情况

会出现数据不一致的情况:一个线程修改了部分数据然后挂起,另外一个线程读取数据,但是这个可以使用共享资源的同步来解决

下面演示了死锁的情况:

package com.zcj.thread02;

public class DeathLock {
    private String lock1 ="lock1";
    private String lock2 ="lock2";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DeathLock death = new DeathLock();
        Thread thread01 = new Thread(death.new Runnable01());
        Thread thread02 = new Thread(death.new Runnable02());
        thread01.start();
        thread02.start();
        try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        thread01.resume();
        thread02.resume();
	}
	
	class Runnable01 implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			synchronized(lock1){
				System.out.println("线程1获得了锁1");
				System.out.println("线程1暂停了");
				Thread.currentThread().suspend();
				System.out.println("线程1恢复了");
				synchronized(lock2){
					System.out.println("线程1获得了锁2");
					
					
				}
			}
		}
		
	}

	class Runnable02 implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			synchronized(lock2){
				System.out.println("线程2获得了锁2");
				System.out.println("线程2暂停了");
				Thread.currentThread().suspend();
				System.out.println("线程2恢复了");
				synchronized(lock1){
					System.out.println("线程2获得了锁1");
					
					
				}
			}
		}
		
	}

}

线程1获得了锁1
线程1暂停了
线程2获得了锁2
线程2暂停了
线程2恢复了
线程1恢复了
从运行结果来看,这里发生了死锁,导致线程1不能获得2锁,线程2不能获得1锁



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值