• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deadlock Problem - Please Help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don' know what is the problem actually. This warning "Warning: To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the commandAction() handler." exists when i click the submit button.
I have read some passage that advice to separate the main from the connection part.
I have done this as follows ..... but the problem is still the same.
Can anyone guide me some solution. Any helps would be appreciated.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;
public class SecondMidletServlet extends MIDlet implements CommandListener {
Display display = null;
List menu = null;
TextBox input = null;
String user = null;
//String url = "http://popolo.no-ip.com:23/phoneserver/RequestServlet";
static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command submitCommand = new Command("Submit", Command.OK, 2);
static final Command exitCommand = new Command("Exit", Command.STOP, 3);
String currentMenu = null;
public SecondMidletServlet() {
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
menu = new List("Invoke Servlet", Choice.IMPLICIT);
menu.append("Add a user", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
mainMenu();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
}
public void addName() {
input = new TextBox("Enter first name:", "", 5, TextField.ANY);
input.addCommand(submitCommand);
input.addCommand(backCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
mainMenu();
} else if (label.equals("Submit")) {
user = input.getString();
try
{
newThread thr = new newThread();
thr.invokeServlet(user);
thr.ReturnStr();

TextBox t = new TextBox("Second Servlet", thr.toString(), 1024, 0);
t.addCommand(backCommand);
t.setCommandListener(this);
}
catch(IOException e) {}
}
else
{
addName();
}
}
}

class newThread extends Thread
{
public String url = "http://popolo.no-ip.com:23/phoneserver/RequestServlet";
public StringBuffer strBuf = null;
public newThread() {}
public void invokeServlet(String user) throws IOException
{
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();
TextBox t = null;
try
{
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");

os = c.openOutputStream();
String str = "name=" + user;
byte postmsg[] = str.getBytes();
System.out.println("Length: "+ str.getBytes());
for(int i=0;i<postmsg.length;i++)
{
os.write(postmsg[i]);
}
// or you can easily do:
// os.write(("name="+user).getBytes());
os.flush();
is = c.openDataInputStream();
int ch;
while ((ch = is.read()) != -1)
{
b.append((char) ch);
System.out.print((char)ch);
}
strBuf = b;
}
finally
{
if(is!= null)
is.close();
if(os != null)
os.close();
if(c != null)
c.close();
}
}
public StringBuffer ReturnStr()
{
return strBuf;
}
}
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it seems to me that newThread is not behaving like a thread. Even though it isn't an abstract class, by extending Thread you're basically saying 'if you call start() on me, whatever is in my run() method will execute in a seperate(this) thread.' It's one of the reasons I think its better to implement Runnable (forced to implement run method and adhere to the above contract) than to extend Thread.
try moving your connection code in newThread into a run() method and changing the line where you call thr.invokeServlet to thr.start().
BTW, friendly pointer: if you place your code in between [code] and a [/code] it will format the code and make it much easier for everyone to read.
[ April 20, 2004: Message edited by: Jason Fox ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic