201771010125王瑜《面向對象程序設計(java) 》第十六週學習總結

                      201771010125王瑜《面向對象程序設計(java) 》第十六週學習總結html

一 理論知識java

1.程序是一段靜態的代碼;它是應用程序執行的藍本。多線程

    進程是程序的一次動態執行,它對應了從代碼加載、執行至執行完畢的一個完整過程。學習

2.多線程是進程執行過程當中產生的多條執行線索,縣城不能獨立存在,必須存在於進程中,同一進程的各線程間共享進程空間的數據測試

3.Java實現多線程有兩種途徑:spa

 (1)建立Thread類的子類:線程

      -定義Thread類的子類並實現用戶線程操做,即 run()方法的實現;在適當的時候啓動線程,因爲Java只支持單重繼承,用這種方法定義的類不可再繼承其餘父類。設計

 (2)在程序中定義實現Runnable接口的類:3d

        -首先設計一個實現Runnable接口的類,而後在類中根據須要重寫run方法,再建立該類對象,以此對象爲參數創建Thread 類的對象;調用Thread類對象的start方法啓動線程,將 CPU執行權轉交到run方法調試

4.線程阻塞:

(1)等待阻塞:經過調用線程的wait()方法,讓線程等待某工做的完成。

(2)同步阻塞:線程在獲取synchronized同步鎖失敗(由於鎖被其它線程所佔用),它會進入同步阻塞狀態。

  (3)其餘阻塞:經過調用線程的sleep()或join() 或發出了I/O請求時,線程會進入到阻塞狀態。

5.線程的狀態

    -利用各線程的狀態變換,能夠控制各個線程輪流使用CPU,體現多線程的並行性特徵。 

    -線程有以下7種狀態:

    New (新建)

    Runnable (可運行)

    Running(運行)

    Blocked (被阻塞)

    Waiting (等待)

    Timed waiting (計時等待) 

    Terminated (被終止)

6.多線程調度

    -Java 的線程調度採用優先級策略:

        優先級高的先執行,優先級低的後執行;

        多線程系統會自動爲每一個線程分配一個優先級,缺省時,繼承其父類的優先級;

        任務緊急的線程,其優先級較高;

        同優先級的線程按「先進先出」的隊列原則。

二 實驗部分

一、實驗目的與要求

(1) 掌握線程概念;

(2) 掌握線程建立的兩種技術;

(3) 理解和掌握線程的優先級屬性及調度方法;

(4) 掌握線程同步的概念及實現技術;

二、實驗內容和步驟

實驗1:測試程序並進行代碼註釋。

測試程序1:

l 在elipse IDE中調試運行ThreadTest,結合程序運行結果理解程序;

l 掌握線程概念;

l 掌握用Thread的擴展類實現線程的方法;

l 利用Runnable接口改造程序,掌握用Runnable接口建立線程的方法。

class Lefthand extends Thread { 

   public void run()

   {

       for(int i=0;i<=5;i++)

       {  System.out.println("You are Students!");

           try{   sleep(500);   }

           catch(InterruptedException e)

           { System.out.println("Lefthand error.");}    

       } 

  } 

}

class Righthand extends Thread {

    public void run()

    {

         for(int i=0;i<=5;i++)

         {   System.out.println("I am a Teacher!");

             try{  sleep(300);  }

             catch(InterruptedException e)

             { System.out.println("Righthand error.");}

         }

    }

}

public class ThreadTest 

{

     static Lefthand left;

     static Righthand right;

     public static void main(String[] args)

     {     left=new Lefthand();

           right=new Righthand();

           left.start();

           right.start();

     }

}

 

測試結果:

 

用runable接口實習:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Lefthand implements Runnable{
    public void run()
    {
       
        for ( int i= 0 ;i<= 5 ;i++)
        {  System.out.println( "You are Students!" );
            try { Thread.sleep( 500 );   }
            catch (InterruptedException e)
            { System.out.println( "Lefthand error." );}   
        }
   }
}
class Righthand implements Runnable {
     public void run()
     {
         
           for ( int i= 0 ;i<= 5 ;i++)
          {   System.out.println( "I am a Teacher!" );
              try {  Thread.sleep( 300 );  }
              catch (InterruptedException e)
              { System.out.println( "Righthand error." );}
          }
     }
}
public class ThreadTest
{
      static Lefthand left;
      static Righthand right;
      public static void main(String[] args)
      {   
            Runnable lefthand = new Lefthand();
            Thread left= new Thread(lefthand);
             left.start();
             Runnable righthand = new Righthand();
             Thread right= new Thread(righthand);
               right.start();
               
      }
}

測試程序2:

l 在Elipse環境下調試教材625頁程序14-一、14-2 、14-3,結合程序運行結果理解程序;

l 在Elipse環境下調試教材631頁程序14-4,結合程序運行結果理解程序;

l 對比兩個程序,理解線程的概念和用途;

l 掌握線程建立的兩種技術。

測試程序3:分析如下程序運行結果並理解程序。

class Race extends Thread {

  public static void main(String args[]) {

    Race[] runner=new Race[4];

    for(int i=0;i<4;i++) runner[i]=new Race( );

   for(int i=0;i<4;i++) runner[i].start( );

   runner[1].setPriority(MIN_PRIORITY);

   runner[3].setPriority(MAX_PRIORITY);}

  public void run( ) {

      for(int i=0; i<1000000; i++);

      System.out.println(getName()+"線程的優先級是"+getPriority()+"已計算完畢!");

    }

}

 

 

 

測試程序4

l 教材642頁程序模擬一個有若干帳戶的銀行,隨機地生成在這些帳戶之間轉移錢款的交易。每個帳戶有一個線程。在每一筆交易中,會從線程所服務的帳戶中隨機轉移必定數目的錢款到另外一個隨機帳戶。

l 在Elipse環境下調試教材642頁程序14-五、14-6,結合程序運行結果理解程序;

 

  建立兩個線程,每一個線程按順序輸出5次「你好」,每一個「你好」要標明來自哪一個線程及其順序號。

 

複製代碼
package Project2;
class Lefthand extends Thread { 
   public void run()
   {
       for(int i=0;i<5;i++)
       {  System.out.println("你好 L");
           try{   sleep(500);   }
           catch(InterruptedException e)
           { System.out.println("Lefthand error.");}    
       } 
  } 
}
class Righthand extends Thread {
    public void run()
    {
         for(int i=0;i<5;i++)
         {   System.out.println("你好 R");
             try{  sleep(300);  }
             catch(InterruptedException e)
             { System.out.println("Righthand error.");}
         }
    }
}
public class ThreadTest 
{
     static Lefthand left;
     static Righthand right;
     public static void main(String[] args)
     {     left=new Lefthand();
           right=new Righthand();
           left.start();
           right.start();
     }
}
hello
複製代碼

 三 實驗總結:

  經過此次實驗,學習了線程的有關知識,理解了實現多線程的兩種途徑:Thread子類和Runnable接口這兩種方法,但在bank的實驗中while循環產生隨機數以及鎖的概念不是很理解。但願老師能夠再講解一下

相關文章
相關標籤/搜索