如何在同一进程中创建相同 java class 运行 的两个实例?

How to create two instances of same java class running in same process?

我正在学习 Java 流程,我正在尝试在同一流程中创建相同 java class 运行 的两个实例,这是一项要求。

class Chat {
    public void getMessage() { * * * some implementation
    }
}

class ProcessMain {
     public static void main(String args[]) {
         Chat c1 = new Chat();
         Chat c2 = new Chat();
         ProcessBuilder pb = new ProcessBuilder(c1); * * * * here is where I am stuck.
         Two instances of same class should run in same process
     }
 }

谁能给我一个线索?

默认情况下 Java 将 运行 主进程 class 在同一个进程中 thread. If you want to have two classes communicate with one another you can pass c1 into a method inside of c2 并以这种方式更改 c1。