我正在練習(xí) Semaphore 類,我創(chuàng)建了兩個(gè)線程,其中一個(gè)將數(shù)據(jù)寫入“Shared”類中的 HashMap,另一個(gè)從 Shared 類中讀取數(shù)據(jù)。所以我的問題是,當(dāng)我讀取數(shù)據(jù)時(shí),它只顯示先前在 Shared 類中添加的數(shù)據(jù),而不是更新的數(shù)據(jù)。我不知道我在哪里失蹤了。如果是這樣,我應(yīng)該從“Writedata”類中讀取線程如何返回 HashMap。class Shared { Map<String, Integer> hashmap = new HashMap<String, Integer>(); { hashmap.put("kishore", 797979); hashmap.put("nanesg", 8768787); hashmap.put("mahesh", 842803); hashmap.put("srikar", 7979980); } public void puthash(String name, Integer in, Map<String, Integer> hashmap) { hashmap.put(name, in); } public Map<String, Integer> gethash() { return this.hashmap; }}這是它的輸出:writeThread trying to get permitread Thread trying to acquiring the permit write Thread acquires the permit write Thread relesing the permit read Thread acquires the permit reading data in hashmap, Here is the data: nanesg : 8768787mahesh : 842803kishore : 797979srikar : 7979980read Thread relesing the permit 為什么 WriteData 線程(特別是函數(shù))沒有添加數(shù)據(jù)。該功能有什么問題?
添加回答
舉報(bào)
0/150
提交
取消