如何在java中调用方法作为后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18088603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make calling a Method as a background process in java
提问by
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 .
请分享您的观点。
采纳答案by William Morrison
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.
如果您需要对 ExecutorService 进行更高程度的控制,请使用ThreadPoolExecutor。至于配置此服务,这将取决于您的用例。你多久调用一次这个方法?如果非常频繁,您可能希望至少始终在池中保留一个线程。我最多不会保留超过 4 或 8 个。
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.
由于您sendSymbol
每半秒只调用一次,因此一个线程应该足够了,sendSymbols
这不是一个非常耗时的例程。我会用 1 个线程配置一个固定的线程池。你甚至可以重用这个线程池来提交其他异步任务。
As long as you don't submit too many, it would be responsive when you call sendSymbol
.
只要你不提交太多,当你调用sendSymbol
.
回答by Miguel Prz
Sure, just use Java Threads, and join it to get the results (or other proper sync method, depends on your requirements)
当然,只需使用Java Threads,并加入它以获得结果(或其他适当的同步方法,取决于您的要求)
回答by Ankur Shanbhag
You need to spawn a separate thread to perform this activity concurrently. Although this will not be a separate process, but you can keep performing other task while you complete sending symbols.
您需要生成一个单独的线程来同时执行此活动。虽然这不会是一个单独的过程,但是您可以在完成发送符号的同时继续执行其他任务。
回答by Hna
The following is an example of how to use threads. You simply subclass Runnable which contains your data and the code you want to run in the thread. Then you create a thread with that runnable object as the parameter. Calling start on the thread will run the Runnable object's run method.
以下是如何使用线程的示例。您只需将 Runnable 子类化,其中包含您的数据和您想要在线程中运行的代码。然后创建一个以该可运行对象为参数的线程。在线程上调用 start 将运行 Runnable 对象的 run 方法。
public class MyRunnable implements Runnable {
private String commaDelimitedSymbols;
public MyRunnable(StringcommaDelimitedSymbols) {
this.commaDelimitedSymbols = commaDelimitedSymbols;
}
public void run() {
// Your code
}
}
public class Program {
public static void main(String args[]) {
MyRunnable myRunnable = new MyRunnable("...");
Thread t = new Thread(myRunnable)
t.start();
}
}
回答by maxammann
There is no really simple solution. Basically you need another thread which runs the method, but you also have to care about synchronization and thread-safety.
没有真正简单的解决方案。基本上,您需要另一个运行该方法的线程,但您还必须关心同步和线程安全。
new Thread(new Runnable() {
public void run() {
sendSymbol(String commaDelimitedSymbols);
}
}).start();
Maybe a better way would be to use Executors
也许更好的方法是使用Executors
But you will need to case about thread-safety. This is not really a simple task.
但是你需要考虑线程安全。这真的不是一项简单的任务。
回答by cdhanna
It sure is possible. Threading is the way to go here. In Java, you can launch a new thread like this
这肯定是可能的。线程是这里的方法。在 Java 中,您可以像这样启动一个新线程
Runnable backGroundRunnable = new Runnable() {
public void run(){
//Do something. Like call your function.
}};
Thread sampleThread = new Thread(backGroundRunnable);
sampleThread.start();
When you call start(), it launches a new thread. That thread will start running the run() function. When run() is complete, the thread terminates. Be careful, if you are calling from a swing app, then you need to use SwingUtil instead. Google that up, sir.
当您调用 start() 时,它会启动一个新线程。该线程将开始运行 run() 函数。当 run() 完成时,线程终止。请注意,如果您从 Swing 应用程序调用,则需要改用 SwingUtil。谷歌一下,先生。
Hope that works.
希望有效。