×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    sabzevar
    Posts
    11

    Question Avoid deadlock in j2me?

    hi
    this program in j2me is not threading.
    PLZ, Avoid deadlock in this program j2me?
    //-------------------------------------------------------
    Class1:
    public String getData(String addr)
    {
    ...
    ... Connector.open(...);
    ...
    return data;
    }
    //-----------------------------------------------------
    ClassGetText extends Class1:
    public String get(String addr)
    {
    ...
    String st=getData(addr);
    ...
    return st;
    }
    //-----------------------------------------------------
    ClassGetPicture extends Class1:
    public Image get(String addr)
    {
    ...
    String st=getData(addr);
    ...
    byte[] b=st.getBytes();
    Image img=Image.createImage(b,0,b.length)
    return img;
    }
    amirsajjadi.com

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Avoid deadlock in j2me?

    If you have only one thread, then it is not deadlocking. If you are seeing the application freeze, it is probably because an exception is being thrown and swallowed.

    You are reading data from a stream of bytes, and converting it to a String. You are then converting the String to a byte[] and trying to create an Image. This is a bad plan. Converting bytes to characters and back does not always give you back the same bytes as you started with. Keep the the byte[] as a byte[].

    Also, make sure you are reading as many bytes as you think you are.

    Graham.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    sabzevar
    Posts
    11

    Question Avoid deadlock in j2me?

    this program is not threading ,So deadlock occurs and sometimes gives the error illegalargumentException

    plz help me.

    Code:
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    
    public class LoadDataFromInternet {
    
        public String GetDataFromWebService(String addressUrl,boolean isUTF8)
        {
            String rt;
            StringBuffer sb = new StringBuffer();
            try
            {
              HttpConnection c=(HttpConnection)Connector.open(addressUrl, Connector.READ, true);
              c.setRequestMethod(HttpConnection.GET);
              if(c.getResponseCode()==HttpConnection.HTTP_OK)
              {
                InputStream is=c.openInputStream();
                InputStreamReader isr=null;
                if(isUTF8)
                {
                    isr=new InputStreamReader(is, "UTF-8");
                }
                int ch=0;
                do
                {
                    if(isUTF8)
                    {
                        ch=isr.read();
                    }
                    else
                    {
                        ch=is.read();
                    }
                    sb.append((char)ch);
                }while(ch!=-1);
    
                try
                 {
                    if(is!=null)
                    {
                        is.close();
                        is=null;
                    }
                    if(c!=null)
                    {
                        c.close();
                        c=null;
                    }
                 }
                 catch(Exception ex)
                 {
                   ex.printStackTrace();
                   //not close
                 }
    
                rt=sb.toString();
    
              }
              else
              {
                  rt=null;
              }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                rt=null;
            }
            return rt;
        }
    }
    //----------------------------------------------------------------------------
    import javax.microedition.lcdui.Image;
    
    public class GetPictureClass extends LoadDataFromInternet {
        public Image GetPicture() throws Exception
        {
            Image img;
            byte[] b=GetDataFromWebService("www.example-pic.com/a.png",false).getBytes();
            if(b!=null)
            {
                img=Image.createImage(b,0,b.length);
            }
            else
            {
                img=null;
            }
            return img;
        }
    
    }
    //----------------------------------------------------------------------------
    public class GetDataClass extends LoadDataFromInternet {
    
        public String CheckUser()
        {
            return (GetDataFromWebService("www.example.com",true));
        }
    
    }
    amirsajjadi.com

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,561

    Re: Avoid deadlock in j2me?

    I have merged both your threads here. Please dont multipost for similar issues.

    Thanks.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    sabzevar
    Posts
    11

    Question Avoid deadlock in j2me?

    Excuse me
    My main question is that, I want a function that has a return value can turn into a thread.

    example :
    Code:
    public function String get(String addr)
    {
           return "amir";
    }
    Thanks
    amirsajjadi.com

  6. #6
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Avoid deadlock in j2me?

    Hi amirsajjadi ,

    The code what you have shared doe's have any threads , It does't cause any dead lock . The http , sms all these operations are blocking operation you need to do it in a separate thread than the main thread . Where ever you are calling this method see in that place . The problem might be over there.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    sabzevar
    Posts
    11

    Re: Avoid deadlock in j2me?

    Thanks for all
    but
    My main question is:
    how to call the following function of thread ?


    public function String get(String addr)
    {
    return "amir";
    }
    amirsajjadi.com

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Avoid deadlock in j2me?

    Try Oracle's Java Essentials tutorial on Defining and Starting a Thread.

    The IllegalArgumentException is probably because you are passing invalid data to createImage() - this may be due to you converting the bytes to chars, then a String to a byte[].

    Graham.

Similar Threads

  1. How to avoid potential deadlock and perform in different thread.
    By shamun.toha in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-12-03, 09:48
  2. Warning: To avoid potential deadlock
    By wijaya88 in forum Mobile Java General
    Replies: 2
    Last Post: 2009-09-07, 06:43
  3. Replies: 7
    Last Post: 2009-01-13, 06:54
  4. To avoid potential deadlock, operations that may block
    By tieuquai in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2008-05-12, 22:25
  5. Undocumented Canvas repaint/paint deadlock in Nokia j2me
    By fieres in forum Mobile Java General
    Replies: 3
    Last Post: 2005-05-31, 03:09

Posting Permissions

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