首页 \ 问答 \ 线程中断和ActionListener Java(Thread interruption and ActionListener Java)

线程中断和ActionListener Java(Thread interruption and ActionListener Java)

我有一个函数graphics()创建我的JFrame和两个JRadioButtons并向它们添加ActionListeners。 这个图形从main()调用,图形本身调用game()。

public void game() throws Exception
{

    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jTextArea1.setText("This is private information.");

    jRadioButton1.setVisible(true);
    jRadioButton2.setVisible(true);
    try {
    t.sleep(40000);
    repaint();
    } catch (InterruptedException e) {
    // We've been interrupted: no more messages.
    return;    
    }

显示“这是私人信息”后。 在文本区域中,我希望程序执行暂停40秒,或者直到用户按下JRadioButton,以较早者为准。 所以我添加了一个ActionListener并在其中调用了t.interrupt()。

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     t.interrupt();
    jRadioButton1.setVisible(false);
    jRadioButton2.setVisible(false);
    //System.out.println(t.interrupted());
    jTextArea1.setText("Please wait...");

    }

但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false。

任何帮助,将不胜感激。


I have a function graphics() that creates my JFrame and two JRadioButtons and adds ActionListeners to them. This graphics is called from main() and graphics itself calls game().

public void game() throws Exception
{

    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jTextArea1.setText("This is private information.");

    jRadioButton1.setVisible(true);
    jRadioButton2.setVisible(true);
    try {
    t.sleep(40000);
    repaint();
    } catch (InterruptedException e) {
    // We've been interrupted: no more messages.
    return;    
    }

After displaying "This is private information." in the text Area, I want the program execution to pause for 40 seconds, or until the user presses the JRadioButton, whichever is earlier. So I added an ActionListener and called t.interrupt() inside it.

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     t.interrupt();
    jRadioButton1.setVisible(false);
    jRadioButton2.setVisible(false);
    //System.out.println(t.interrupted());
    jTextArea1.setText("Please wait...");

    }

However, even after choosing the JRadioButton which should trigger the interrupt, that does not happen and t.interrupted returns false.

Any help would be appreciated.


原文:https://stackoverflow.com/questions/13061054
更新时间:2023-05-30 22:05

最满意答案

由于您无法在新项目中重现它,因此在此特定项目中必定会有不同的内容,这会导致您正在描述的问题。

您可以尝试的几件事(没有特别的顺序):

  • 检查您是否在两个项目中使用完全相同的库版本(有效的版本和不可用的版本):软件包版本和目标平台(net4,net35 ...)。

  • 删除非工作项目的packages文件夹,以便强制NuGet重新下载所有软件包。

  • 从“参考”节点选择“RestSharp”时,在“Visual Studio属性”窗口中查找引用库的确切路径。 在工作项目和非工作项目引用的库之间进行二进制比较。

  • 遗憾的是,SymbolSource上没有RestSharp的符号包,因此您无法在非工作项目中直接调试RestSharp。 如果您拥有许可证或之前未使用过试用版,则可以使用Reflector.NET VSPro

  • 将非工作项目的部分移动到工作项目,直到它停止工作。

编辑:

查看RestRequest源代码AddObject似乎没有使用您正在设置的JsonSerializer 。 您是否尝试过使用AddBody


Since you can't reproduce it in a new project, there must be something different going on in this particular project, that's causing the issues, you're describing.

A couple of things you could try (in no particular order):

  • Check that you're using the exact same version of the library in both projects (the one that works and the one that doesn't): package version and target platform (net4, net35...).

  • Delete the packages folder of your non-working project so that NuGet will be forced to re-download all the packages.

  • Lookup the exact path to the referenced library in Visual Studio Properties window when you have RestSharp from References node selected. Do a binary compare between the libraries referenced by the working and the non-working project.

  • Unfortunately there's no symbol package for RestSharp on SymbolSource, so you can't directly debug RestSharp in your non-working project. You could use Reflector.NET VSPro if you have the license or haven't used the trial before.

  • Move parts of your non-working project to the working one until it stops working.

EDIT:

Looking at the source code of RestRequest, AddObject doesn't seem to use the JsonSerializer you are setting. Have you tried using AddBody instead?

相关问答

更多
  • 您是否有机会在另一个线程下调用此代码? 也许是背景工作者? 如果是这样,那么可能就是不抛出异常的原因。 只需将所有代码包装在Try-Catch块中,您就能捕获所有异常。 更新: 我认为引发异常是因为你将一个数组放在没有属性名的JSON对象中,如果这样做,通过插入这一行 writer.WritePropertyName("arrayKey"); 在第89行之前,前一个异常将消失,但是你会在循环中得到一个新异常,这个新异常是由于键值对必须在一个对象{}内引起的,所以你不能将它们直接放在一个阵列。 无论如何,我 ...
  • 你不能或你应该将Json.NET添加到RestSharp。 在RestSharp的github repo上存在这个问题。 You cant or you should add Json.NET to RestSharp. There is a issue about this on the github repo of RestSharp.
  • 在上面的模型中,another_class是一个字符串字段。 我正在寻找一种方法,让我可以使用AnotherClass的对象分配another_class变量。 它会自动调用序列化方法。 但是,我已经找到了实现相同目标的方法,尽管我们自己调用这些方法。 因此,在AnotherClass类定义中,我们需要编写两个方法: to_json(*a)和self.json_create(o)如下所述: http : //www.skorks.com/2010/04/serializing-and-deserializi ...
  • 你可以试试这个: AnnotationIntrospector annotationInspector = new JacksonAnnotationIntrospector(); AnnotatedClass annotatedClass = AnnotatedClass.constructWithoutSuperTypes(OID.class, new ObjectMapper().getSerializationConfig()); List su ...
  • 我想,现在这是不可能的。 但是您可以为每个属性创建两个单独的序列化程序。 我知道,这是一个小解决方法,但它应该工作。 I think, this is not possible now. But you can create two separate serializers for each property. I know, this a little workaround, but it should work.
  • 由于您无法在新项目中重现它,因此在此特定项目中必定会有不同的内容,这会导致您正在描述的问题。 您可以尝试的几件事(没有特别的顺序): 检查您是否在两个项目中使用完全相同的库版本(有效的版本和不可用的版本):软件包版本和目标平台(net4,net35 ...)。 删除非工作项目的packages文件夹,以便强制NuGet重新下载所有软件包。 从“参考”节点选择“RestSharp”时,在“Visual Studio属性”窗口中查找引用库的确切路径。 在工作项目和非工作项目引用的库之间进行二进制比较。 遗憾的是 ...
  • 这个 ["student":{"name":"sam","rollNumer":1},"student":{"name":"tom","rollNumer":2}] 无效JSON(您可以使用jsonlint等在线工具自行验证 )。 查看JSON规范的详细信息: 对象的定义 : 一个对象是一组无名的名称/值对。 一个对象以{(左大括号)开始并以}(右大括号)结束。 每个名称后跟:(冒号),名称/值对由(逗号)分隔。 数组的定义 : 数组是值的有序集合。 数组以[(左括号)开始,以[]结尾(右括号)。 值由( ...
  • 我找到了解决办法,或者这可能是合适的解决方案。 无论哪种方式,它似乎都有效。 如果有更好的方法,请告诉我。 (我觉得应该有) 我定义了一个实现Map的内部类,并将该类的实例提供给JsonGenerator.writeObject()而不是为它提供Map 。 一旦通用声明被“隐藏”,Jackson似乎能够解析键和值类型,并为创建的MapSerializer提供非null的MapSerializer ,从而产生所需的JSON输出。 对测试源代码的以下添加/修改生成所需的输出。 p ...
  • 我设法通过这种方式注册IHttpRequest使其工作: container.Register(c => HttpContext.Current.ToRequestContext ().Get()).ReusedWithin(Funq.ReuseScope.None); 现在,当我尝试解决它们时,我总是得到IHttpRequest对象。 此外,在我的应用程序中进行了更多的测试之后,我能够检测到,如果并发性足够高,依赖于使用ReuseScope.Request注册 ...
  • 看来,自述文件中有一个错误。 https://github.com/restsharp/RestSharp/issues/886 https://github.com/restsharp/RestSharp/issues/947 这是它应该如何使用: 有一个重大变化:默认的Json Serializer不再与Json.NET兼容。 要使用Json.NET进行序列化,请从https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279 ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)