为什么线程只运行一次?

时间:2012-07-20 11:40:47

标签: java multithreading

我正在尝试跟踪鼠标光标的位置,因此需要使用线程继续跟踪它。我的线程只运行一次。我猜我一路上都错过了一些东西。

代码:

mousePosition类

import java.awt.MouseInfo;
import java.awt.Point;
import java.util.Timer;

public class mousePosition implements Runnable
{
    static Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
    static Timer t = new Timer();
    int x,y = 0;


    public void run()
    {
        try
        {
                x = mouseLocation.x;
                y = mouseLocation.y;

                System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis());
        }

        catch(Exception e)
        {
            System.out.println("Exception caught : "+e);
        }

    }



}

主要班级

public class threadRunner
{
    public static void main(String[] args)
    {
        Thread t1 = new Thread(new mousePosition());

        t1.start();

    }

}

感谢您的帮助。我知道之前曾问过这个问题,但我仍在努力使其成功。

5 个答案:

答案 0 :(得分:6)

Thread的run()方法只运行一次。如果你做了类似的事情,它会运行得更多:

public void run() {
  while(true) { // Or with a stop condition
    try {
            x = mouseLocation.x;
            y = mouseLocation.y;

            System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis());

    } catch(Exception e) {
        System.out.println("Exception caught : "+e);
    }
  }
}

虽然我确信这在线程上的循环是昂贵的,但是以计算成本来说,并且使用Observer design pattern有更好的做法。这种良好实践的实现和示例正好是MouseListener

答案 1 :(得分:2)

当您start()Thread时,唯一发生的事情就是执行Thread s run()方法。 run()方法完成后,Thread就会死亡。

要不断检查鼠标位置,您应该开始某种MouseListener,而是有一个很好的教程here

要了解有关线程的更多信息,我建议您查看Java Tutorials on the subject

答案 2 :(得分:2)

这是每50ms读取鼠标位置的简单方法

class mousePosition {
    public static void main(String[] args) {
        Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                int x, y;
                try {
                    PointerInfo mouseLocation = MouseInfo.getPointerInfo();
                    x = mouseLocation.getLocation().x;
                    y = mouseLocation.getLocation().y;
                    System.out.println("X:" + x + " Y:" + y + " at time "
                            + System.currentTimeMillis());
                } catch (Exception e) {
                    System.out.println("Exception caught : " + e);
                }
            }
        }, 0, 50);
    }
}

答案 3 :(得分:0)

这样做:

public void run()
    {
        try
        {       
                boolean mouseTracked = false;
                while(!mouseTracked){
                   x = mouseLocation.x;
                   y = mouseLocation.y;

                System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis());
                mouseTracked = true; //do this when mouse tracked..
        }
}
        catch(Exception e)
        {
            System.out.println("Exception caught : "+e);
        }

    }

这会继续跟踪鼠标,直到你将mouseTracked设置为true,将mouseTracked设置为true,你将在匹配条件下执行

答案 4 :(得分:0)

像这样改变你的课程

import java.awt.MouseInfo;
import java.awt.Point;
import java.util.Timer;

public class MousePosition implements Runnable
{
static Point mouseLocation = null;
static Timer t = new Timer();
int x,y = 0;


public void run()
{   
  while(true){
    try
    {
          mouseLocation= MouseInfo.getPointerInfo().getLocation();

            x = mouseLocation.x;
            y = mouseLocation.y;

            System.out.println("X:"+x+" Y:"+y+" at time "+System.currentTimeMillis());
    }

    catch(Exception e)
    {
        System.out.println("Exception caught : "+e);
    }
  }
}
}