I am using a mouse listener for mouse pressed and released. When the mouse is pressed I want to have a counter incrementing a variable, and when the mouse is released I want to decrement that variable. Right now, my code is working and does that but the increment is going too fast I wanted to slow it down because I am using these numbers for the coordinates in a game. I tried adding a Thread.sleep(100) but I was getting skewed outputs. It looked like multiple threads were going at once and I had numbers all over the place. Below is the sample code.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.lang.*;
public class Sample extends JFrame {
private JPanel jp = new JPanel();
int i = 0;
boolean once = true;
boolean on = true;
Thread t1 = new Thread(new Increase());
Thread t2 = new Thread(new Decrease());
public sample() {
setVisible(true);
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(jp);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
if (!once) //false
{
t2.interrupt();
}
if (once) //true
{
once = false;
t1.start();
}
else {
t1 = new Thread(new Increase());
t1.start();
}
}
public void mouseReleased(MouseEvent event) {
t1.interrupt();
if (on) //true
{
on = false;
t2.start();
}
else {
t2 = new Thread(new Decrease());
t2.start();
}
}
});
}
public static void main(String[] args) {
new Sample();
}
public int getI() {
return i;
}
public void setI(int num) {
i = num;
}
class Increase implements Runnable {
public void run() {
int num = getI();
while (!Thread.currentThread().isInterrupted()) {
try {
setI(++num);
Thread.sleep(100);
System.out.println(num);
}
catch (InterruptedException e) {
}
}
}
}
//Thread.currentThread().isInterrupted()
class Decrease implements Runnable {
public void run() {
int num = getI();
while (!Thread.currentThread().isInterrupted()) {
try {
setI(--num);
Thread.sleep(100);
System.out.println(num);
}
catch (InterruptedException e) {
}
}
}
}
}
You're probably running into a race condition between the two threads, the fact that i
isn't volatile also suggests that the threads may not be working with the same actual value as each other.
Threads are also non-reentrant, meaning that once the run
method exists, they can not be restarted.
You could achieve the same result simply using a single Thread
and a "delta" (or change) value.
The following example uses a Swing Timer
, as it's simpler and allows me to update the UI safely, but the principle is the same.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel label;
private Timer timer;
private int value = 0;
private int delta = 1;
public TestPane() {
setLayout(new GridBagLayout());
label = new JLabel("0");
add(label);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
delta *= -1;
}
@Override
public void mouseReleased(MouseEvent e) {
delta *= -1;
}
});
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setValue(getValue() + delta);
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
label.setText(Integer.toString(value));
}
}
}
And just because I'm completely crazy, and it's nice to demonstrate the extra work load. This example uses two Thread
s.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements Value {
private JLabel label;
private volatile int value = 0;
private ManipulateRunner incrementRunner;
private ManipulateRunner decrementRunner;
private Thread incrementThread;
private Thread decrementThread;
public TestPane() {
incrementRunner = new ManipulateRunner(this, 1);
decrementRunner = new ManipulateRunner(this, -1);
setLayout(new GridBagLayout());
label = new JLabel("0");
add(label);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
decrementRunner.pause();
if (incrementThread == null) {
incrementThread = new Thread(incrementRunner);
incrementThread.start();
}
incrementRunner.resume();
}
@Override
public void mouseReleased(MouseEvent e) {
incrementRunner.pause();
if (decrementThread == null) {
decrementThread = new Thread(decrementRunner);
decrementThread.start();
}
decrementRunner.resume();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public int getValue() {
return value;
}
@Override
public void setValue(final int value) {
if (EventQueue.isDispatchThread()) {
this.value = value;
label.setText(Integer.toString(value));
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setValue(value);
}
});
}
}
}
public static interface Value {
public int getValue();
public void setValue(int value);
}
public static class ManipulateRunner implements Runnable {
protected final Object pauseLock = new Object();
private int delta;
private AtomicBoolean paused = new AtomicBoolean(false);
private AtomicBoolean stopped = new AtomicBoolean(false);
private Value value;
public ManipulateRunner(Value value, int delta) {
this.delta = delta;
this.value = value;
}
public void pause() {
if (!paused.get() && !stopped.get()) {
paused.set(true);
synchronized (pauseLock) {
pauseLock.notify();
}
}
}
public void resume() {
if (paused.get() && !stopped.get()) {
paused.set(false);
synchronized (pauseLock) {
pauseLock.notify();
}
}
}
public void stop() {
if (!stopped.get()) {
stopped.set(true);
synchronized (pauseLock) {
pauseLock.notify();
}
}
}
@Override
public void run() {
while (!stopped.get()) {
while (!stopped.get() && paused.get()) {
synchronized (pauseLock) {
try {
pauseLock.wait();
} catch (InterruptedException ex) {
}
}
}
if (!stopped.get()) {
value.setValue(value.getValue() + delta);
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
}
}
}
Personally, the solution which is the simplest and works, is the better solution, but that's me
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.