日出星辰

          線程學習筆記【1】----進程、線程概念及創建線程

          1.進程與線程

             每個進程都獨享一塊內存空間,一個應用程序可以同時啟動多個進程。比如IE瀏覽器,打開一個Ie瀏覽器就相當于啟動了一個進程。

             線程指進程中的一個執行流程,一個進程可以包含多個線程。

             每個進程都需要操作系統為其分配獨立的內存空間,而同一個進程中的多個線程共享這塊空間,即共享內存等資源。

             每次調用java.exe的時候,操作系統都會啟動一個Java虛擬機進程,當啟動Java虛擬機進程時候,Java虛擬機都會創建一個主線程,該線程會從程序入口main方法開始執行。

            Java虛擬機每啟動一個線程,就給會給該線程分配一個線程方法棧,用來存放相關信息(比如局部變量等),線程就在這個棧上運行。所以Java對象中的局部變量都是線程安全的,但實例變量及類變量由于不是保存在棧中,所以不是線程安全的。

            進程有三種狀態:就緒、執行、阻塞。

            

          2.線程創建方式

             Runnable方式:(此種方式靈活,推薦使用)

          public class Thread02 implements Runnable {
          
          	public static void main(String[] args) {
          		Runnable r = new Thread02();
          		Thread t1 = new Thread(r, "t1");
          		/** 
          		 * 	   Thread源碼
          		 *     public Thread(Runnable target, String name) {
          					init(null, target, name, 0);
          	                     }
          		 */
          		Thread t2 = new Thread(r, "t2");
          		t1.start(); // 啟動線程t1,處于就緒狀態,等待cpu
          		t2.start(); // 啟動線程t2,處于就緒狀態,等待cpu
          		t1.run(); // 主線程main調用對象t1的run方法
          	}
          
          	public void run() {
          		System.out.println("thread's name is "
          				+ Thread.currentThread().getName());
          	}
          
          }
          

          運行結果可能是:

          thread's name is t1
          thread's name is main
          thread's name is t2


          Thead方式

          public class Thread03 extends Thread {
          
          	public static void main(String[] args) {
          		Thread03 t1 = new Thread03();     //不注意的情況下寫成了Thread t1=new Thread()   注:Thread03此時就是一個線程了
          		t1.start();
          	}
          
          	public void run() {
          		System.out.println("thread's name is "
          				+ Thread.currentThread().getName());
          	}
          }
          

          運行結果:thread's name is Thread-0

          注意:每次程序運行時除了自定義的線程外還有一個main線程。

           綜合:

          public class Thread01 {
          public static void main(String[] args) {

          Thread thread
          =new Thread();
          thread.start();
          //真正起作用 的是run()
          /**而Thread中的run
          * public void run() {
          if (target != null) {
          target.run();
          }
          }
          所以自己創建的線程要重寫run方法,把要執行的內容放到run()中,所以要實現接口或繼承進而產生子類
          */

          //創建線程的方式1 thread子類方式(繼承)
          Thread thread1=new Thread(){
          public void run() {
          while(true){
          try {
          Thread.sleep(
          500);//休息500毫秒
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          //Thread.currentThread()得到當前線程
          System.out.println("線程1的名字是 "+Thread.currentThread().getName());
          }
          }
          };
          // thread1.start(); //不寫 線程無法啟動


          //創建線程的方式2 runnable方式(實現) 推薦使用
          Thread thread2=new Thread(new Runnable(){

          public void run() {

          while(true){
          try {
          Thread.sleep(
          300);
          }
          catch (InterruptedException e) {
          e.printStackTrace();
          }
          System.out.println(
          "thread2'name is "+Thread.currentThread().getName());

          }
          }});
          // thread2.start();

          //執行的是thread
          new Thread(new Runnable(){
          public void run() {
          System.out.println(
          "runnable "+Thread.currentThread().getName());
          }}){
          public void run() { //子類中的run方法覆蓋父類中的run方法,這樣就不會執行runnable
          System.out.println("thread "+Thread.currentThread().getName());
          }
          }.start();
          }
          /***
          * 在單個cpu中執行多線程很有可能降低執行效率而不是提高 一個人在不同地方做同一件事情
          */
          }

          posted on 2011-08-27 22:33 日出星辰 閱讀(123) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 郎溪县| 临夏县| 铜山县| 合肥市| 庆元县| 珠海市| 霍城县| 漳浦县| 石狮市| 尚志市| 金山区| 崇仁县| 汪清县| 华池县| 和林格尔县| 新田县| 贵定县| 青州市| 华容县| 荆门市| 阿拉善右旗| 岢岚县| 洛扎县| 台州市| 丘北县| 浦城县| 玛曲县| 运城市| 报价| 威海市| 安庆市| 临桂县| 玛曲县| 孟连| 来安县| 德兴市| 泗洪县| 崇义县| 南安市| 克山县| 岢岚县|