So, I started with a clean project in Netbeans, talkLock_beta. The idea is that now that most of my R&D work is done, I should try to design talkLock rather than just hack stuff together.
I have done so, and made a midlet that does a simple test. I increment an integer variable in a thread, a global integer variable for my midlet. The midlet knows that the integer was incremented in the thread. So I don't have to do any crazy message passing things with threads, just use globals. Yay!
I could clean up the code and call it threadsandglobals or something, but for now, here it is.
// includes
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.List.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
import javax.microedition.rms.*;
import javax.microedition.rms.RecordEnumeration.*;
import javax.microedition.io.*;
// our midlet
public class talkLock_beta extends MIDlet implements CommandListener {
// Commands
private Command runthreads;
private Command getvalues;
private Command resetvalues;
private Command exit;
//Forms
Form screenForm;
// Strings
// Ints
int getchunknum;
int postchunknum;
// Displays
Display thescreen;
// our void main
public talkLock_beta(){
// construct our main screen, a Form
screenForm = new Form("talkLock");
// construct our Commands for the CommandListener
exit = new Command("exit", Command.EXIT, 1);
runthreads = new Command("run threads", Command.SCREEN, 0);
getvalues = new Command("get values", Command.SCREEN, 0);
resetvalues = new Command("reset values", Command.SCREEN, 0);
// add our Commands to the Form
screenForm.addCommand(exit);
screenForm.addCommand(runthreads);
screenForm.addCommand(getvalues);
screenForm.addCommand(resetvalues);
// ping the user
screenForm.append("midlet is alive");
// initialize our counters
postchunknum = 1;
getchunknum = 1;
} // end of our void main
// our runthreads method
public void runthreads(){
// the GET thread
Thread getthread = new Thread(){
public void run(){
++getchunknum;
} // end of run
}; // end of getthread
// the POST thread
Thread postthread = new Thread(){
public void run(){
++postchunknum;
} // end of run
}; // end of getthread
// see how they run
postthread.start();
getthread.start();
} // end of runthreads method
// midlet lifecycle destroyApp method
public void destroyApp (boolean unconditional) { }
// midlet lifecycle pauseApp method
public void pauseApp() { }
// midlet lifecycle startApp method
public void startApp() {
// initialize our Display
thescreen = Display.getDisplay(this);
// set our Form as the current Display
thescreen.setCurrent(screenForm);
// flag the CommandListener to listen to our Display object
screenForm.setCommandListener(this);
}
// we have to define our methods for the CommandListener to call
public void commandAction (Command c, Displayable s) {
// our exit command
if(c == exit){
destroyApp(false); notifyDestroyed();
} // end of exit
// our runthreads command
else if (c == runthreads){
runthreads();
} // end of runthreads
// our getvalues command
else if (c == getvalues){
screenForm.append("" + getchunknum);
screenForm.append("" + postchunknum);
} // end of getvalues
// our resetvalues command
else if (c == resetvalues){
getchunknum = 0;
postchunknum = 0;
} // end of resetvalues
} // end of commandAction
} // end of midlet
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment