【发布时间】:2018-07-03 10:53:05
【问题描述】:
我有一个包含很长信息列表的大 json 文件,我需要在许多子线程中只读该列表。
在 java 中,我们只能通过值而不是通过引用来传递变量,我希望我的程序尽可能减少内存/磁盘使用量。
现在我将完整列表或仅将其子列表传递给我创建的每个线程。
有没有办法从所有线程访问相同的 List 变量,而无需将完整的 List 复制到每个线程中?
我需要“仅阅读”列表
这是我的程序的工作原理
1 - 服务(等待文件创建)
2 - 将创建的 Json 文件内容读入 MyList
3 - 在具有不同限制/偏移的部分 MyList 上启动线程
我正在尝试做的是这样的事情
List<Map<String,String>> MyList = JsonToObject(filePath);
executor = Executors.newFixedThreadPool( 10 );
在午餐类
List<Map<String,String>> MyList = JsonToObject(filePath);
executor = Executors.newFixedThreadPool( 10 );
int limit = 10;
int offset= 0;
for ( int i = 0 ; i < MyList.size() && offset < MyList.size() ; i++ ) {
offset = i * 10 ;
Child thread = new Child( limit , offset );
executor.submit( thread );
}
在子类
public void run(){
for ( int i = this.offset ; i < this.limit ; i++ ) {
this.doSomthingWith ( Luncher.Mylist.get( i ) );
}
}
【问题讨论】:
标签: java multithreading global-variables visibility