Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Using swing timers from actionListeners
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Daniel Pitts" data-source="post: 2025730"><p>Ah, I see the problem...</p><p></p><p>doRoll blocks until dieTm.stop() is called... The problem is, that</p><p>the Timer event won't be able to be dispatched until doRoll returns,</p><p>which means that actionPerformed won't be called, which means</p><p>dieTm.stop() won't be called, which means... DEADLOCK!</p><p></p><p>public int doRoll() {</p><p> setVisible(true);</p><p> counter = 0;</p><p> dieValue = (int)Math.ceil((Math.random()*6));</p><p> dieTm.start();</p><p> return dieValue;</p><p>}</p><p></p><p>public void actionPerformed(ActionEvent e) {</p><p> setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);</p><p> counter++;</p><p> System.out.println(counter);</p><p> if (counter == 10) {</p><p> dieTm.stop();</p><p> setIcon(dieImg[dieValue-1]);</p><p> return dieValue;</p><p> }</p><p>}</p></blockquote><p></p>
[QUOTE="Daniel Pitts, post: 2025730"] Ah, I see the problem... doRoll blocks until dieTm.stop() is called... The problem is, that the Timer event won't be able to be dispatched until doRoll returns, which means that actionPerformed won't be called, which means dieTm.stop() won't be called, which means... DEADLOCK! public int doRoll() { setVisible(true); counter = 0; dieValue = (int)Math.ceil((Math.random()*6)); dieTm.start(); return dieValue; } public void actionPerformed(ActionEvent e) { setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]); counter++; System.out.println(counter); if (counter == 10) { dieTm.stop(); setIcon(dieImg[dieValue-1]); return dieValue; } } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Using swing timers from actionListeners
Top