gpt4 book ai didi

java - 为什么这个线程(WriteData)没有执行它的功能?

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:24 26 4
gpt4 key购买 nike

我正在练习信号量类(class)我创建了两个线程,其中一个将数据写入“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;
}

}

class ReadHashMap implements Runnable {

Semaphore sem;
String name;
Shared shared = new Shared();

ReadHashMap(Semaphore sem, String name) {
this.sem = sem;
this.name = name;

new Thread(this, name).start();

}

public void run() {
try {
System.out.println(Thread.currentThread().getName() + " Thread trying to acquiring the permit ");
sem.acquire();
System.out.println(Thread.currentThread().getName() + " Thread acquires the permit ");
Map<String, Integer> hashmap = shared.gethash();
Set<Entry<String, Integer>> set = hashmap.entrySet();
System.out.println("reading data in hashmap, Here is the data: ");
for (Map.Entry<String, Integer> mp : set) {

System.out.println(mp.getKey() + " : " + mp.getValue());
}

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + " Thread relesing the permit ");
sem.release();
}

}
}

class WriteData implements Runnable {

Semaphore sem;
String name;
Shared shared = new Shared();

WriteData(Semaphore sem, String name) {
this.sem = sem;
this.name = name;
new Thread(this, name).start();

}

public void run() {
try {
System.out.println(Thread.currentThread().getName() + "Thread trying to get permit");
sem.acquire();
Map<String, Integer> hashmap = shared.gethash();
System.out.println(Thread.currentThread().getName() + " Thread acquires the permit ");
shared.puthash("added1", 98799, hashmap);
shared.puthash("added2", 987989, hashmap);
shared.puthash("added3", 98979, hashmap);

} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " Thread relesing the permit ");
sem.release();
}
}

public class SemaphoreDemo {
public static void main(String args[]) {
Semaphore sem = new Semaphore(1);

new WriteData(sem, "write");
new ReadHashMap(sem, "read");
}
}

这是它的输出:

writeThread trying to get permit
read 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 : 8768787
mahesh : 842803
kishore : 797979
srikar : 7979980
read Thread relesing the permit

为什么 WriteData 线程(特别是函数)没有添加数据。该函数出了什么问题?

最佳答案

因为您在 ReadHashMapWriteData 中创建了不同的 Shared 实例。

解决方案

使用相同的共享:

class ReadHashMap implements Runnable {

Semaphore sem;
String name;
Shared shared;

ReadHashMap(Semaphore sem, String name, Shared shared) {
this.sem = sem;
this.name = name;
this.shared = shared;
new Thread(this, name).start();
}
....
}

class WriteData implements Runnable {

Semaphore sem;
String name;
Shared shared;

WriteData(Semaphore sem, String name, Shared shared) {
this.sem = sem;
this.name = name;
this.shared = shared;
new Thread(this, name).start();
}

....
}

public class SemaphoreDemo {
public static void main(String args[]) {
Semaphore sem = new Semaphore(1);
Shared shared = new Shared();

new WriteData(sem, "write", shared);
new ReadHashMap(sem, "read", shared);
}
}

结果

writeThread trying to get permit
write Thread acquires the permit
read Thread trying to acquiring the permit
write Thread relesing the permit
read Thread acquires the permit
reading data in hashmap, Here is the data:
nanesg : 8768787
mahesh : 842803
kishore : 797979
added2 : 987989
added3 : 98979
srikar : 7979980
added1 : 98799
read Thread relesing the permit

关于java - 为什么这个线程(WriteData)没有执行它的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51898576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com