<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>中文 &#187; hanshileiai</title>
	<atom:link href="http://software.intel.com/zh-cn/blogs/author/hanshileiai/feed/" rel="self" type="application/rss+xml" />
	<link>http://software.intel.com/zh-cn/blogs</link>
	<description></description>
	<lastBuildDate>Mon, 28 May 2012 13:40:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>多线程 -- 常用操作方法</title>
		<link>http://software.intel.com/zh-cn/blogs/2011/09/15/400008627/</link>
		<comments>http://software.intel.com/zh-cn/blogs/2011/09/15/400008627/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 06:52:46 +0000</pubDate>
		<dc:creator>hanshileiai</dc:creator>
				<category><![CDATA[博客征文专栏]]></category>
		<category><![CDATA[并行计算]]></category>

		<guid isPermaLink="false">http://software.intel.com/zh-cn/blogs/2011/09/15/400008627/</guid>
		<description><![CDATA[  1.取得当前线程的名字: currentThread().getName() view plain class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i&#60;3;i++){ System.out.println(Thread.currentThread().getName() + "运行，i = " + i) ; // 取得当前线程的名字 } } }; public class ThreadNameDemo{ public static void main(String args[]){ MyThread mt = new MyThread() ; // 实例化Runnable子类对象 new Thread(mt).start() ; // 系统自动设置线程名称 new Thread(mt,"线程-A").start() ; [...]]]></description>
			<content:encoded><![CDATA[<p> </p>
<p>1.取得当前线程的名字: currentThread().getName()</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i&lt;3;i++){<br />
System.out.println(Thread.currentThread().getName()<br />
+ "运行，i = " + i) ; // 取得当前线程的名字<br />
}<br />
}<br />
};<br />
public class ThreadNameDemo{<br />
public static void main(String args[]){<br />
MyThread mt = new MyThread() ; // 实例化Runnable子类对象<br />
new Thread(mt).start() ; // 系统自动设置线程名称<br />
new Thread(mt,"线程-A").start() ; // 手工设置线程名称<br />
new Thread(mt,"线程-B").start() ; // 手工设置线程名称<br />
new Thread(mt).start() ; // 系统自动设置线程名称<br />
new Thread(mt).start() ; // 系统自动设置线程名称<br />
}<br />
};</p>
<p>2.通过isAlive() 判断线程是否执行完</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i " + t.isAlive()) ; // 判断是否启动<br />
t.start() ; // 启动线程<br />
System.out.println("线程开始执行之后 --&gt; " + t.isAlive()) ; // 判断是否启动<br />
for(int i=0;i " + i) ;<br />
}<br />
// 以下的输出结果不确定<br />
System.out.println("代码执行之后 --&gt; " + t.isAlive()) ; // 判断是否启动</p>
<p>}<br />
};</p>
<p>3.使用join()方法让一个线程强制运行(线程强制运行期间,其他线程无法运行,必须等待此线程完成之后才能继续执行)</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i&lt;50;i++){<br />
System.out.println(Thread.currentThread().getName()<br />
+ "运行，i = " + i) ; // 取得当前线程的名字<br />
}<br />
}<br />
};<br />
public class ThreadJoinDemo{<br />
public static void main(String args[]){<br />
MyThread mt = new MyThread() ; // 实例化Runnable子类对象<br />
Thread t = new Thread(mt,"线程"); // 实例化Thread对象<br />
t.start() ; // 启动线程<br />
for(int i=0;i10){<br />
try{<br />
t.join() ; // 线程强制运行<br />
}catch(InterruptedException e){}<br />
}<br />
System.out.println("Main线程运行 --&gt; " + i) ;<br />
}<br />
}<br />
};</p>
<p>4.线程的休眠</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i&lt;50;i++){<br />
try{<br />
Thread.sleep(500) ; // 线程休眠<br />
}catch(InterruptedException e){}<br />
System.out.println(Thread.currentThread().getName()<br />
+ "运行，i = " + i) ; // 取得当前线程的名字<br />
}<br />
}<br />
};<br />
public class ThreadSleepDemo{<br />
public static void main(String args[]){<br />
MyThread mt = new MyThread() ; // 实例化Runnable子类对象<br />
Thread t = new Thread(mt,"线程"); // 实例化Thread对象<br />
t.start() ; // 启动线程<br />
}<br />
};<br />
5.线程的中断操作 : interrupt()</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
System.out.println("1、进入run()方法") ;<br />
try{<br />
Thread.sleep(10000) ; // 线程休眠10秒<br />
System.out.println("2、已经完成了休眠") ;<br />
}catch(InterruptedException e){<br />
System.out.println("3、休眠被终止") ;<br />
return ; // 返回调用处<br />
}<br />
System.out.println("4、run()方法正常结束") ;<br />
}<br />
};<br />
public class ThreadInterruptDemo{<br />
public static void main(String args[]){<br />
MyThread mt = new MyThread() ; // 实例化Runnable子类对象<br />
Thread t = new Thread(mt,"线程"); // 实例化Thread对象<br />
t.start() ; // 启动线程<br />
try{<br />
Thread.sleep(2000) ; // 线程休眠2秒<br />
}catch(InterruptedException e){<br />
System.out.println("3、休眠被终止") ;<br />
}<br />
t.interrupt() ; // 中断线程执行<br />
}<br />
};</p>
<p>6.后台线程:setDaemon()</p>
<p>在Java程序中,只要前台有一个线程在运行,则整个java进程都不会消失,所以此时可以设置一个后台线程,这样即使Java进程结束了,此后台线程依然会继续执行.要想实现这样的操作直接使用setDaemon()方法即可.</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
while(true){<br />
System.out.println(Thread.currentThread().getName() + "在运行。") ;<br />
}<br />
}<br />
};<br />
public class ThreadDaemonDemo{<br />
public static void main(String args[]){<br />
MyThread mt = new MyThread() ; // 实例化Runnable子类对象<br />
Thread t = new Thread(mt,"线程"); // 实例化Thread对象<br />
t.setDaemon(true) ; // 此线程在后台运行<br />
t.start() ; // 启动线程<br />
}<br />
};</p>
<p>7.线程的优先级</p>
<p>view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i&lt;5;i++){<br />
try{<br />
Thread.sleep(500) ; // 线程休眠<br />
}catch(InterruptedException e){}<br />
System.out.println(Thread.currentThread().getName()<br />
+ "运行，i = " + i) ; // 取得当前线程的名字<br />
}<br />
}<br />
};<br />
public class ThreadPriorityDemo{<br />
public static void main(String args[]){<br />
Thread t1 = new Thread(new MyThread(),"线程A") ; // 实例化线程对象<br />
Thread t2 = new Thread(new MyThread(),"线程B") ; // 实例化线程对象<br />
Thread t3 = new Thread(new MyThread(),"线程C") ; // 实例化线程对象<br />
t1.setPriority(Thread.MIN_PRIORITY) ; // 优先级最低<br />
t2.setPriority(Thread.MAX_PRIORITY) ; // 优先级最高<br />
t3.setPriority(Thread.NORM_PRIORITY) ; // 优先级中级<br />
t1.start() ; // 启动线程<br />
t2.start() ; // 启动线程<br />
t3.start() ; // 启动线程<br />
}<br />
};<br />
8.主方法的优先级</p>
<p>view plain<br />
public class MainPriorityDemo{<br />
public static void main(String args[]){<br />
System.out.println("主方法的优先级：" +<br />
Thread.currentThread().getPriority()) ; // 取得主方法的优先级<br />
System.out.println("MAX_PRIORITY = " + Thread.MAX_PRIORITY) ;<br />
System.out.println("NORM_PRIORITY = " + Thread.NORM_PRIORITY) ;<br />
System.out.println("MIN_PRIORITY = " + Thread.MIN_PRIORITY) ;<br />
}<br />
};<br />
9.线程的礼让</p>
<p>在线程操作中,也可以使用yield()方法将一个线程的操作暂时让给其他线程执行.<br />
view plain<br />
class MyThread implements Runnable{ // 实现Runnable接口<br />
public void run(){ // 覆写run()方法<br />
for(int i=0;i&lt;5;i++){<br />
try{<br />
Thread.sleep(500) ;<br />
}catch(Exception e){}<br />
System.out.println(Thread.currentThread().getName()<br />
+ "运行，i = " + i) ; // 取得当前线程的名字<br />
if(i==2){<br />
System.out.print("线程礼让：") ;<br />
Thread.currentThread().yield() ; // 线程礼让<br />
}<br />
}<br />
}<br />
};<br />
public class ThreadYieldDemo{<br />
public static void main(String args[]){<br />
MyThread my = new MyThread() ; // 实例化MyThread对象<br />
Thread t1 = new Thread(my,"线程A") ;<br />
Thread t2 = new Thread(my,"线程B") ;<br />
t1.start() ;<br />
t2.start() ;<br />
}<br />
};</p>
]]></content:encoded>
			<wfw:commentRss>http://software.intel.com/zh-cn/blogs/2011/09/15/400008627/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

