Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 38
Registered: ‎02-04-2009
My Device: 9530
Accepted Solution

Thread blocking

[ Edited ]

hello,

 

I was hoping you guys could help me out. I've read all the posts on the event thread dispatch problem and followed all of the advice in there with no avail. 

Here's my situation:

On the device I'm getting the blocking operation not permitted on event dispatch thread

I have already made separate Threads that I call to make an HTTPS connection:


public class SendPostRequest extends Thread
{
    String url;
    LoginSaver l;
    Hashtable data;
    String response = "";
    public SendPostRequest(String u, Hashtable d, LoginSaver ls)
    {
        url = u;
        l = ls;
        data = d;
    }
   
    public void start()
    {  
        try
        {
            OutputStream out;
            HttpsConnection conn;
            String postData = "";
       
            Dialog.alert("connecting");
            conn = (HttpsConnection)Connector.open(url);
            conn.setRequestMethod(HttpsConnection.POST);

 


//....
           
            input.close();
            conn.close();
            conn = null;
            response = raw.toString();
        }
        catch (Exception e)
        {
            Dialog.alert("postrequest crash: " + e.toString());
            System.out.println("I/O Exception: " + e.toString());
        }
    }
   
    public String getResponse()
    {
        return response;
    }

}

 

and then I run it like this:
private static String sendPost(String url, Hashtable data, LoginSaver l)
 {      
     SendPostRequest s = new SendPostRequest(url, data, l);
     s.start();
     return s.getResponse();
}

 

The first time I run this it crashes and give me the error at HTTPS connection creation line.

So do I need to interrupt the main EventThread to allow this connection to be completed, or am I doing something else wrong? Also, I'll note that the storm works perfect before I added in the new thread stuff, the problem shows with older phones like the 8800 series. Thanks for your help guys.

Message Edited by azdragon2 on 03-11-2009 11:26 PM
Developer
Posts: 163
Registered: ‎07-16-2008
My Device: Not Specified
My Carrier: Sprint

Re: Thread blocking

There are two things wrong - Your code for making the request should be in the thread's run() method, not start().  In your static method, you're calling thread.start, it goes to your thread..  and executes all that code immediately, not on a seperate thread.  Normally you'd call thread.start(), you don't need to change the start method at all, and you'd have your code in the run() method.

 

The second is with threading..  After you move your code to the run method, your call to thread.start() will return immediately.  So you can't return s.getResponse..  Or you can, but it would always be "".  You'll likely want to have that method return nothing, and then after you get the response in your thread's run() method, you can call other methods to process it from there.

Andrew Cowart | Metova
www.metova.com
Developer
Posts: 38
Registered: ‎02-04-2009
My Device: 9530

Re: Thread blocking

[ Edited ]

Wow,  that did it. Amazing, thanks a million acowart.

 

I changed the start method to run. And then I did this in the calling method:

 

private static String sendPost(String url, Hashtable data, LoginSaver l)
    { 
        SendPostRequest s = new SendPostRequest(url, data, l);
        s.start();

        while (l.getJSON().equals("")){}

 

//Waits until the thread sets the string in l.setJSON()

        String temp = l.getJSON();


//Once its set, grabs it and resets everything

 

       l.setJSON("");
        return temp;
    }

 

once again thanks a bunch. Also  is there a better way to give it time until it sets the response String? I hate putting a essentially an infinite while loop. I'd also like to avoid doing timeouts, if possible.

Message Edited by azdragon2 on 03-12-2009 01:10 AM
Developer
Posts: 53
Registered: ‎01-08-2009
My Device: Not Specified

Re: Thread blocking

You probably want to add a Thread.sleep(100) in that while loop (or even 500ms), otherwise you're putting the CPU in a tight loop and you'll probably be seeing that hourglass if the response takes too long to come back.
Developer
Posts: 62
Registered: ‎11-24-2008
My Device: Blackberry 9530

Re: Thread blocking

In addition to marcn suggestion, you could also do something like this:

 

 

private static String sendPost(String url, Hashtable data, LoginSaver l)
{
SendPostRequest s = new SendPostRequest(url, data, l);
s.start();

// Your old code
// while (l.getJSON().equals("")){}

// New code:
// Wait for the thread to notify us that it's done.
s.wait();

//Waits until the thread sets the string in l.setJSON()
String temp = l.getJSON();

//Once its set, grabs it and resets everything
l.setJSON("");

return temp;
}

 

 Then, inside your run loop, wrap your processing code in a "synchronized (this)" block, and call "this.notifyAll()" once the information is ready for processing.

 



All code is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement.
Developer
Posts: 62
Registered: ‎11-24-2008
My Device: Blackberry 9530

Re: Thread blocking

[ Edited ]

Actually, I don't think the solution I posted would work if implemented using the steps I described. I'll have to look up how to do it exactly when I'm done driving to work.

 

Edit: Grammar correction.

Message Edited by MySpoonIs2Big on 03-12-2009 07:39 AM


All code is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement.
Developer
Posts: 48
Registered: ‎02-10-2009
My Device: Not Specified

Re: Thread blocking

[ Edited ]

Also, Java best practice is to implement Runnable and not extend Thread. It doesn't hurt to extend Thread like you did, but technically it is not correct.

 

new Thread(new Runnable() { public void run() { // thread instructions } }).start();

 

 

But this is just me being a stickler. Smiley Tongue

Message Edited by cowkingcoder on 03-12-2009 09:26 AM