Java 如何同时运行两种方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9664036/
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 run two methods simultaneously
提问by RazorMx
I have this piece of code:
我有这段代码:
public static void main(String[] args) {
Downoader down = new Downoader();
Downoader down2 = new Downoader();
down.downloadFromConstructedUrl("http:xxxxx", new File("./references/word.txt"), new File("./references/words.txt"));
down2.downloadFromConstructedUrl("http:xxxx", new File("./references/word1.txt"), new File("./references/words1.txt"));
System.exit(0);
}
Is it possible to run these two methods: down.downloadFromConstructedUrl()
and down2.downloadFromConstructedUrl()
simultaneously? If so, how?
是否有可能运行这两种方法:down.downloadFromConstructedUrl()
和down2.downloadFromConstructedUrl()
同时?如果是这样,如何?
回答by Aurelien Ribon
That's what threads are for. A thread is a lightweight inner process used to run code in parallel to your "main thread".
这就是线程的用途。线程是一个轻量级的内部进程,用于与“主线程”并行运行代码。
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
回答by Amber
That would be what concurrency and threadingis for.
这就是并发和线程的用途。
回答by amit
You can use Thread, and run both methods on parallel using multithreading. You will have to override run()
and invoke Thread.start()
您可以使用Thread,并使用多线程并行运行这两种方法。您将不得不覆盖run()
并调用Thread.start()
Note that you will have to take care for synchronizing your methods.
请注意,您必须注意同步您的方法。
Also note: You will get "real parallel run" only if your machine has 2+ cores, however, if it doesn't - the OS will simulate "parallel" run for you.
另请注意:只有当您的机器有 2 个以上内核时,您才会获得“真正的并行运行”,但是,如果没有 - 操作系统将为您模拟“并行”运行。
回答by Luchian Grigore
You start two different threads, but they'll run truly parallel if you have at least a 2-core machine. Google Java multithreading.
您启动两个不同的线程,但如果您至少有一台 2 核机器,它们将真正并行运行。谷歌 Java 多线程。
回答by moodywoody
Yes, it is absolutely possible. Multi-threaded or concurrent programming is a very wide and complicated topic, though.
是的,这是绝对可能的。不过,多线程或并发编程是一个非常广泛和复杂的话题。
Best get started here
最好从这里开始
http://docs.oracle.com/javase/tutorial/essential/concurrency/
http://docs.oracle.com/javase/tutorial/essential/concurrency/
回答by aioobe
You start two threads:
你开始两个线程:
Try this:
尝试这个:
// Create two threads:
Thread thread1 = new Thread() {
public void run() {
new Downloader().downloadFromConstructedUrl("http:xxxxx",
new File("./references/word.txt"),
new File("./references/words.txt"));
}
};
Thread thread2 = new Thread() {
public void run() {
new Downloader().downloadFromConstructedUrl("http:xxxxx",
new File("./references/word1.txt"),
new File("./references/words1.txt"));
}
};
// Start the downloads.
thread1.start();
thread2.start();
// Wait for them both to finish
thread1.join();
thread2.join();
// Continue the execution...
(You may need to add a few try/catch blocks, but the above code should give you a good start.)
(您可能需要添加一些 try/catch 块,但上面的代码应该会给您一个良好的开端。)
Further reading:
进一步阅读:
回答by Peter ?tibrany
Instead of using threads directly, you better use ExecutorService, and run all download tasks through this service. Something like:
与其直接使用线程,不如使用ExecutorService,通过该服务运行所有下载任务。就像是:
ExecutorService service = Executors.newCachedThreadPool();
Downloader down = new Downloader("http:xxxxx", new File("./references/word.txt"), new File("./references/words.txt"));
Downloader down2 = new Downloader("http:xxxx", new File("./references/word1.txt"), new File("./references/words1.txt"));
service.invokeAll(Arrays.asList(down, down2));
Your Downloader
class must implement Callable
interface.
你的Downloader
类必须实现Callable
接口。