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

标签 java multithreading semaphore

我正在练习信号量类(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/

相关文章:

java - 如何在没有 EventListener 的情况下从 Firebase 检索数据?

java - 嵌入式H2数据库: Multithreaded access

java - Spring 上下文:property-placeholder for a boolean value

c++ - 在多线程 C++ 程序中使用 std::vector 时应用程序崩溃

c - shmctl 和 semctl 不删除 ipc

java - Docker 错误 : standard_init_linux. go:195: exec 用户进程导致 "exec format error"

c - 微基准测试显示进程切换比线程切换更快;怎么了?

c# - 如何使静态方法线程安全?

java - 如何设置Redisson分布式锁的最大许可数

multithreading - 使用信号量不会唤醒其他进程