是否有可能创建多个机器人并让它们在多个“桌面”中运行。我使用mac,可以创建多个桌面(也称为空格),并在每个桌面上运行多个窗口。是否有可能一次使用多个java命令行工具使用机器人类,每个工具运行在不同的桌面上。如果是的话,我该怎么做呢?
发布于 2019-11-14 19:26:01
为了做到这一点,您可能需要在java (也称为多线程)中使用线程。下面是帮助您理解的示例程序。线程将不完全同时运行,而是同时运行。为了使它们同时运行,需要线程的同步,这将使代码更加复杂。
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
// Thread is what we are using
class Anything1 extends Thread {
boolean activate = true;
public void run() {
// add here your robot class with actions
while (activate) {
try {
Robot robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello1");
}
}}
class Anything2 extends Thread {
boolean activate = true;
public void run() {
// add here your robot class with actions
while (activate) {
try {
Robot robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// for testing
System.out.println("Hello2");
}
}
}
public class App {
public static void main(String[] args) {
// activates the process
Anything1 p = new Anything1();
Anything2 r = new Anything2();
p.start();
r.start();
}
}
https://stackoverflow.com/questions/37651316
复制相似问题