Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-24-2008, 09:01 AM
Member
 
Join Date: Sep 2008
Posts: 5
dewyatt is on a distinguished road
A little help please? :/
I'm rather new to Java and I'm having some trouble with threading.
I have read the essential concurrency java docs multiple times and lots of other java threading documentation but I'm still lost.
I'm writing a JavaME MIDP 2 program which has animated GIF images.
Each image has it's own animation thread.
Basically the problem is sometimes the program just freezes up.
I'm quite certain it's a threading issue.
Code:
public class GifAnimation { GifAnimation() { myAnimator = null; myThread = null; myActive = false; myPaused = false; } void Start(String imageFile, Canvas theCanvas) { Stop(); myAnimator = new GifAnimator(); myAnimator.setup(imageFile, this, theCanvas); myActive = true; myThread = new Thread(myAnimator); myThread.start(); } void Stop() { myAnimator = null; myActive = false; myPaused = false; if (null != myThread && myThread.isAlive()) { try { myThread.interrupt(); myThread.join(); } catch (InterruptedException e) { } } myThread = null; } public void Pause() { if (myPaused) return; myPaused = true; myThread.interrupt(); } public void Resume() { if (!myPaused) return; myPaused = false; myThread.notify(); } public synchronized Image getImage() { return myImage; } public synchronized void setImage(Image i) { myImage = i; } private GifAnimator myAnimator; private Thread myThread; boolean isActive; boolean isPaused; private Image myImage; }
Code:
public class GifAnimator implements Runnable { private GifDecoder myGifDecoder; private GifAnimation myAnimation; private Canvas myCanvas; public void setup(String imageFile, GifAnimation theAnimation, Canvas theCanvas) { myAnimation = theAnimation; myCanvas = theCanvas; myGifDecoder = new GifDecoder(); myGifDecoder.read(imageFile); myAnimation.setImage(myGifDecoder.getImage()); } public void run() { int n = myGifDecoder.getFrameCount(); int t = 0; while (myAnimation.isActive()) { for (int i = 0; i < n; ++i) { while (myAnimation.isPaused()) { synchronized (this) { try { wait(); } catch (InterruptedException e) { } } } myAnimation.setImage(myGifDecoder.getFrame(i)); t = myGifDecoder.getDelay(i); myCanvas.repaint(); myCanvas.serviceRepaints(); try { Thread.sleep(t); } catch (InterruptedException e) { } } } } }
My main thread is a Canvas derived class.
It has a GifAnimation member variable.
It calls GifAnimation.Next() when a certain key is pressed.
Here's the relevant bits:
Code:
public class Main extends Canvas { private GifAnimation myAnimation; public Main() { super(); Next(); } protected void showNotify() { myAnimation.Resume(); } protected void hideNotify() { myAnimation.Pause(); } protected void paint(Graphics g) { Image img = myAnimation.getImage(); if (null != img) g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT); } private void Next() { ... myAnimation.Start(someNewImage, this); repaint(); } public void keyPressed(int keyCode) { switch (keyCode) { case Canvas.KEY_STAR: Next(); break; } } }
I apologize for the lengthiness of it all, I've stripped it down as much as possible.
Could someone please help me synchronize things here?
I suspect I'm using wait()/notify() incorrectly but I'm not sure (though I have read about it as much as possible).
I think "Deadlock Drawing" (sonyericsson developer website) may be part (or all!) of the problem but I really don't know.

I could really use some help on this, I'm utterly lost.
This program is important to me, it's a learning aid I intend to use every day.

Last edited by dewyatt : 09-24-2008 at 09:57 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-24-2008, 04:33 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Have you tried debugging your code? Add println() statements to trace logic flow and variable values.

For wait and notify to communicate, they must be issued against the same object. You have notify using the mythread object and wait using "this" in GifAnimator. Two different objects.
Do a search for some simple examples of wait and notify usage.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-24-2008, 11:23 PM
Member
 
Join Date: Sep 2008
Posts: 5
dewyatt is on a distinguished road
Quote:
Originally Posted by Norm View Post
Have you tried debugging your code? Add println() statements to trace logic flow and variable values.

For wait and notify to communicate, they must be issued against the same object. You have notify using the mythread object and wait using "this" in GifAnimator. Two different objects.
Do a search for some simple examples of wait and notify usage.
Yes, I removed all the println statements to shorten the code.
I'll add them back in and post where it hangs.

As far as wait/notify on different objects, it's actually not like that in my current code, I think I was just experimenting when confused.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-24-2008, 11:45 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
it's actually not like that in my current code
Then you are wasting everyone's time.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-25-2008, 12:35 AM
Member
 
Join Date: Sep 2008
Posts: 5
dewyatt is on a distinguished road
Quote:
Originally Posted by Norm View Post
Then you are wasting everyone's time.
I changed the code after I posted this.
Rather than relying completely on strangers to solve my problem, I have been trying to fix it myself.
Now stop wasting my thread space.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-25-2008, 02:02 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 793
Fubarable is on a distinguished road
Quote:
Originally Posted by dewyatt View Post
Now stop wasting my thread space.
Please do me a favor and take some time to go through Norm's posting history, not just the number of posts, but read a selection of them. This should give you an idea of just how helpful he is here. Next you may wish to reconsider this comment above and take his suggestions to heart.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-25-2008, 02:34 AM
Member
 
Join Date: Sep 2008
Posts: 5
dewyatt is on a distinguished road
Quote:
Originally Posted by Fubarable View Post
Please do me a favor and take some time to go through Norm's posting history, not just the number of posts, but read a selection of them. This should give you an idea of just how helpful he is here. Next you may wish to reconsider this comment above and take his suggestions to heart.
I noticed he had over 1000 posts the first time he posted.
I'm sure he's been helpful to others.
In fact, his first comment to me would have been helpful if I hadn't already done that and known that.
His second comment to me was, however, a little bit rude and somewhat ignorant.
Ignorant because, as I said:
"Rather than relying completely on strangers to solve my problem, I have been trying to fix it myself."
Naturally, anybody with a small amount of determination would be doing this.
And nobody is going to update a post every time he changes one line of code.

Personally, I help many people on other forums.
The difference is that I'm helpful and understanding.
I understand that people make mistakes, people don't always think of everything, not everyone understands Inverse Kinematics, etc, etc.

I'd rather wait around for an understanding person to help me.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-25-2008, 03:56 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 793
Fubarable is on a distinguished road
As you wish. I've suddenly lost my understanding though.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-25-2008, 06:55 AM
Member
 
Join Date: Sep 2008
Posts: 5
dewyatt is on a distinguished road
Quote:
Originally Posted by Fubarable View Post
As you wish. I've suddenly lost my understanding though.
That's fine.
I've actually already fixed the problem.
serviceRepaints() being called was part of it.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-25-2008, 07:15 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 793
Fubarable is on a distinguished road
Congratulations.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 09:51 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org