首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么这个线程(WriteData)不能执行它的功能?

为什么这个线程(WriteData)不能执行它的功能?
EN

Stack Overflow用户
提问于 2018-08-17 23:26:15
回答 1查看 37关注 0票数 1

我正在练习信号量类,我已经创建了两个线程,一个在“共享”类中将数据写入HashMap,另一个从共享类中读取数据。

所以我的问题是,当我读取数据时,它只显示之前在共享类中添加的数据,而不是更新的数据。我不知道我迷失在哪里了。

我是否应该从"Writedata“类中读取,如果是,线程如何返回HashMap。

代码语言:javascript
运行
复制
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");
    }
}

下面是它的输出:

代码语言:javascript
运行
复制
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线程(特别是函数)没有添加数据。这个函数有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-17 23:36:01

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

解决方案

使用相同的Shared

代码语言:javascript
运行
复制
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);
    }
}

结果

代码语言:javascript
运行
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51898576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档