Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序在尝试使用线程时锁定_Java_Multithreading - Fatal编程技术网

Java 程序在尝试使用线程时锁定

Java 程序在尝试使用线程时锁定,java,multithreading,Java,Multithreading,这是我在这里的第一篇帖子,希望一切都好 我在上网本。我用钮扣之类的东西做了一个窗户 我有一个名为SimpleThread的类,如下所示: public class SimpleThread extends Thread { public SimpleThread() { } @Override public void run() { } 我有不同类型的子类线程扩展simplethread(TimerThread,MouseGrabThread) 在我的UI窗口类中,我有: privat

这是我在这里的第一篇帖子,希望一切都好

我在上网本。我用钮扣之类的东西做了一个窗户

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

public class SimpleThread extends Thread {

public SimpleThread()
{

}

@Override
public void run()
{

}
我有不同类型的子类线程扩展simplethread(TimerThread,MouseGrabThread)

在我的UI窗口类中,我有:

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在计时器期间冻结的那一行,因为它在主线程中运行。我不知道如何纠正这个问题

请原谅我缺乏编程方面的知识,我刚开始自学线程,而我正在大学里学习一门关于数据结构的非常简单的课程。如果可能的话,你能尽可能把答案“哑”下来吗


另外,我知道我做这件事很糟糕,但我调用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();
}
在粘贴的代码中,当
计时器运行时,您一直启动
鼠标标签
。您可能更希望在计时器打开时让鼠标抓取运行

Edit:事实上,
stop()
已被弃用,您确实应该在
timerRead
中使用
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();
}
与:


您可能希望在倒计时结束时结束
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();
}
在粘贴的代码中,当
计时器运行时,您一直启动
鼠标标签
。您可能更希望在计时器打开时让鼠标抓取运行

Edit:事实上,
stop()
已被弃用,您确实应该在
timerRead
中使用
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();
}
与:


如果我想让GUI在倒计时时保持可用,该怎么办?现在,整个程序冻结,它不会更新TimerRead输出的文本框或鼠标坐标的X和Y文本区域。您的
TimerRead
可以引用
mouseGrab
,因此
timer
可以自行决定何时打破它。请参阅我的(即将发布的)Edit2以获取说明。如果我想在GUI倒计时时保持其可用性,该怎么办?现在,整个程序冻结,它不会更新TimerRead输出的文本框或鼠标坐标的X和Y文本区域。您的
TimerRead
可以引用
mouseGrab
,因此
timer
可以自行决定何时打破它。有关说明,请参见my(传入)Edit2。