I'm a newbie: I have a question about threads

J

Jeff

midp 2.0
Java SE 5.0
J2ME version 2.2

Below is my code I'm stucked with..... The code here shows a midlet class
and another class derived from Thread (NetworkThread). This NetworkThread
gets started from the main midlet... Search my code for the term ??????? and
there you find my trouble area... What I'm having trouble with is that at
that point in my code a NetworkThread has been given a url to access, and
putting network access in the main thread is a bad thing because one can
never know how long it takes to access the server... I want this line:
System.out.println("NETWORK COMMINUCATION DONE"); to be processed after
NetworkThread is done requesting the url.... I'm not sure how to solve this
code so that the midlet waits for NetworkThread to access the url... But
while NetworkThread access the url I want the midlet to response to user
input, I don't want it to look like the midlet has crashed......

Any tips on how I can solve this issue will be greatly appreciated...

By the way, if you see anything else that could have been improved in my
code, then please tell me about it too....

MY CODE:


NETWORKTHREAD:
package com.test;
import java.io.IOException;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;

public class NetworkThread extends Thread
{
private boolean networkStop;
private boolean networkPause;
private HttpConnection httpConnection = null;
private InputStream inputStream;
private String Url;
private String netCommand = null;
private String netArg = null;
private LoginForm loginForm;

public NetworkThread(String serverURL)
{
Url = serverURL;
}

synchronized void requestStop()
{
networkStop = true;
notify();
}

synchronized void resumeGame()
{
networkPause = false;
notify();
}

synchronized void setCommand(String cmd, String arg)
{
System.out.println("setCommand = " + cmd);
netCommand = cmd;
netArg = arg;
networkPause = false;
notify();
}

void pauseThread()
{
networkPause = true;
}

public void run()
{
networkPause = true;
networkStop = false;

while (true)
{
if (networkStop) {
break;
}
synchronized(this) {
while (networkPause) {
try {
wait();
}
catch (Exception e) {}
}
}
synchronized(this) {
if (netCommand != null) {
if (netCommand.equals("LOGIN")) {
//Here some networking processing will
//be done
}
else if (netCommand.equals("LOGOUT")) {
}
netCommand = null;
}
}
pauseThread();
}
}
}

THE MIDLET:
package com.test;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.IOException;
import java.lang.String;

public class Test extends MIDlet implements Runnable
{
private Display display;
private NetworkThread networkThread;
private String Url;

public Test() {
display = Display.getDisplay(this);
}

public void startApp() throws MIDletStateChangeException {
if (networkThread == null) {
networkThread = new NetworkThread(Url);
networkThread.start();
}
else {
networkThread.start();
}
}

public void destroyApp(boolean unconditional) throws
MIDletStateChangeException {
}

public void pauseApp() {
}

public void commandAction(Command c, Displayable s) {
if (c == okCommand) {
networkThread.setCommand("LOGIN", "arg1");
???????
System.out.println("NETWORK COMMINUCATION DONE");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top