简体   繁体   English

同时运行两个Java文件

[英]Run two java files concurrently

I have two java applications. 我有两个Java应用程序。 They are very simple: they insert 500,000 rows of fake data into a MongoDB database and SQL database respectively. 它们非常简单:它们分别向MongoDB数据库和SQL数据库中插入500,000行伪数据。 I time each operation. 我为每个操作计时。

How can I launch these two java files at the exact same time? 我如何才能完全同时启动这两个Java文件?

How exact does exact mean? 精确是什么意思? Running them at exactly the same time is going to be damn near impossible, and fully impossible on a single-core machine (not that those still exist...). 完全相同的时间运行它们几乎是不可能的,并且在单核计算机上完全不可能(不是那些仍然存在...)。

But the easiest thing, if you want them launched very quickly, is: 但是,如果您希望它们很快启动,最简单的方法是:

java -jar first.jar & java -jar second.jar

If there's some startup time that you want to synchronize after, you could rig them both such that they busy-wait for a signal from an external event (such as a file appearing). 如果要在某个启动时间之后进行同步,则可以同时绑定它们,以便它们忙于等待来自外部事件(例如文件出现)的信号。 Then start them both up, wait for them to warm up, and trigger the signal. 然后将它们都启动,等待它们预热并触发信号。

There is no need to start them at the same time (and you can't be 100% exact). 无需同时启动它们(而且不可能100%精确)。 You just have to measure how much time each one executes. 您只需要测量每个人执行多少时间。

If this doesn't suit you, perhaps you can setup 2 crons to start the two applications. 如果这不适合您,也许您可​​以设置2克朗来启动两个应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM