简体   繁体   English

Java-将数据加载到内存一次,然后多次使用

[英]Java - Load data to memory once, and use it multiple times

I have a Java program, which needs to use a function from a imported source package that loads 2GB data into the memory and use it to do the calculation. 我有一个Java程序,需要使用导入的源包中的函数,该函数将2GB数据加载到内存中并使用它来进行计算。

However every time when I call the function, the loading process is processed, which is very time consuming. 但是,每次我调用该函数时,都会处理加载过程,这非常耗时。

My program's structure is like: 我的程序的结构如下:

Socket Server (listening on a port)

|_ Multi Server Thread (Work as a input stream reader)

...|_ Sample Protocol (deal with the input steam using the data related functions)

I know this is a question too wide, but: 我知道这是一个太大的问题,但是:

  1. Are there any general idea that I can run the loading only once and use it to do all the calculation afterwards? 是否有一般的想法,我只能运行一次加载,然后再使用它进行所有计算?

  2. IF I import the package in the Top Level (in socket server), will that help me accelerate the loading process somehow? 如果我在顶级服务器(在套接字服务器中)中导入了软件包,是否可以以某种方式帮助我加快加载过程?

  3. What is the more common method used by the enterprise? 企业最常用的方法是什么?

Since this question did not get any answer for a long time, here is a small summary: 由于这个问题很长时间没有得到任何答案,所以这里是一个小总结:

It turns out in the current method I used, if I load the lib in the highest level (Socket Server), we can make sure that for every thread in it we don't need to load the library again. 事实证明,在我使用的当前方法中,如果我在最高级别(套接字服务器)中加载库,我们可以确保对于其中的每个线程,我们都不需要再次加载该库。

In the enterprise level, there are better multi-thread functions for java, like: 在企业级别,有更好的Java多线程功能,例如:

newCachedThreadPool() newCachedThreadPool()

newFixedThreadPool() 的newFixedThreadPool()

newSingleThreadExecutor() newSingleThreadExecutor()

newScheduledThreadPool() 的newScheduledThreadPool()

For example we can use them like, 例如,我们可以像这样使用它们

ExecutorService fixedThreadPool() = Executors.newFixedThreadPool(4);

Future<T> future1 = singleThreadPool.submit(new MyRunnable());

singleThreadPool.execute(new MyRunnable);

singleThreadPool.shutdown();

which might be useful in real practice. 在实际实践中可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM