简体   繁体   中英

Java: Starting Thread on mouse click event

All! :) I am newbie in Java, so, maybe, my question is little stupid, but... I am trying to run a Thread by mouse click on JButton.

Here i added a mouse listener:

btnGo.addMouseListener(new MouseAdapter() {
 @Override
 public void mouseClicked(MouseEvent e) {
  ThreadTest t = new ThreadTest();
  t.run();
  }
});

And my ThreadTest class:

public class ThreadTest extends Thread{

public void run()
{
    while(true)
    {   
        System.out.println("Tick!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}

}

Thread is starting normally by clicking (i see "Tick-s in console :)" , but main thread with window hangs :( Цhy is this happening?

run() does not start Thread. Try start() instead.

btnGo.addMouseListener(new MouseAdapter() {
 @Override
 public void mouseClicked(MouseEvent e) {
  ThreadTest t = new ThreadTest();
  t.start();
  }
});

But be aware: You can start Thread only one time!

Maybe it's better to implement interface (not tested, sorry if there any compile errors):

public class RunnableTest implements Runnable{

public void run()
{
    while(true)
    {   
        System.out.println("Tick!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}

}

and then:

btnGo.addMouseListener(new MouseAdapter() {
 @Override
 public void mouseClicked(MouseEvent e) {
  Thread t = new Thread(new RunnableTest());
  t.start();
  }
});

(and maybe you need to stop thread before...)

Thread is starting normally by clicking (i see "Tick-s in console :)" , but main thread with window hangs :( Цhy is this happening?

In fact the current thread (UI thread here) has entered the run method. This thread is responsible for the refresh if your GUI. Since it is busy with "Ticks" messages priting, the window hangs...

Replace this line of cocde:

t.run();

with this:

t.start();

Don't forget to find a clean way for exiting the run method. Otherwise your thread will run for ever until the JVM is stopped. You would end up with something like this:

public class ThreadTest extends Thread{
   private boolean canGo=true;    

    public void run()
    {
        while(canGo)
        {   
            System.out.println("Tick!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }       
    }

    public void kill() {
        canGo = false;
    }
}
public class Mouse implements MouseListener, Runnable
{
Thread mouseWork;

@Override
public void run()
{
    while(true)
    {
        System.out.println("CRAZY!!!");
        System.out.println("!!!CRAZY");
    }
}
public void mousePressed(MouseEvent e)
{
    mouseWork = new Thread(this);
    mouseWork.start();
    System.out.println("GO!");
}
@Override
public void mouseReleased(MouseEvent e)
{
    mouseWork.stop();
    System.out.println("STOP!");
}

You need to lock the thread onject

public class ThreadTest extends Thread{

public void run()
{
    while(true)
    {   
        System.out.println("Tick!");
        try {
            synchronized(this)
            {
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM