進程:進程是操做系統結構的基礎;是一次程序的執行;是一個程序及其數據在處理機上順序執行時所發生的活動;是程序在一個數據集合上運行的過程,它是系統進行資源分配和調度的一個獨立單位;進程是受操做系統管理的基本運行單元。java
線程:能夠理解成是在進程中獨立運行的子任務;使用多線程也就是在使用異步。線程分爲兩種一種是用戶線程,一種是守護線程;守護線程是一種特殊的線程,當進程中不存在非守護線程了,則守護線程自動銷燬(垃圾回收線程),setDaemon(true)設置線程爲守護線程。多線程
package chapter1.create; public class MyThread extends Thread{ @Override public void run() { super.run(); System.out.println("MyThread"); } }
package chapter1.create; public class Run { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); System.out.println("end!"); } }
運行結果以下:異步
end!
MyThreadide
線程是一個子任務,CUP以不肯定的方式,或者說是隨機的時間來調用線程中的run方法,因此就會出現先打印end!,再打印MyThread了。this
package chapter1.create; public class MyRunnable implements Runnable{ @Override public void run() { System.out.println("運行中..."); } }
package chapter1.create; public class Run { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); System.out.println("運行結束"); } }
運行結果:spa
運行結束
運行中...
Thread.java類也實現了Runnable接口,也就意味着Thread(myRunnable)能夠傳入一個Thread類的對象,這樣作能夠將一個Thread對象中的run方法交由其餘的線程進行調用。操作系統
使用繼承Thread類的方式建立線程時,最大的侷限就是不支持多繼承。線程
package chapter1.create; public class MehthodCurrentThreadTest { static class CountOperate extends Thread{ public CountOperate() { System.out.println("CountOperate init----begin"); System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName()); System.out.println("this.getName()="+this.getName()); System.out.println("CountOperate init----end"); } @Override public void run() { super.run(); System.out.println("run----begin"); System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName()); System.out.println("this.getName()="+this.getName()); System.out.println("run----end"); } } public static void main(String[] args) { CountOperate countOperate = new CountOperate(); Thread t = new Thread(countOperate); t.setName("A"); t.start(); } }
運行結果:code
CountOperate init----begin
Thread.currentThread().getName()=main
this.getName()=Thread-0
CountOperate init----end
run----begin
Thread.currentThread().getName()=A
this.getName()=Thread-0
run----end對象
判斷線程是否爲中斷狀態的方法:
中止線程的方法:
package chapter1.create.threadinterrupt; public class MyThread extends Thread{ @Override public void run() { super.run(); for(int i=0;i<500000;i++) { System.out.println("i="+(i+1)+"interrupted------------"+this.isInterrupted()); } } }
package chapter1.create.threadinterrupt; public class Run { public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } } }
運行結果:
i=499992interrupted------------true
i=499993interrupted------------true
i=499994interrupted------------true
i=499995interrupted------------true
i=499996interrupted------------true
i=499997interrupted------------true
i=499998interrupted------------true
i=499999interrupted------------true
i=500000interrupted------------true
在沉睡中中止:
package chapter1.create.threadinterrupt; public class StopWhenSleepTest { static class MyThread extends Thread{ @Override public void run() { super.run(); try { System.out.println("run begin"); Thread.sleep(200000); System.out.println("run end"); } catch (InterruptedException e) { System.out.println("進MyThread.java類run方法中的catch了。"+this.isInterrupted()); e.printStackTrace(); } } } public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } System.out.println("Main end"); } }
運行結果:
run begin
Main end
進MyThread.java類run方法中的catch了。false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at chapter1.create.threadinterrupt.StopWhenSleepTest$MyThread.run(StopWhenSleepTest.java:12)
若是在sleep狀態下中止線程,會進入catch,而且清楚中止狀態值,使之成爲false。先interrupt再sleep也會進入catch,此處不作贅述。
package chapter1.create.threadinterrupt; public class StopThreadByExceptionTest { static class MyThread extends Thread{ @Override public void run() { super.run(); try { for(int i=0;i<500000;i++) { if(this.isInterrupted()) { System.out.println("已是中止狀態了!我要退出了"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } } catch (InterruptedException e) { System.out.println("進MyThread.java類run方法中的catch了。"); e.printStackTrace(); } } } public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(2000); myThread.interrupt(); } catch (Exception e) { System.out.println("main catch."); e.printStackTrace(); } } }
運行結果:
i=420003
i=420004
i=420005
i=420006
i=420007
i=420008
i=420009
i=420010
i=420011
已是中止狀態了!我要退出了
進MyThread.java類run方法中的catch了。
java.lang.InterruptedException
at chapter1.create.threadinterrupt.StopThreadByExceptionTest$MyThread.run(StopThreadByExceptionTest.java:14)
setPriority(int priority):值的範圍1-10,設置線程優先級有助於幫「線程規劃器」肯定在下一次選擇哪個線程來優先執行。