前面的文章已經介紹了有關線程的調度,接下來介紹如何使用方法對線程進行控制
1、線程休眠
public static void sleep(long millis)
/* * 線程休眠 * public static void sleep(long millis) */ public class ThreadSleepDemo { public static void main(String[] args) { ThreadSleep ts1 = new ThreadSleep(); ThreadSleep ts2 = new ThreadSleep(); ThreadSleep ts3 = new ThreadSleep(); ts1.setName("zhangsan"); ts2.setName("lisi"); ts3.setName("wangwu"); ts1.start(); ts2.start(); ts3.start(); } } public class ThreadSleep extends Thread { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(getName() + ":" + x + ",日期:" + new Date()); // 睡眠 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2、線程加入
public final void join()
public class ThreadJoin extends Thread { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(getName() + ":" + x); } } } /* * public final void join():等待該線程終止。 */ public class ThreadJoinDemo { public static void main(String[] args) { ThreadJoin tj1 = new ThreadJoin(); ThreadJoin tj2 = new ThreadJoin(); ThreadJoin tj3 = new ThreadJoin(); tj1.setName("zhangsan"); tj2.setName("lisi"); tj3.setName("wangwu"); tj1.start(); try { tj1.join(); } catch (InterruptedException e) { e.printStackTrace(); } tj2.start(); tj3.start(); } }
3、線程禮讓
public static void yield()
public class ThreadYield extends Thread { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(getName() + ":" + x); Thread.yield(); } } } /* * public static void yield():暫停當前正在執行的線程對象,並執行其他線程。 * 讓多個線程的執行更和諧,但是不能靠它保證一個線程一次。 */ public class ThreadYieldDemo { public static void main(String[] args) { ThreadYield ty1 = new ThreadYield(); ThreadYield ty2 = new ThreadYield(); ty1.setName("zhangsan"); ty2.setName("lisi"); ty1.start(); ty2.start(); } }
4、後台線程
public final void setDaemon(boolean on)
public class ThreadDaemon extends Thread { @Override public void run() { for (int x = 0; x < 100; x++) { System.out.println(getName() + ":" + x); } } } /* * public final void setDaemon(boolean on):將該線程標記為守護線程或用戶線程。 * 當正在運行的線程都是守護線程時,Java 虛擬機退出。 該方法必須在啟動線程前調用。 * */ public class ThreadDaemonDemo { public static void main(String[] args) { ThreadDaemon td1 = new ThreadDaemon(); ThreadDaemon td2 = new ThreadDaemon(); td1.setName("zhangsan"); td2.setName("lisi"); // 設置收獲線程 td1.setDaemon(true); td2.setDaemon(true); td1.start(); td2.start(); Thread.currentThread().setName("wuyudong"); for (int x = 0; x < 5; x++) { System.out.println(Thread.currentThread().getName() + ":" + x); } } }
5、中斷線程
public final void stop()
public void interrupt()
import java.util.Date; public class ThreadStop extends Thread { @Override public void run() { System.out.println("開始執行:" + new Date()); // 我要休息10秒鐘 try { Thread.sleep(10000); } catch (InterruptedException e) { // e.printStackTrace(); System.out.println("線程被終止了"); } System.out.println("結束執行:" + new Date()); } } /* * public final void stop():讓線程停止,過時了,但是還可以使用。 * public void interrupt():中斷線程。 把線程的狀態終止,並拋出一個InterruptedException。 */ public class ThreadStopDemo { public static void main(String[] args) { ThreadStop ts = new ThreadStop(); ts.start(); // 超過三秒不醒過來,就干掉你 try { Thread.sleep(3000); // ts.stop(); ts.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } }