mxtah

          飛·天

           

          java threads(2e)學習和其他(更新中......)

          本來想把每天看書的心得寫到每天的blog中,不過后來想想這種散亂的東西還是寫到一起吧。

          1.有關(guān)join()方法:join()的作用等同于sleep()和isAlive()的結(jié)合使用。
          用join():把線程A連接(join)到當前線程,然后當前線程將保持sleep,一直到線程A not alive。
          用sleep()和isAlive():while(A.isAlive()){thread.sleep(time)};用這個方法比較消耗cpu,并且時間上比用join()浪費。因為當前線程要經(jīng)過time以后才會去檢查A的活動性,有可能那時候A早就stop了。
          ?
          引用java threads(2e):
          When the join() method is called, the current thread will simply wait until the thread it is joining with is no longer alive.
          ?
          2.別把join()和isAlive()用于當前線程。
          ?
          引用java threads(2e):
          The concept of a thread calling the isAlive() or the join() method on itself does not make sense.
          ?
          3.補充一個東西(和線程無關(guān)):
          看以下代碼:
          public class Test{?
          ?? public static void main(String[] args) {
          ??? StringChange sc = new StringChange();
          ??? String str = "hello";
          ??? sc.change(str);
          ??? System.out.println("outer:" + str);
          ??? }
          }
          class StringChange {
          ?public StringChange() {
          ?}
          ?
          ?public void change(String s) {
          ? s += "world";
          ? System.out.println("inner:" + s);
          ?}
          }

          類似的代碼在網(wǎng)上很多,這里有兩個問題一個是java中參數(shù)傳遞的問題;另一個是當參數(shù)為String的reference時的傳遞問題。
          首先一點,java中是不存在傳引用的說法的,都是傳值,只不過當參數(shù)為基本類型時,傳遞的是該參數(shù)本身,而當參數(shù)為對象的reference時,傳遞的是該reference的地址值,所以,如果傳基本類型,那么在執(zhí)行change之后,main中的基本類型的值沒有變化,但是如果傳的是reference的話,就會改變main中的對象(也就是改變對象的屬性值)。
          第二點,因為String對象是個特殊的對象,他一旦生成后,值就不可再變,因此當執(zhí)行類似于 s += "world";的代碼時,其實是生成了另外一個sTemp,而且打印的也是該sTemp,當change返回時,sTemp消失,而s本身沒有變化。
          敲得我都累啊。呵呵,這個問題很基本,網(wǎng)上也很多介紹,不過說法很多,其中的錯誤也不少,不知道我這里說清楚沒。哈,自己看懂就OK啦。

          言歸正傳,下面繼續(xù)thread的學習。

          4.有關(guān)activeCount()和enumerate(Thread[] tarray):
          activeCount()是返回當前線程的線程組中活動線程的數(shù)目,而enumerate(Thread[] tarray)則是把當前線程的線程組及其子組中的每一個活動線程復制到指定的數(shù)組中。
          可以結(jié)合這兩個方法,根據(jù)當前線程的線程組中的活動線程的數(shù)目,創(chuàng)建一個數(shù)組,并把當前線程的線程組的每一個活動線程復制到該數(shù)組。
          不過這里有個疑問--activeCount()返回的數(shù)據(jù)中,包括當前線程的線程組的子組嗎?待我再研究研究。。

          引用java threads(2e):
          A thread reference first appears in the thread array returned by the enumerate() method, and is counted by the activeCount() method, when the thread object is first constructed and not when the thread is started.
          The thread is removed from the thread array either when the thread is stopped or when the run() method has completed.

          5.有關(guān)thread的restart:
          根據(jù)java threads(2e)的描述,一個thread對象的不能夠restart。參考下面的代碼:
          import java.applet.Applet;
          public class Animate extends Applet {
          ???TimerThread t;
          ???public void start() {
          ??????if (t == null)
          ?????????t = new TimerThread(this, 500);
          ??????t.start();
          ???}
          ???public void stop() {
          ??????t.shouldRun = false;
          ??????try {
          ?????????t.join();
          ??????} catch (InterruptedException e) {}
          ??????// t = null;
          ???}
          }
          以上代碼不能實現(xiàn)t的restart。

          引用java threads(2e):
          when we try to restart the thread by calling the TimerThread's start() method, nothing happens. The start() method won'treturn an exception condition, but the run()?? method also won't be called. The isAlive() method also won't return true.

          ---第一天寫blog,也是剛開始學習threads---就當留個記號,同時鼓勵自己。

          5.先突出重點,把我要引用的話寫出來:
          引用java threads(2e):
          Since there is only one object lock for each instance of the class, the lock that freeBusyFlag() will try to grab is the same lock tryGetBusyFlag() will grab.

          這兩天看書一直有個疑問,在以下代碼中:
          public class BusyFlag {
          ???protected Thread busyflag = null;
          ???public void getBusyFlag() {
          ??????while (tryGetBusyFlag() == false) {
          ?????????try {
          ????????????Thread.sleep(100);
          ?????????} catch (Exception e) {}
          ??????}
          ???}
          ???public synchronized boolean tryGetBusyFlag() {
          ??????if (busyflag == null) {
          ?????????busyflag = Thread.currentThread();
          ?????????return true;
          ??????}
          ??????return false;
          ???}
          ???public synchronized void freeBusyFlag() {
          ??????if (busyflag == Thread.currentThread()) {
          ?????????busyflag = null;
          ??????}
          ???}
          }
          是不是應該把tryGetBusyFlag()和freeBusyFlag()放到同一個synchronized定義的范圍呢,呵呵,上面那句話把我一下點醒了。
          嗯,回味無窮,這是不是算是初學者在學習過程中的一種快樂呢?!
          ?????????----今天心情不錯

          6.兩段代碼:
          (1)public class BusyFlag {
          ???protected Thread busyflag = null;
          ???public void getBusyFlag() {
          ??????while (tryGetBusyFlag() == false) {
          ?????????try {
          ????????????Thread.sleep(100);
          ?????????} catch (Exception e) {}
          ??????}
          ???}
          ???public synchronized boolean tryGetBusyFlag() {
          ??????if (busyflag == null) {
          ?????????busyflag = Thread.currentThread();
          ?????????return true;
          ??????}
          ??????return false;
          ???}
          ???public synchronized void freeBusyFlag() {
          ??????if (busyflag == Thread.currentThread()) {
          ?????????busyflag = null;
          ??????}
          ???}
          }
          (2)public class BusyFlag {
          ???protected Thread busyflag = null;
          ???public synchronized void getBusyFlag() {
          ??????while (true) {
          ?????????if (busyflag == null) {
          ????????????busyflag = Thread.currentThread();
          ????????????break;
          ?????????}
          ?????????try {
          ????????????Thread.sleep(100);
          ?????????} catch (Exception e) {}
          ??????}
          ???}???
          ?? public synchronized void freeBusyFlag() {
          ??????if (busyflag == Thread.currentThread()) {
          ?????????busyflag = null;
          ??????}
          ???}
          }
          第二段代碼會導致死鎖,第一段不會。
          把第二段代碼的改成以下就可以了:
          public void getBusyFlag() {
          ??????while (true) {
          ?????????synchronized(this) {
          ????????????if (busyflag == null) {
          ???????????????busyflag = Thread.currentThread();
          ???????????????break;
          ????????????}
          ?????????}
          ?????????try {
          ????????????Thread.sleep(100);
          ?????????} catch (Exception e) {}
          ??????}
          }?

          posted on 2006-04-21 09:37 飛天 閱讀(153) 評論(0)  編輯  收藏


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


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 新闻| 湘阴县| 温州市| 洛阳市| 崇州市| 定安县| 武冈市| 云林县| 永德县| 昌江| 巴彦淖尔市| 宁津县| 黔江区| 翁牛特旗| 清流县| 芮城县| 东兰县| 宿迁市| 岳普湖县| 炎陵县| 保定市| 宣威市| 盐城市| 渝中区| 新野县| 喀什市| 凌海市| 安吉县| 确山县| 武冈市| 邵阳市| 榕江县| 普安县| 历史| 阿合奇县| 桐庐县| 洪洞县| 喀什市| 钟山县| 沾化县| 新巴尔虎右旗|