当另一个循环在同一个类中运行时,是否可以循环运行

use*_*544 2 java loops class

是否可以进行循环运行,例如检查是否发生了鼠标移动,同时程序正在运行另一个循环来执行其他操作.我知道我可以在彼此内部使用循环,但这对于我的程序识别鼠标的移动来说效率不够,因为在我的程序中需要大约30分钟才能发生任何更改.

以防万一我不明白我的问题......我问的是同一个班级是否可以同时发生两个循环而不会相互干扰.

Ell*_*sch 5

是.而且,这是一个例子 -

public static class Test implements Runnable {
  public Test(String name) {
    this.name = name;
  }

  private String name;

  public void run() {
    for (int i = 0; i < 5; i++) {
      System.out.println(name + ": " + i);
      System.out.flush();
    }
  }
}

public static void main(String[] args) {
  Thread a = new Thread(new Test("A"));
  Thread b = new Thread(new Test("B"));
  b.start();
  a.start();
  System.out.println("Mainly 1");
  try {
    b.join();
    a.join();
  } catch (InterruptedException e) {
    e.printStackTrace();
  } finally {
    System.out.println("Mainly 2");
  }
}
Run Code Online (Sandbox Code Playgroud)

这可能会导致我的测试结果略有变化

B: 0
Mainly 1
A: 0
B: 1
B: 2
A: 1
B: 3
B: 4
A: 2
A: 3
A: 4
Mainly 2