简体   繁体   English

在其他应用程序运行时,如何防止Java程序滞后?

[英]How can I prevent my Java program from lagging while other applications are running?

I wrote a simple code in Java that uses the Robot class to move the mouse according to some conditions. 我用Java写了一个简单的代码,该代码使用Robot类根据某些条件移动鼠标。

Although the code works nicely, there seems to be a 'lag' when other applications are running. 尽管代码运行良好,但是在其他应用程序运行时似乎存在“滞后”。

I think Java has some issues posting system messages. 我认为Java在发布系统消息方面存在一些问题。

Is there a workaround to avoid this? 有解决方法可以避免这种情况吗?

Before you start thinking about reducing the lag, you must first understand it's causes. 在开始考虑减少延迟之前,您必须首先了解其原因。 I'll present the answer(s) in a fashion in which you can understand the "why" along with the "what to do". 我将以一种您可以理解“为什么”以及“该做什么”的方式来显示答案。

By your description that the lag only occurs when other programs are running along with your robot, the most likely causes for the lag are: 根据您的描述,仅当其他程序与您的机器人一起运行时才会发生延迟,导致延迟的最可能原因是:


Lack of system resources - Too many things running at the same time, consuming too much memory/processing-power, thus making the OS slow-down some programs in order to be able to run the others. 缺乏系统资源 -太多事情同时运行,消耗太多的内存/处理能力,从而使OS降低某些程序的速度,以便能够运行其他程序。

What to do: To try to fix such issues, you can try to optimize your code, to make it use less memory/processing power, thus reducing the cause of the lag, with implicitly reduces the lag itself. 要做的事情:要尝试解决此类问题,您可以尝试优化代码,使其使用更少的内存/处理能力,从而减少延迟的原因,并隐式地减少延迟本身。 Unfortunetly tough, it's hard to legally do the same for any 3rd party programs, so the lag can hardly be completely removed if the concurrent applications are not yours. 不幸的是,很难对任何第三方程序进行合法的操作,因此,如果并发应用程序不是您的,则很难完全消除这一滞后。


Concurrency regarding a non-replicable, non-shareable component - One or more components that cannot be accessed by more than one process at a time and that cannot be cloned into multiple instances needs to be used by more than one process that is running. 关于不可复制,不可共享组件的并发性 -一个或多个组件一次不能被一个以上的进程访问,并且不能被克隆到多个实例中,需要一个以上正在运行的进程使用。 While one process takes control of it, any other processes have no choice but wait for the component to be freed. 尽管一个进程控制着它,但其他进程别无选择,只能等待组件被释放。

What to do: In this case, there hardly is any legal method other than to reduce the concurrent process's priority while increasing your's (effectively slowing them down in order for your program to run faster), or shut them down completely. 怎么办:在这种情况下,除了降低并发进程的优先级而又增加优先级(有效地降低它们的运行速度以使程序运行更快)或完全关闭它们之外,几乎没有其他合法方法。

How to do: To increase your process's priority, this is the code to set it at 80% (default is usually 50%), inset at your main() : 怎么做:为了增加进程的优先级,这是将其设置为main() 80%(默认通常为50%)的代码:

Thread.currentThread().setPriority((int)(Thread.MAX_PRIORITY*0.8));

Note: You can set your process to "never" let go of whatever components it needs, using Thread.MAX_PRIORITY without multiplying by 0.8 , but that is unrecommended , as it will pretty much pause any process that requires the components (quasi-same to shutting them down while yours is running), and if your program hangs, for whatever reason, so will those, as the components are never released. 注意:您可以使用Thread.MAX_PRIORITY将进程设置为“永不”释放所需的任何组件,而不用乘以0.8 ,但这是不推荐的 ,因为它将几乎暂停任何需要组件的进程(与(在您的程序运行时将其关闭),并且如果您的程序因任何原因挂起,由于这些组件永远不会释放,它们也会挂起。

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

相关问题 如何从Linux上运行的Java访问首选应用程序? - How can I access preferred applications from Java running on Linux? 我怎样才能阻止草草滞后于我的游戏? - How can I stop grass from lagging my game? 如何制作在运行时更新的“模块化” Java程序? - How can I make a “modular” Java program that updates while running? 如何防止ArrayList在Java GUI中重置? - How can I prevent my ArrayList from resetting in a Java GUI? 如何通过Java程序创建Lync会议? - How can I create a Lync meeting from my Java program? 如何从我的 Java 程序执行 Perl 脚本? - How can I execute a Perl script from my Java program? 我如何防止 Selenium RC 在我的测试运行时窃取 window 焦点? - How do I prevent Selenium RC from stealing window focus while my tests are running? 我可以使用Java操作其他应用程序吗? - Can i manipulate other applications using Java? 当我的Java应用程序在Ubuntu中运行时,如何防止Alt键打开Dash Home? - How can I prevent Alt key opening Dash Home while my Java app runs in Ubuntu? Java | 如何让其中一个线程休眠而其他线程仍在运行? - Java | How can I make one of the threads sleep while the other ones are still running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM