首页 \ 问答 \ 更改ReentrantLock的所有者(Changing owner for ReentrantLock)

更改ReentrantLock的所有者(Changing owner for ReentrantLock)

所以我写了一个小程序,它会绕着它移动,当它们碰撞时它们会向相反的方向移动,但是当我试图延迟执行时,它们不会在愚蠢的时候快速移动我得到java.lang.IllegalMonitorStateException在canvasRender中锁定.java,创建一个实例:

ReentrantLock renderLock = new ReentrantLock();

方法暂停执行一段时间,所以圈子不会超快移动。

 publlic void delay(){    
        renderLock.unlock();
        try { Thread.sleep(10); } catch (Exception e) {} ;
        renderLock.lock();
    }

然后从另一个类中创建一个窗口并添加actionListener

public static void main(String[] args){
//Buttons and other elements
// ...
JButton start = new JButton("Start!");
createAndShowGUI();
}

在createAndShowGUI()中:

static void createAndShowGUI(){
//adding elements to panels    
start.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        start(); //this will set gameIsRunning variable to true and create models
        while (gameIsRunning) {
            //update(); //which has delay(); at the end of frame drawing
            //but even if just put delay()
            delay(); //still says exception
            start.setEnabled(false); //while game is running button is unavailable
            }
            start.setEnabled(true);
   }
});
}

在这种情况下,我的锁被Thread main所拥有,但当我点击按钮“Start!”时 当前是Thread AWT-EventQueue-0 ,所以程序崩溃。 如何解决这个问题? (或我在哪里愚蠢?)


So I wrote a little program which will move circles around and when they collide they will move opposite direction, however when I'm trying to delay the execution so they won't move around stupidly fast I get java.lang.IllegalMonitorStateException Lock in canvasRender.java, creating an instance:

ReentrantLock renderLock = new ReentrantLock();

Method which will pause execution for a moment, so circles won't move around super fast.

 publlic void delay(){    
        renderLock.unlock();
        try { Thread.sleep(10); } catch (Exception e) {} ;
        renderLock.lock();
    }

then from another class where I create a window and add actionListener

public static void main(String[] args){
//Buttons and other elements
// ...
JButton start = new JButton("Start!");
createAndShowGUI();
}

In createAndShowGUI():

static void createAndShowGUI(){
//adding elements to panels    
start.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        start(); //this will set gameIsRunning variable to true and create models
        while (gameIsRunning) {
            //update(); //which has delay(); at the end of frame drawing
            //but even if just put delay()
            delay(); //still says exception
            start.setEnabled(false); //while game is running button is unavailable
            }
            start.setEnabled(true);
   }
});
}

In this case my lock is owned by the Thread main, but at the time when I click button 'Start!' current is Thread AWT-EventQueue-0, and so the program crashes. How to fix this issue? (or where am I silly?)


原文:https://stackoverflow.com/questions/42824677
更新时间:2024-04-20 20:04

最满意答案

例如,您可以检索元素的样式并更改它。 其他类型的小部件可能具有其他功能


e.g. you can retrieve the Style of the element and alter that. other type of widgets might have other functionalities

相关问答

更多
  • 我知道这有多令人沮丧,我一直在同一个地方。 我已经包含了我用来实现这个工作的所有步骤。 但这听起来像你几乎在那里,所以你可能不需要所有这些。 正如你所提到的使用你自己的css类名,你必须实现com.google.gwt.user.cellview.client.CellTable.Resources并将你的类的一个实例传递给CellTable的构造函数。 CellTable声明 CellTable myCellTable = new CellTable(10, MyResou ...
  • GWT的CssResource可以: @if user.agent safari { .unseen-activity div.unseen-label { left: 510px; } } @else { .unseen-activity div.unseen-label { left: 470px; } } .unseen-activity div.unseen-label { display: inline; margi ...
  • 我假设我们有一个类似于下面的结构
    A B C
    这就是我解决问题的方法: .hideTrivial span.trivial { display: none; }
    A
  • 例如,您可以检索元素的样式并更改它。 其他类型的小部件可能具有其他功能 e.g. you can retrieve the Style of the element and alter that. other type of widgets might have other functionalities
  • 我建议从http://gwtbootstrap.github.io/开始 以及: https : //developers.google.com/web-toolkit/doc/latest/DevGuideUiCss 然后,我将深入了解GWT文档,以便更好地处理创建自定义主题。 在某些时候,任何样式主题/框架都将无法满足您的需求,您需要为自己的样式设置一些基础知识。 就个人而言,我强烈建议使用UiBinder和ClientBundle: https://developers.google.com/web- ...
  • 看起来我发布的太快了。 这是我能够提出的,如果你有一个更优雅的方法,请帮助你: 我的菜单中的静态HTML文件如下所示:
  • Manage Events
  • 我能够注册鼠标按下事件并通过将其分配给该元素来监听它。 我看到的主要缺点是我必须为每个菜单项执行此操作。 这是我提出的一些代码: Element e = RootPanel.get("manage_events").getElement(); DOM.sinkEvents( ...
  • 您可以通过将以下内容添加到gwt.xml来禁用内联资源(所有ClientBundle资源): You can disable inlining of resources (all ClientBundle resources though) by adding the following to your gwt.xml:
    根据我的想法,我提出了一个样本,检查它是否符合您的需求 将以下css类添加到应用程序css中 .gwt-RadioButton { background-color: grey; border: 1px solid #FFFF00; margin: 10px; font-size: 2em; } 不需要添加任何addStyleNames作为RadioButton由deafult监听.gwt-RadioButton css类。 应用上面的css后的结果是这样的, 编辑: 在您提供上述样本后,您可以非常清楚自 ...
  • 我有一些想法:你是否将css导入应用程序? 您可以在application.gwt.xml或index.html中执行此操作。 是css加载? 你可以用firebug检查一下。 检查目标文件夹或war文件夹以找出路径。 I have some ideas: did you import the css into application? You can either do that in your application.gwt.xml or in your index.html. is the css l ...
  • FlexTable(扩展HTMLTable )似乎不会将其包含的单元格应用于任何特定样式。 有两种可能的解决方案: 将样式应用于封闭的FlexTable并使用后代选择器( .myFlexTable th {} )来设置单元格的样式。 使用CellFormatter将自己的样式名称添加到FlexTable中的单元格。 FlexTable (which extends HTMLTable) doesn't appear to apply any particular styles the cells it co ...
  • 相关文章

    更多
  • ReentrantLock有什么用处?
  • 关于多线程问题,signalAll,await问题
  • CentOS 6.3 更改yum源,改为163源
  • 更改我的网页默认的暴风影音播放器
  • eclipse的默认(打开)编辑器的更改
  • 最近工作回顾
  • weblogic启动server错误
  • Java并发编程总结---Hadoop核心源码实例解读
  • 一步一步掌握线程机制(四)---同步方法和同步块
  • Hadoop 创建用户及HDFS权限,HDFS操作等常用Shell命令
  • 最新问答

    更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(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?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在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)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)