gpt4 book ai didi

java - 尝试使用线程时程序锁定

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:31 26 4
gpt4 key购买 nike

这是我在这里发表的第一篇文章,希望一切顺利。

我在 netbeans。我制作了一个带有按钮等的窗口。

我有一个名为 SimpleThread 的类,如下所示:

public class SimpleThread extends Thread {

public SimpleThread()
{

}

@Override
public void run()
{

}

我有不同类型的子类线程,它们扩展了简单线程(TimerThread、MouseGrabThread)。

public class MouseGrabThread extends SimpleThread{

private Point pt;
private JTextField x, y;

public MouseGrabThread(JTextField x, JTextField y)
{
super();
this.x = x;
this.y = y;
}

@Override
public void run()
{
while(this.isAlive())
{
int[] xyVal = new int[2];
xyVal = getCoords();
x.setText("" + xyVal[0]);
y.setText("" + xyVal[1]);
}
}

public int[] getCoords()
{
Point pt = MouseInfo.getPointerInfo().getLocation();

int[] retArr = new int[2];
retArr[0] = (int)pt.getX();
retArr[1] = (int)pt.getY();

return retArr;

}



public class TimerThread extends SimpleThread {

private JTextArea label;
private int time;

public TimerThread(JTextArea label, int time)
{
super();
this.label = label;
this.time = time;
}

@Override
public void run()
{
int counter = time;
while(counter != -1)
{

label.setText("You have: " + counter + " seconds until the timer ends!\n");
counter--;
try {
this.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
}

}
stop();
}

在我的 UI Window 类中,我有这个:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
timer.start();
if(timer.isAlive())
{
mouseGrab.start();
}
while(timer.isAlive())//######
{
if(!mouseGrab.isAlive())
{
mouseGrab.start();
}
}
}

当我按下按钮时,程序卡住了 10 秒。

我猜我标记的行 (//#####) 是导致 UI 在计时器持续时间内卡住的行,因为它在主线程中运行。我不知道如何纠正这个问题。

请原谅我缺乏编程知识,我现在正在自己进入线程,同时我正在大学学习一门非常简单的数据结构类(class)。如果可能的话,你能把答案尽可能地“简化”吗?

另外,我知道我这样做很糟糕,但我调用了 stop() 函数,尽管它不太好(不要向我开枪,我不知道还能怎么做!)如果有人可以为我回答这个问题,告诉我如何做得更好,那就太好了!

最佳答案

您可能想要的是在倒计时结束时结束 mouseGrab:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.

mouseGrab.start();
timer.start();

// Wait until countdown finishes
while(timer.isAlive()) {}

mouseGrab.stop();
}

在您粘贴的代码中,您在 timer 运行时不断启动 mouseGrab。您可能更希望在计时器打开时让鼠标抓取运行。

编辑:确实,stop() 已被弃用,您确实应该在 TimerThread 中使用 boolean running 属性,并将其 run() 方法的内容包装在某些

while (running) {
/* stuff */
}

然后用一些 setter 从外部“停止”这个线程。正确答案例如:

   mouseGrab.start();
timer.start();

// Wait until countdown finishes
while(timer.isAlive()) {}

mouseGrab.setRunning(false);
}

Edit2:这最终似乎就是你想要的:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
SimpleThread timer = new TimerThread(mouseGrab, jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second

mouseGrab.start();
timer.start();
}

与:

public class MouseGrabThread extends SimpleThread {

private Point pt;
private JTextField x, y;
private boolean running;

public MouseGrabThread(JTextField x, JTextField y) {
super();
this.x = x;
this.y = y;
}

@Override
public void run() {
running = true;
while(running) {
int[] xyVal = new int[2];
xyVal = getCoords();
x.setText("" + xyVal[0]);
y.setText("" + xyVal[1]);
}
}

public int[] getCoords() {
Point pt = MouseInfo.getPointerInfo().getLocation();

int[] retArr = new int[2];
retArr[0] = (int)pt.getX();
retArr[1] = (int)pt.getY();

return retArr;

}

public void break() {
this.running = false;
}
}


// ------------- //


public class TimerThread extends SimpleThread {

private MouseGrabThread mouseGrab;
private JTextArea label;
private int time;

public TimerThread(MouseGrabThread mouseGrab, JTextArea label, int time)
{
super();
this.label = label;
this.time = time;
this.mouseGrab = mouseGrab;
}

@Override
public void run()
{
int counter = time;
while(counter != -1)
{
label.setText("You have: " + counter + " seconds until the timer ends!\n");
counter--;
try {
this.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
}

}

mouseGrab.break();
}
}

关于java - 尝试使用线程时程序锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15839865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com