ZendX
ZendX

Reputation: 3

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?

Upvotes: 0

Views: 1827

Answers (4)

user7774183
user7774183

Reputation: 11

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!");
}

Upvotes: 1

Valdrinium
Valdrinium

Reputation: 1416

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();
        }
    }       
}

}

Upvotes: 0

Stephan
Stephan

Reputation: 43013

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;
    }
}

Upvotes: 1

Anton Bessonov
Anton Bessonov

Reputation: 9803

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...)

Upvotes: 3

Related Questions