Results 1 to 10 of 10
  1. #1
    dewyatt is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default 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.
    Java 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;
    }
    Java 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:
    Java 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 07:57 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    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.

  3. #3
    dewyatt is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    it's actually not like that in my current code
    Then you are wasting everyone's time.

  5. #5
    dewyatt is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    dewyatt is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As you wish. I've suddenly lost my understanding though.

  9. #9
    dewyatt is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •