码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程控制函数setDaemon,join,interupt

时间:2014-05-10 03:53:22      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:java   多线程操作   join   interupt   setdaemon   

1、setDeamon

设置线程为后台运行的函数

public class SetDaemon
{
	public static void main(String[] args) throws InterruptedException
	{
		Thread tt=new Thread(new ThreadTest());
		tt.setDaemon(true);						//设置程序为后台运行
		tt.start();
		Thread.sleep(3);
	}
}

class ThreadTest implements Runnable
{
	public void run()
	{
		while(true)
		{
			System.out.println(Thread.currentThread().getName()+" is running...");	
		}

	}
}

bubuko.com,布布扣

可见当父线程后台运行的线程自动结束。

2、join

强制CPU执行某个线程

public class SetDaemon
{
	public static void main(String[] args) throws InterruptedException
	{
		Thread tt=new Thread(new ThreadTest());
		tt.start();
		
		for(int i=0;i<10;i++)
		{
			if(i==5)
			{
				tt.join();
			}
			System.out.println(Thread.currentThread().getName()+i+" is running...");
		}
	}
}

class ThreadTest implements Runnable
{
	public void run()
	{
		for(int i=0;i<5;i++)
		{
			System.out.println(Thread.currentThread().getName()+i+" is running...");	
		}

	}
}
bubuko.com,布布扣

3、interrupt

中断线程

public class SetDaemon
{
	public static void main(String[] args) throws InterruptedException
	{
		Thread tt=new Thread(new ThreadTest());
		tt.start();
		Thread.sleep(2000);
		System.out.println("The subthread is interupted in the main thread.");
		tt.interrupt();
		tt.join();
		System.out.println("The main thread is over.");
	}
}

class ThreadTest implements Runnable
{
	public void run()
	{
		try
		{
			System.out.println("The subthread is sleeping...");
			Thread.sleep(200000);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("The subthread is interupted.");
			return;
		}
		System.out.println("The subthread is over.");
	}
}
bubuko.com,布布扣

注意:这里的线程中断和通常所讲的硬件中断并不是同一个概念,硬件中断是...(这个就不要讲啦),这里的中断可以理解成线程的状态被设置成了中断状态(即挂起了一个小旗,告诉其它线程‘哥处于中断状态’,禁止某些操作),此时执行某些函数会触发异常,被中断的线程进入到异常处理代码段。

请仔细体会以下代码:

public class SetDaemon
{
	public static void main(String[] args) throws InterruptedException
	{
		Thread tt=new Thread(new ThreadTest());
		tt.start();
		Thread.sleep(5);
		System.out.println("The subthread is interupted in the main thread.");
		tt.interrupt();
		tt.join();
		System.out.println("The main thread is over.");
	}
}

class ThreadTest implements Runnable
{
	public void run()
	{
		try
		{
			while(true)
			{
				System.out.println(Thread.currentThread().getName()+" is running...");
				Thread.sleep(1);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("The subthread is interupted.");
			return;
		}
		//System.out.println("The subthread is over.");
	}
}
bubuko.com,布布扣

可以使用isInterupt()查看线程是否被中断。




java多线程控制函数setDaemon,join,interupt,布布扣,bubuko.com

java多线程控制函数setDaemon,join,interupt

标签:java   多线程操作   join   interupt   setdaemon   

原文地址:http://blog.csdn.net/cjc211322/article/details/25421087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!