开发者

Threads: Just what is it that makes them confusing? Two Runnables with Mouse Listener

开发者 https://www.devze.com 2022-12-24 18:15 出处:网络
I have a JWindow and a JFrame both I have made runnable and both impleme开发者_Python百科nt a mouse listener.

I have a JWindow and a JFrame both I have made runnable and both impleme开发者_Python百科nt a mouse listener. I have alot of testing to do for a project of mine and to simplify it I wish to be able to automate most of it, so I have starting my own mouse recorder and replayer (uses Java Robot Class).

Kinda like a simplified AutoHotKey or AutoIt thing... but it'll run on my Ubuntu machine aswell as my Windows one!!!

The JWindow I have made is translucent and covers the entire screen, when you click on it it disappears and replays the click to the object behind and then reappears. This is the recording process. When the User right clicks the is set to not visible and the recorded actions are replayed.

During replay I want the option to be able to exit the entire application and so I though the best way to do this would be to make the JFrame and the JWindow Runnable.

The JFrame is simply their to offer a close option from the application.

So, in my Main class I have

public static void Main(String[] args){
    recorder = new Recorder();
    gui = new GUI();
    Thread tr = new Thread(recorder);
    Thread tg = new Thread(gui);
    tr.setName("Recorder");
    tg.setName("GUI");
    tr.start();
    tg.start();
}

My understanding is Recorder and GUI are runnable objects and they are made into threads via the new Thread command. When I use .start() I am beginning the execution of the thread and from here on the system decides which thread is running at any particular time.

Onto the Recorder and GUI classes.

public class Recorder
        implements Runnable, MouseListener {

//Constructor and other code

    public void mouseClicked(MouseEvent e) {

        if (e.getButton() == MouseEvent.BUTTON1) {
             //Record events
        }else{
             //Replay events
        }
        System.exit(0);
    }

    public void run() {
        System.out.println("Recorder");
    }
}

public class GUI 
    implements Runnable, MouseListener {

//Constructor, simply constructs JFrame and sets mouselistener to it

   public void mouseClicked(MouseEvent e) {
       System.exit(0);
   }

   public void run() {
        System.out.println("GUI");
   }

}

My application prints out Recorder and then GUI Allows me to record my events then right click on the JWindow to replay them...

but then when I click on the JFrame's close button or even in the frame because of the mouse listener it won't exit until all of the actions have been fully replayed?

One thing I did wonder is what ever I put in run is what is what keeps the thread running? So when System.out.println(""); is executed the thread dies? So I tried a while loop around them and my application successfully prints

GUI GUI GUI RECORDER RECORDER GUI RECORDER etc etc

So I can see that they threads are running concurrently... it's just that all of the other actions outside of the run don't seem to get executed... How can I include my mouse listener, etc within the threads execution?


You are confusing Threads with Objects. When you have an Object that is a Runnable that just gives a starting point for a Thread. However this doesn't mean that when another thread (in this case the Event thread that handles the MouseListener) calls a method in your Runnable that it executed by the thread that is executing the Runnable. When a method is called, it never switches to another thread. If you want this you need a mechanism. For instance a queue that the MouseListener can post tasks to, and in your Runnable.run() you then keep looking for a new task.


When swing initializes it starts it's own Event Dispatch thread. All your listener methods executes within this thread. It doesn't matter whether or not your listener object implements runnable.

See this tutorial to grasp the basics of multi-threading in context of Swing. http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

The actual answer to your question is in this part of the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

But I suggest that you go through the whole tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号