线程Thread(JAVA) 篇九 停止线程以及线程其他方法(join)

停止线程有两种途径:
1.stop()方法停止线程 – 但是该方法不安全 已过时
2.run()方法结束 终止线程
任务中通常都有循环结构,通过标志位控制循环就能结束线程任务。
如下代码所示:

class StopThread implements Runnable
{
	private boolean flag=true;
	@Override
	public synchronized void run()
	{
		while(flag)
		{	
			System.out.println(Thread.currentThread().getName()+"...");
		}
	}
	//设置标记位
	public void setFlag()
	{
		flag = false;
	}
}

public class Test
{
	public static void main(String[] args)
	{
		StopThread stopThread = new StopThread();
		Thread thread = new Thread(stopThread);
		Thread thread2 = new Thread(stopThread);
		thread.start();
		thread2.start();
		int num = 0;
		for( ; ;)
		{
			if(num++ == 50) 
			{
			    stopThread.setFlag();
				break;
			}
			System.out.println("main..." + num);
		} 
		System.out.println("OVER");
	}
}

通过标记位置停止代码。
但是,当线程处于冻结状态,无法读取标记,则无法通过此方法停止线程,需要使用interrupt()方法将线程从冻结状态强制恢复到运行状态来,让线程具备CPU的执行资格,但是强制动作发生了InterruptedException需要处理。
如下所示:

class StopThread implements Runnable
{
	private boolean flag=true;
	@Override
	public synchronized void run()
	{
		while(flag)
		{	
			try
			{
				wait();
			} catch (Exception e)
			{
				System.out.println(Thread.currentThread().getName()+"..." +  e);
				flag = false;//处理异常
			}
			System.out.println(Thread.currentThread().getName()+"...");
		}
	}
	//设置标记位
	public void setFlag()
	{
		flag = false;
	}
}

public class Test
{
	public static void main(String[] args)
	{
		StopThread stopThread = new StopThread();
		Thread thread = new Thread(stopThread);
		Thread thread2 = new Thread(stopThread);
		thread.start();
		thread2.start();
		int num = 0;
		for( ; ;)
		{
			if(num++ == 5) 
			{
				thread.interrupt();//强制唤醒线程,抛出异常
				thread2.interrupt();
		//	    stopThread.setFlag();
				break;
			}
			System.out.println("main..." + num);
		} 
		System.out.println("OVER");
	}
}

运行结果如下:

main…1
main…2
main…3
main…4
main…5
OVER
Thread-0…java.lang.InterruptedException
Thread-0…
Thread-1…java.lang.InterruptedException
Thread-1…

线程中常用的方法有setDaemon()方法:该方法将线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,JAVA虚拟机退出。并且该方法必须在启动线程前调用。

class StopThread implements Runnable
{
	private boolean flag=true;
	@Override
	public synchronized void run()
	{
		while(flag)
		{	
			try
			{
				wait();
			} catch (Exception e)
			{
				System.out.println(Thread.currentThread().getName()+"..." +  e);
				flag = false;//处理异常
			}
			System.out.println(Thread.currentThread().getName()+"...");
		}
	}
	//设置标记位
	public void setFlag()
	{
		flag = false;
	}
}

public class Test
{
	public static void main(String[] args)
	{
		StopThread stopThread = new StopThread();
		Thread thread = new Thread(stopThread);
		Thread thread2 = new Thread(stopThread);
		thread.start();
		thread2.setDaemon(true);//守护线程(后台线程),当所有前台线程结束时候,不管该线程处于什么状态,也自动结束
		thread2.start();
		int num = 0;
		for( ; ;)
		{
			if(num++ == 5) 
			{
				thread.interrupt();//强制唤醒线程,抛出异常
		//		thread2.interrupt();
		//	    stopThread.setFlag();
				break;
			}
			System.out.println("main..." + num);
		} 
		System.out.println("OVER");
	}
}

线程中常用的方法有join()方法:该方法用于申请调用此方法的线程加入到运行,即使得该线程获取执行权和执行资格,其他正在运行的线程释放执行权。

class Demo implements Runnable
{
	@Override
	public void run()
	{
		for(int i = 5; i > 0; i--) 
		{
			System.out.println(Thread.currentThread().getName()+" : "+i);
		}
	}
}

public class Test
{
	public static void main(String[] args) throws InterruptedException
	{
		Demo demo = new Demo();
		
		Thread thread1 = new Thread(demo);
		Thread thread2 = new Thread(demo);
		Thread thread3 = new Thread(demo);
		
		thread1.start();
		thread1.join();
		thread2.start();
		thread3.start();
		for (int i = 0; i < 5; i++)
		{
			System.out.println(Thread.currentThread().getName());
		}
	}
}

运行结果如下:

Thread-0 : 5
Thread-0 : 4
Thread-0 : 3
Thread-0 : 2
Thread-0 : 1
main
main
main
main
main
Thread-1 : 5
Thread-2 : 5
Thread-1 : 4
Thread-2 : 4
Thread-2 : 3
Thread-1 : 3
Thread-1 : 2
Thread-1 : 1
Thread-2 : 2
Thread-2 : 1

可以看出Thread-0运行完毕主线程才能继续运行。

线程中常用的方法有setPriority()方法:该方法设置线程的优先级。优先级越大,被执行的概率越大。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值