×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Getting warning to avoid deadlock - what should be written in the run method?

    Hi,

    In the Retrieve email code, im getting the warning as -To avoid potential deadlock, operations that may block, such as
    networking, should be performed in a different thread than the commandAction() handler.

    Got to know that from discussion forum's that thread concept should be used and networking operations should be written in run() method, but im not getting clearly of wat exactly should come under the run method.

    Any help would be appreciated.

    Here is the code for Retrieving email -

    class CheckMail extends MIDlet implements CommandListener {
    private String MAIL_SERVER_URL =
    "http://localhost:8080/examples/servlet/CheckMailServlet?";
    private int MSG_LIST_SIZE = 5;
    Display display = null;
    List menu = null;
    List dmenu = null;
    TextBox input = null;
    String[] msgs = new String[MSG_LIST_SIZE];
    String user = null;
    static int MAX_LIST_SIZE;

    int status = 0;
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);

    public CheckMail() {System.out.println("Inside the constructor");}

    public void startApp() throws MIDletStateChangeException {
    System.out.println("Inside the startApp");
    display = Display.getDisplay(this);
    displayMenu();
    }

    public void pauseApp() {System.out.println("Inside the pauseApp");}

    public void destroyApp(boolean unconditional) {
    System.out.println("Inside the destroyApp");
    notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
    if(c == exitCommand ) {
    destroyApp(true);
    } else if (c == backCommand) {
    handleBack();
    } else if (c == submitCommand) {
    user = input.getString();
    retreiveMail(0, null);
    } else if (d == dmenu) {
    handleMainMenu();
    } else if (d == menu) {
    handleMsgMenu();
    } else
    loginUser();
    }




    /* Display Main Screen */
    private void displayMenu() {
    dmenu = new List("Check Email", Choice.IMPLICIT);
    if (user == null)
    dmenu.append("Login", null);
    else
    dmenu.append("Logout", null);
    dmenu.append("Check Mail", null);
    dmenu.addCommand(exitCommand);
    dmenu.setCommandListener(this);
    display.setCurrent(dmenu);
    status = 0;
    }

    /* This method takes users Mail ID and password*/
    private void loginUser() {
    input = new TextBox(
    "Enter Login Name/Password (Seperate by /) :", "test/test", 25,
    TextField.ANY);

    input.addCommand(submitCommand);
    input.addCommand(backCommand);
    input.setCommandListener(this);
    display.setCurrent(input);
    }

    /* This method retrieves Message with msgNum and
    if no MsgNum, displays the list of all msgs*/
    private void retreiveMail(int s, String msgNum) {

    String popURL = MAIL_SERVER_URL + "u=" + user;
    if (s == 0)
    popURL = popURL; // Login
    if (s == 1)
    // List messages
    popURL = popURL + "&s=" + msgNum + "&a=l";
    else if (s == 2)
    // Retreive message with i = msgNum
    popURL = popURL + "&i=" + msgNum + "&a=r";

    StreamConnection c = null;
    InputStream is=null;
    StringBuffer sb = null;
    String err = null;

    try {
    c = (StreamConnection)Connector.open(popURL,
    Connector.READ_WRITE);

    is = c.openInputStream();
    int ch;
    sb = new StringBuffer();
    while ((ch = is.read()) != -1) {
    sb.append((char)ch);
    }
    } catch(Exception ex){ err = ex.getMessage(); } finally {
    try {
    if(is!= null) {is.close(); }
    if(c != null) {c.close(); }
    } catch(Exception exp) { err = exp.getMessage(); }
    }

    if (err == null) {
    if (s == 0) {
    user = sb.toString();

    if (user.indexOf("INVALIDUSR") >= 0) {
    user = null;
    showAlert("Invalid User Name and Password");
    } else {
    input = null;
    dmenu = null;
    displayMenu();
    }
    } else if (s == 1) // For retreiving the list
    showMList(sb.toString());
    else // s ==2, show Message
    showMsg(sb.toString());
    } else
    showAlert(err); // Need to Show Error Page
    }

    /* Display the selected Message*/
    private void showMsg(String msg) {
    StringBuffer buffer = new StringBuffer(200);
    int delim = 'ß';
    int j = msg.indexOf(delim);
    int i =0;

    // Get's FROM
    buffer.append(msg.substring(0,j ));
    for (int k=0;k<4;k++) {
    i = j+1;
    j = msg.indexOf(delim, i);
    // Get's TO, CC, Subject, Date
    buffer.append(msg.substring(i,j ));
    }
    // Rest of the Message
    buffer.append("\n" + msg.substring(j+1 ));
    showAlert(buffer.toString());
    }

    /* Display Error On screen*/
    private void showAlert(String err) {
    Alert a = new Alert("");
    a.setString(err);
    a.setTimeout(Alert.FOREVER);
    display.setCurrent(a);
    }

    /* Display MessageList of User*/
    private void showMList(String msgStr) {
    for (int i=0;i<MAX_LIST_SIZE;i++) {
    msgs[i] = "";
    }
    menu = new List("Messages", Choice.IMPLICIT);
    int delim = 'ß';
    String sub = null;
    int i =0;
    int k = 0;
    int l =0;
    int j =msgStr.indexOf(delim);
    while( j >= 0) {
    sub = msgStr.substring(i,j);
    if ((k % 2) == 0) {
    msgs[l] = sub; // First would be Message Number
    l++;
    } else
    menu.append(sub, null);
    k++;
    i = j + 1;
    j = msgStr.indexOf(delim, i); // Rest of substrings
    }
    sub = msgStr.substring(i); // Last substring
    menu.append(sub, null);
    menu.addCommand(backCommand);
    menu.addCommand(exitCommand);
    menu.setCommandListener(this);
    status = 1;
    display.setCurrent(menu);
    }

    private void handleBack() {
    if (status == 1) {
    dmenu = null;
    displayMenu();
    } else
    display.setCurrent(menu);
    }

    private void handleMainMenu() {
    int index = dmenu.getSelectedIndex();
    switch(index) {
    case 0 :
    if (user != null) {
    retreiveMail(1, "0");
    break;
    }
    case 1 :
    status = 1;
    loginUser();
    break;
    case 2 :
    break;
    }
    }

    private void handleMsgMenu() {
    int index = menu.getSelectedIndex();
    if (!msgs[index].equals("")) {
    retreiveMail(2, msgs[index]); // Retreive Mail
    }
    }
    }


    Regards
    Nagesh

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Getting warning to avoid deadlock - what should be written in the run method?

    Hi Nagesh,
    If I am not wrong then this issue was previously discussed..
    Well the code that is responsible to receive the Mail must be inside the run().
    When you are trying to open the Http Connection/Reading-writing-editing the user data,must be on the different thread,as an individual.

    So put the retreiveMail() methods on the thread,
    is this make sense?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Getting warning to avoid deadlock - what should be written in the run method?

    the IDE will give this kind of warning if you are trying to set up Connection within the main Thread of ur application...
    this is because whilst waiting for a response from ur server (or the url u are trying to open) the whole system will hang, no graphics will be updated and stuff... so what the IDE is hinting to (and which is also the best way) is to create a seperate thread which does the connection so that the main app Thread will still be able to do its own stuff (for example react on commands (buttons) and/or keep painting what it has to paint)...

  4. #4
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Re: Getting warning to avoid deadlock - what should be written in the run method?

    Quote Originally Posted by raj_J2ME View Post
    Hi Nagesh,
    If I am not wrong then this issue was previously discussed..
    Well the code that is responsible to receive the Mail must be inside the run().
    When you are trying to open the Http Connection/Reading-writing-editing the user data,must be on the different thread,as an individual.

    So put the retreiveMail() methods on the thread,
    is this make sense?
    You mean to say that all the methods in the code which i have pasted, should be written in another file which implements Runnable?

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Getting warning to avoid deadlock - what should be written in the run method?

    Quote Originally Posted by nagesh.chetana View Post
    You mean to say that all the methods in the code which i have pasted, should be written in another file which implements Runnable?
    HI Nagesh,
    As far as I have read the code pasted here,Just you need to write the method which retrieve the mail,thats all,okey
    You can do one more thing is that just make a thread and call the retreiveMail() method from the thread's run()..
    is this make sense?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  6. #6
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Re: Getting warning to avoid deadlock - what should be written in the run method?

    Quote Originally Posted by raj_J2ME View Post
    HI Nagesh,
    As far as I have read the code pasted here,Just you need to write the method which retrieve the mail,thats all,okey
    You can do one more thing is that just make a thread and call the retreiveMail() method from the thread's run()..
    is this make sense?
    Ok Raj,
    I got it. i'll implement and see whether it works.
    Thanks

  7. #7
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    148

    Re: Getting warning to avoid deadlock - what should be written in the run method?

    Quote Originally Posted by nagesh.chetana View Post
    Ok Raj,
    I got it. i'll implement and see whether it works.
    Thanks
    Hi Raj,

    The problem is solved by anonymous implementation of the Thread.
    Example for submit command -

    if (c == submitCommand) {
    try{
    new Thread(){
    public void run(){
    while (isThread){
    user = input.getString();
    retreiveMail(0, null);
    try{
    sleep(1000);
    }
    catch (Exception e){
    }
    isThread = false;
    }
    }
    }.start();
    }
    catch (Exception e){
    e.printStackTrace();
    }
    }


    Thanks for the suggestion.

    Rgds
    Nagesh

  8. #8
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Getting warning to avoid deadlock - what should be written in the run method?

    Hi,
    Nagesh,it is nice to see,
    Your welcome,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. Cannot run command line GCCE HelloWorldBasic on Emulator
    By loginssuck in forum Symbian Tools & SDKs
    Replies: 33
    Last Post: 2008-06-21, 12:58
  2. Warning message appears when run the application
    By afka in forum Mobile Java General
    Replies: 3
    Last Post: 2006-08-22, 17:57
  3. warning LNK4099 edll.pdb was not found.. help me
    By mayank.mca in forum Symbian
    Replies: 1
    Last Post: 2006-01-30, 12:13
  4. Plz resolve Deadlock warning
    By yasomca in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-11-04, 07:43
  5. App written for 3650 will not run on 7210?
    By richardsenior in forum Mobile Java General
    Replies: 6
    Last Post: 2003-12-09, 23:00

Posting Permissions

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