java如何实现进程,如何在Java中将方法作为后台进程进行调用

In my application , I have this logic when the user logins , it will call the below method , with all the symbols the user owns .

public void sendSymbol(String commaDelimitedSymbols) {

try {

// further logic

} catch (Throwable t) {

}

}

my question is that as this task of sending symbols can be completed slowly but must be completed , so is there anyway i can make this as a background task ??

Is this possible ??

please share your views .

解决方案

Something like this is what you're looking for.

ExecutorService service = Executors.newFixedThreadPool(4);

service.submit(new Runnable() {

public void run() {

sendSymbol();

}

});

Create an executor service. This will keep a pool of threads for reuse. Much more efficient than creating a new Thread each time for each asynchronous method call.

If you need a higher degree of control over your ExecutorService, use ThreadPoolExecutor. As far as configuring this service, it will depend on your use case. How often are you calling this method? If very often, you probably want to keep one thread in the pool at all times at least. I wouldn't keep more than 4 or 8 at maximum.

As you are only calling sendSymbol once every half second, one thread should be plenty enough given sendSymbols is not an extremely time consuming routine. I would configure a fixed thread pool with 1 thread. You could even reuse this thread pool to submit other asynchronous tasks.

As long as you don't submit too many, it would be responsive when you call sendSymbol.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>