有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在其他线程可以访问静态映射时从数据库更新静态映射

class A {
static Map<String,ColA> mapA;
static const int a = 12;
static Map<String,colB> mapB;
}

Q1>; 现在我需要每10分钟从数据库更新一次地图。可以有多个线程访问mapA。因此,如果我在其中一个线程中使用synchronized进行更新,那么在更新过程中,如何出现线程可能中断的任何实例。如果是这样,如何解决这个问题

Q2>; 我也有不同的班级成员。其中有些是常量,有些是从内存加载的。然而,它们都有一个共同的属性。对于所有实例,它们总是相同的(静态)。那么把所有的东西都放在一个班上有意义吗?还是我们应该有两个单独的班

谢谢


共 (2) 个答案

  1. # 1 楼答案

    1. If synchronization is used on the group of statement or the method which is responsible 为了改变MapA的关键状态,我认为不会有问题

    2. I am not getting a clear understanding of your 2nd question, but still you can divide{}

    内聚类是指具有反映其名称的方法的类

    例如:

     `class Animal with methods like Sound, Movements, etc...`
    
  2. # 2 楼答案

    Now I need to update the Map from database in every 10 minutes. There can be multiple thread which can be accessing mapA. So if i update in one of the thread using synchonized how will there be any instance where the thread may break when the updation is going on. If so how to solve this problem??

    这听起来像是值得使用的地方。从数据库中获取数据,将其放入映射中,然后以原子方式将其与“旧”映射交换。这样,任何客户端代码都可以获取映射,并知道它实际上是一个快照,在它们下不会更改

    Also I have different member of the class. Some of them are constants and some of them are loaded from memrory. However all of them have one common property. They are always same for all the instances(static). So does it make sense to have everything in one class or should we have two seperate class.

    这是一个相当模糊的描述。您指定的属性(一些是常量,一些是从内存加载的,所有属性都有一个公共属性,所有属性都是静态的)都与它们是否应该在同一个类中无关