簡單的java執行緒示例 排水

2021-09-12 18:36:19 字數 905 閱讀 1687

這個程式是作為當時學習多執行緒的範例,水塘進排水,程式很簡單

執行緒a和執行緒b 分別為排水和進水,包含執行緒初始化方法

先進行排水工作,進行一次工作觀察目前剩水量mqsl是否足夠排水,水排空

時,掛起排水線,進行進水工作

synchronized 關鍵字 :

代表這個方法加鎖,相當於不管哪乙個執行緒(例如執行緒a),執行到這個方法時,都要檢查有沒有其它執行緒b(或者c、 d等)正在用這個方法(或者該類的其他同步方法),有的話要等正在使用synchronized方法的執行緒b(或者c 、d)執行完這個方法後再執行此執行緒a,沒有的話,鎖定呼叫者,然後直接執行。它包括兩種用法:synchronized 方法和 synchronized 塊。

package thread;

public class water

}class threada extends thread

catch

(interruptedexception e)

}else}}

public boolean isempty()

public void

run(

)catch

(interruptedexception e)}}

}class threadb extends thread

} public void

run(

)catch

(interruptedexception e)}}

}/

結果圖

多執行緒簡單示例。。。

插入兩個list box,分別為 idc list1 和 idc list2 再插入乙個按鈕,為 idc button1 caption為 run 效果圖如下 雙擊 run 按鈕,新建 onbtnrun 函式。如下 void cmultthreadsdlg onbtnrun 在cmultthread...

多執行緒簡單示例

原文 多執行緒簡單示例 using system using system.collections.generic using system.linq using system.text using system.threading using system.runtime.remoting.mes...

Java簡單爬蟲示例

步驟簡介 獲取鏈結文字內容 urlconnection例項呼叫getinputstream 獲取輸入流,並返回輸入流 inputstream 物件,初始化給inputstreamreader,將位元組流轉換為字元流,並指定字符集編碼utf 8。bufferedreader從字元流中逐行讀取文字存入s...