java保存数据线程会占用cou,如何使用Java在数据库中保存线程实例

This is SAP PI requirement,

Source System: XY_Client

Middleware: PI System

Target system : SAP

The XML files are received to the PI system, for each XML file an internal file is generated to keep track of store_number, and count of xml files.

How it works: suppose if XML_FILE_1 reaches PI, internal file called sequence_gen is created. the file contains the store number present in XML file and count will be initialized to 1.

so first time,

sequence_gen file contains Store: 1001 Count:1

(after some time interval)If XML_FILE_2 reaches PI, second time,

sequence_gen file contains Store: 1001 Count:2

and so on..

My question is : If 'n' number of files come at the same time to PI system, the 1st file will lock the sequence_gen file. so how will the 2nd file update the value to the sequence_gen file? So how to tackle this problem?

I thought of creating a thread instance for every call and storing it in a database. and retrieving each instance, performing function, returning result to the xml call and deleting that instance.. Is it possible? How to go forward on this?

解决方案

Rather than keep track of all of the threads that are locking and unlocking the file, you could have a single thread that is in charge of changing it. Have each thread place a request to change the file into a Concurrent Queue, which then notifies the Sequence_Gen thread to write to its own file. In essence:

Sequence_Gen thread:

@Override

public synchronized void Run(){

while(true){ //Some condition

while(queue.isEmpty()) {

this.wait();

}

Object obj = queue.pop();

//Open file

file.write(obj);

//Close file

}

}

Then, in any other thread, just queue and notify that there is something to write.

public synchronized void AddItem(Object item) {

queue.put(item);

this.notifyAll();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值