日出星辰

          線程學(xué)習(xí)筆記【1】----進程、線程概念及創(chuàng)建線程

          1.進程與線程

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

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

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

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

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

            進程有三種狀態(tài):就緒、執(zhí)行、阻塞。

            

          2.線程創(chuàng)建方式

             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,處于就緒狀態(tài),等待cpu
          		t2.start(); // 啟動線程t2,處于就緒狀態(tài),等待cpu
          		t1.run(); // 主線程main調(diào)用對象t1的run方法
          	}
          
          	public void run() {
          		System.out.println("thread's name is "
          				+ Thread.currentThread().getName());
          	}
          
          }
          

          運行結(jié)果可能是:

          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());
          	}
          }
          

          運行結(jié)果: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();
          }
          }
          所以自己創(chuàng)建的線程要重寫run方法,把要執(zhí)行的內(nèi)容放到run()中,所以要實現(xiàn)接口或繼承進而產(chǎn)生子類
          */

          //創(chuàng)建線程的方式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(); //不寫 線程無法啟動


          //創(chuàng)建線程的方式2 runnable方式(實現(xiàn)) 推薦使用
          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();

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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 漠河县| 云和县| 晋城| 松溪县| 鲁山县| 隆化县| 桃江县| 林周县| 包头市| 昭平县| 梨树县| 南岸区| 志丹县| 高安市| 年辖:市辖区| 若尔盖县| 安塞县| 紫阳县| 从化市| 无为县| 尚志市| 静海县| 宜兰县| 平顶山市| 永嘉县| 皋兰县| 吉安县| 玛纳斯县| 平度市| 长治县| 钟祥市| 杭锦旗| 江安县| 竹北市| 宜兴市| 新蔡县| 崇阳县| 镇安县| 镇原县| 亚东县| 察哈|