繁体   English   中英

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

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

我有一个Java程序,需要使用导入的源包中的函数,该函数将2GB数据加载到内存中并使用它来进行计算。

但是,每次我调用该函数时,都会处理加载过程,这非常耗时。

我的程序的结构如下:

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)

我知道这是一个太大的问题,但是:

  1. 是否有一般的想法,我只能运行一次加载,然后再使用它进行所有计算?

  2. 如果我在顶级服务器(在套接字服务器中)中导入了软件包,是否可以以某种方式帮助我加快加载过程?

  3. 企业最常用的方法是什么?

由于这个问题很长时间没有得到任何答案,所以这里是一个小总结:

事实证明,在我使用的当前方法中,如果我在最高级别(套接字服务器)中加载库,我们可以确保对于其中的每个线程,我们都不需要再次加载该库。

在企业级别,有更好的Java多线程功能,例如:

newCachedThreadPool()

的newFixedThreadPool()

newSingleThreadExecutor()

的newScheduledThreadPool()

例如,我们可以像这样使用它们

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

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

singleThreadPool.execute(new MyRunnable);

singleThreadPool.shutdown();

在实际实践中可能会很有用。

暂无
暂无

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

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