为了账号安全,请及时绑定邮箱和手机立即绑定

为什么这个线程(WriteData)没有做它的功能?

为什么这个线程(WriteData)没有做它的功能?

慕村225694 2021-07-23 17:01:58
我正在练习 Semaphore 类,我创建了两个线程,其中一个将数据写入“Shared”类中的 HashMap,另一个从 Shared 类中读取数据。所以我的问题是,当我读取数据时,它只显示先前在 Shared 类中添加的数据,而不是更新的数据。我不知道我在哪里失踪了。如果是这样,我应该从“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 线程(特别是函数)没有添加数据。该功能有什么问题?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号