JAVA學習筆記,java學習筆記林信良

來源:互聯網
上載者:User

JAVA學習筆記,java學習筆記林信良

之前學習的都是關於順序編程的知識,程式在任意時刻都只能執行一個步驟。線程作為我接觸並發編程的第一堂課,我感覺很興奮。

1、定義任務

   實現Runnable介面並編寫run()方法(線程將會執行此方法內代碼)。

class LiftOff implements Runnable {  protected int countDown = 10;private static int taskCount = 0;private final int id = taskCount++;public LiftOff() {}public LiftOff(int countDown) {this.countDown = countDown;}public String status() {return "#" + id + "("+ (countDown > 0 ? String.valueOf(countDown) : "Liftoff!")+ "),";}public void run() {while (countDown-- > 0) {System.out.print(status());Thread.yield();}System.out.println();}}public class ThreadTest {public static void main(String[] args) {LiftOff launch = new LiftOff();launch.run();}}/* * Output: #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(Liftoff!), */// :~
如上代碼,從Runnable匯出一個類,再調用它的run()方法,但這個方法不會產生任何內在的線程能力。

要實現線程行為,必須將它附著到線程上。


2、建立和啟動線程

A. 通過Thread構造器。將Runnable對象提交給一個Thread構造器,再調用Thread對象的start()方法啟動線程。

public class ThreadTest {public static void main(String[] args) {Thread t = new Thread(new LiftOff());t.start();System.out.println("Waiting for LiftOff");}}/* * Output: Waiting for LiftOff * #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(Liftoff!), */// :~
在main()線程中,start()迅速的返回,此時子線程t執行的LiftOff.run()任務還未結束。這時兩個線程並發運行,通過輸出也可以看出。


B. 使用Executor。Executor是java.util.concurrent包中的執行器,可以更好的管理Thread對象。

a. CachedThreadPool,為每個任務都建立一個線程。

b. FixedThreadPool,使用有限的線程集來執行所提交的任務。

c. SingeThreadExecutor,如線程數量為1的FixedThreadPool。

public class ThreadTest {public static void main(String[] args) {ExecutorService exec1 = Executors.newCachedThreadPool();ExecutorService exec2 = Executors.newFixedThreadPool(5);//線程集限制數目為5ExecutorService exec3 = Executors.newSingleThreadExecutor();exec1.execute(new LiftOff());exec1.shutdown();//防止新任務被提交給這個Executors}}

3、Callable介面

Runnable是執行工作的獨立任務,但它沒有返回值。而Callable可以從任務中產生返回值。

下面代碼展示它的用法:

import java.util.concurrent.*;import java.util.*;class TaskWithResult implements Callable<String> {private int id;public TaskWithResult(int id) {this.id = id;}public String call() {return "result of TaskWithResult " + id;}}public class ThreadTest {public static void main(String[] args) {ExecutorService exec = Executors.newCachedThreadPool();ArrayList<Future<String>> results = new ArrayList<Future<String>>();for (int i = 0; i < 10; i++) {results.add(exec.submit(new TaskWithResult(i)));// Callable的調用方式必須是 ExecutorService.submit(),// 同時submit()會產生一個Future對象,可以通過isDone()查詢Future是否完成,通過get()擷取call()的傳回值.}for (Future<String> fs : results)try {System.out.print(fs.get() + ",");} catch (InterruptedException e) {System.out.println(e);return;} catch (ExecutionException e) {System.out.println(e);} finally {exec.shutdown();}}}/* * Output: result of TaskWithResult 0,result of TaskWithResult 1,result of * TaskWithResult 2,result of TaskWithResult 3,result of TaskWithResult 4,result * of TaskWithResult 5,result of TaskWithResult 6,result of TaskWithResult * 7,result of TaskWithResult 8,result of TaskWithResult 9, */// :~


4、線程相關方法和知識

a. yield(),表示當前線程已經完成或暫時不需要CPU,其它線程可以佔用之,這隻是個建議。

b. 優先順序,通過getPriority()和setPriority()方法擷取和重設線程的優先順序,優先順序和多數作業系統都不能映射的很好,唯一可移植的是設定優先權時,只使用 MAX_PRIORITY、NORM_PRIORITY和MIN_PRIORITY三個層級。

c. deamon(後台線程),它是程式啟動並執行時候在後台提供的一種泛型服務的線程,當所有非後台線程結束時,程式會殺死所有後台線程。

設定成後台線程的方法是:必須線上程啟動前調用 setDaemon(true),後台線程派生出來的子線程都是後台線程。

d. join(),如果某個線程在另一個線程t上調用t.join(),此線程會被掛起,直到目標線程t結束才可恢複(即t.isAlive()返回為false)。





+待添加





誰有好的JAVA學習筆記

建議先學習javaSE把基礎學通了.然後學習Servlet ,mvc 。 然後學習jsp. 在來學習Struts,Hibernate,Spring,ibatis架構. 還需瞭解最基本的sql 命令. 至於javaME我覺得沒這個必要要學,如果你要做手機開發的話。那你就學校ME,
1、首先要學習java的基礎知識。
不要被新技術迷惑,所謂萬變不離其宗,新技術都是基於java的基礎之上,如果基礎不紮實,對於這些新技術的理解也是一知半解,學不到根源
2、做一個java項目
在學習完java的基礎知識之後,做一個java項目來鞏固一下,在項目中你會發現很多問題,通過解決問題,加深基礎知識的掌握。
3、學習資料庫的基礎知識和開發應用
軟體開發離不了資料庫,掌握幾種流行的資料庫:Oracle、SQL server、MySQL等。
4、JEE基礎
在這裡首先要學習網站基礎,包括HTML、DHTML、JavaScript;接著要學習XML,XML+JAXP;然後學習JEE基礎,包括JEE開發環境,RMI/IIOP、JNDI;最後學習JDBC資料庫的應用開發。

5、web開發
全面的JEE的web開發知識:Servlet+JSP+javaBean+TagLib,到這裡做一個完整的web應用項目開發
 
JAVA學習筆記

我看過這個書
手頭就拿著看呢
是林信良寫的
寫的還是不錯的
我建議你還是把這本書跟
將java基礎的數結合起來看效果會更好
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.