J
Jeff
midp 2.0
Java SE 5.0
J2ME version 2.2
I have some problems with threads. In this post I first list my trouble code
and beneath the code you will find my questions....
This is a thread i've created:
public class NetworkThread extends Thread
{
private boolean networkStop = false;
private String cmd = "";
public NetworkThread()
{
}
synchronized void requestStop()
{
networkStop = true;
notify();
}
public void setCommand(String cmd) {
this.cmd = cmd;
}
public void stop() {
networkStop = true;
}
public void run() {
while (!networkStop) {
System.out.println("NetworkThread");
}
}
}
A debug function:
private void testing () {
System.out.println("TESTING");
}
Here is my code that runs the thread above
NetworkThread networkThread = new NetworkThread();
networkThread.start();
testing();
networkThread.stop();
I wonder why the word NetworkThread isn't written out to the debug window
when running this code?....
I also tryed to modify the code a bit:
private void testing() {
System.out.println("getServerResonse A");
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
System.out.println("getServerResonse B");
loginForm.setInfo("ERROR");
throw new IOException("HTTP reponse code: " + rc);
}
System.out.println("getServerResonse C");
InputStream inputStream = httpConnection.openInputStream();
/*
THIS CODE IS A SIMPLIFIED VERSION OF A MUCH MORE COMPLEX CODE
*/
}
NetworkThread networkThread = new NetworkThread();
networkThread.start();
testing();
networkThread.stop();
This code generate this output in debug window:
getServerResonse A
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
(I cut the list of NetworkThread, I guess there was several hundreds of
them....)
getServerResonse B and getServerResonse C wasn't written out and it looks
like the midlet went into a loop where it only write NetworkThread to the
screen and did not let any other processes do their tasks... In addition to
not writing out getServerResonse B and getServerResonse C the midlet didn't
reponse to mouse click on emulator
What am I doing wring here?
Jeff
Java SE 5.0
J2ME version 2.2
I have some problems with threads. In this post I first list my trouble code
and beneath the code you will find my questions....
This is a thread i've created:
public class NetworkThread extends Thread
{
private boolean networkStop = false;
private String cmd = "";
public NetworkThread()
{
}
synchronized void requestStop()
{
networkStop = true;
notify();
}
public void setCommand(String cmd) {
this.cmd = cmd;
}
public void stop() {
networkStop = true;
}
public void run() {
while (!networkStop) {
System.out.println("NetworkThread");
}
}
}
A debug function:
private void testing () {
System.out.println("TESTING");
}
Here is my code that runs the thread above
NetworkThread networkThread = new NetworkThread();
networkThread.start();
testing();
networkThread.stop();
I wonder why the word NetworkThread isn't written out to the debug window
when running this code?....
I also tryed to modify the code a bit:
private void testing() {
System.out.println("getServerResonse A");
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
System.out.println("getServerResonse B");
loginForm.setInfo("ERROR");
throw new IOException("HTTP reponse code: " + rc);
}
System.out.println("getServerResonse C");
InputStream inputStream = httpConnection.openInputStream();
/*
THIS CODE IS A SIMPLIFIED VERSION OF A MUCH MORE COMPLEX CODE
*/
}
NetworkThread networkThread = new NetworkThread();
networkThread.start();
testing();
networkThread.stop();
This code generate this output in debug window:
getServerResonse A
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
NetworkThread
(I cut the list of NetworkThread, I guess there was several hundreds of
them....)
getServerResonse B and getServerResonse C wasn't written out and it looks
like the midlet went into a loop where it only write NetworkThread to the
screen and did not let any other processes do their tasks... In addition to
not writing out getServerResonse B and getServerResonse C the midlet didn't
reponse to mouse click on emulator
What am I doing wring here?
Jeff