看板 java 關於我們 聯絡資訊
想說老師還沒教,就自己先看看,不過遇到了一些問題 以下為課本範例程式碼: //此程式是用來計算1+....+N的總合 /* 程式範例: Ch10_2_1.java */ class UserClass { // 宣告使用者類別 private int length; // 建構子 public UserClass(int length) { this.length = length; } // 計算總和 public int sum() { int temp = 0; for ( int i = 1; i <= length; i++ ) temp += i; return temp; } } // 執行緒類別, 實作Runnable介面 class UserThread extends UserClass implements Runnable { // 建構子 public UserThread(int length) { super(length); } // 執行執行緒 public void run() { System.out.println(Thread.currentThread() + "總和 = " + sum()); //這裏我就有問題了Thread.currentThread(),我看書上寫 類別方法,可以取得目前的執行緒物件s 老實說我不太懂他的意思 } } // 主類別 public class Ch10_2_1 { // 主程式 public static void main(String[] args) { System.out.println("執行緒: " + Thread.currentThread()); // 建立執行緒物件 UserThread ut1 = new UserThread(5); Thread t1 = new Thread(ut1, "執行緒A"); UserThread ut2 = new UserThread(4); Thread t2 = new Thread(ut2, "執行緒B"); // 啟動執行緒 t1.start(); t2.start(); } } 結果印出: 執行緒:Thread[main,5,main] Thread[執行緒 A,5,main]總和=15 Thread[執行緒 B,5,main]總和=10 //[執行緒 A,5,main] 這裏面第1個 執行緒 A (是名稱),那第2個"5"是指甚麼?! 而第3個 main是代表在主程式下執行?! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.254.242.179
LaPass:要找Thread.toString()的javadoc 這是優先級跟執行緒組 11/22 17:44
PsMonkey:請參考 Thread.toString() 的 source code 11/22 17:45
LaPass:叫他查source code太狠了吧..... XD 11/22 17:55
結果我打 System.out.println("t1="+t1.toString()); System.out.println("t2="+t2.toString()); 竟然結果和 System.out.println(Thread.currentThread()); 2種寫法幾乎完全一樣?! 所以[執行緒 A,5,main]分別指 執行緒名稱 優先權 群組名稱字串?! 是這樣對吧?! ※ 編輯: TWTRubiks 來自: 111.254.242.179 (11/22 18:07)
PsMonkey:就跟你說查 source code 了... 也未必會有最後一個 11/23 09:27
TWTRubiks:是直接google嗎?還是說要去哪查? 11/23 12:21