Write efficient and friendly multi-threaded J2ME networking applications

xiaoxiao2021-03-06  50

When you write J2ME networking applications, you will often write this code:

Public void CommandAction (Command C, Displayable S) {

IF (c == MexitCommand)

NotifyDestroyed ();

Else IF (c == mconnectCommand)

CONNECT ();

}

Private void connection () {

String url = getAppproperty ("NetWorkthReading.URL");

Try {

HTTPConnection HC = (httpConnection) Connector.Open (URL);

InputStream in = hc.openinputstream ();

INT contentLength = (int) hc.getlength ();

IF (contentLength == -1) contentLength = 255;

Byte [] raw = new byte [contentLength];

........

.......

}

What problems will occur when you run such a program? The WTK will prompt your networking job to block user input, let you go to another thread to operate. OK, then we will build a thread in accordance with his request, such as writing:

Public void CommandAction (Command C, Displayable S) {

IF (c == MexitCommand)

NotifyDestroyed ();

Else if (c == mconnectCommand) {

Thread t = new thread () {

Public void run () {

CONNECT ();

}

}

T.Start ();

}

}

This will pass this, then why is this? It is very important to understand the principle behind things. Let me introduce it. When the program runs, Application Management Software first initializes a MIDlet, then call his StartApp () method so that MIDlet enters the Active status, this The program branch is the main thread. After he performs other methods, it will return to this branch to continue. Then the network is a possible operation, meaning that he may not return. I have written on the Internet of the netbook to run on the phone, which is indeed time consuming. If he does not return, then it is not possible to carry out the following operation, and the user cannot enter. This looks like a mobile phone is like a dead, which is obviously not friendly. Take a look at the schematic below:

When we create a new thread in the app to handle the networking or browse a lot of RMS data, it is different. At this time, the main thread will return immediately after starting the thread, and it will not block.

Think about this, even if he can work normally, but every time the user presses the button, there will be new threads, which is obviously not efficient enough. Fortunately, WAIT () and Notify () / notifyall () in Java will coordinate such problems. We start the thread to let him enter the waiting state, when the user presses the button, let him continue to run. Code is like this:

Public synchronized void run () {

While (mtrucking) {

Try {wait ();

Catch (InterruptedException IE {}

IF (mtrucking) connect ();

}

}

PUBLIC SYNCHRONIZED VOID Go () {notify ();} This efficiency is higher! When the user is connected to the network, we should make a prompt interface, such as an animation telling the user to operate the networking operation. This is relatively friendly. Then when the user chooses the networked action, we let me do a good in advance display on the screen, and then display the result of the returned results after the end of the network. This is an excellent networking application. The following code can depict an animation effect on the screen, of course, you can also modify what you like. Import java.util. *; import javax.microedition.lcdui. *;

public class WaitCanvas extends Canvas {private int mCount, mMaximum; private int mInterval; private int mWidth, mHeight, mX, mY, mRadius; private String mMessage; public WaitCanvas () {mCount = 0; mMaximum = 36; mInterval = 100; mWidth = getWidth (); mHeight = getHeight (); // Calculate the radius int halfWidth = (mWidth - mRadius) / 2;. int halfHeight = (mHeight - mRadius) / 2; mRadius = Math.min (halfWidth, halfHeight); // Calculate the location. Mx = HALFWIDTH - MRADIUS / 2; My = HALFHEIGHT - MRADIUS / 2;

// Create a Timer to update the display. Timertask Task = new timetask () {public void Run () {mcount = (mcount 1)% mmaximum; repaint ();}}; timer Timer = new time (); time .schedule (task, 0, minterval); public void setment (String s) {mMessage = S; repaint ();} public void Paint (graphics g) {int = - (McOUNT * 360 / mmaximum); // Clear The Whole Screen. G.SetColor (255, 255, 255); g.fillRect (0, 0, mwidth, mahight); // Now draw the pinwheel. G.setcolor (0, 0, 0); g.drawarc (MX, MX, MRADIUS, MRADIUS, 0, 360); G. Fillarc (MX, MX, MY, MRADIUS, MRADIUS, THETA 90, 90); G. Fillarc (MX, MX, MX, MRADIUS, MRADIUS, THETA 270, 90 ); // Draw the message, if there is a message. If (mMmessage! = Null) g.drawstring (mMessage, Mwidth / 2, MHEIGHT, Graphics.Bottom | Graphics.hcenter);}} You can download Sun here The instance code provided is taken carefully, and it will benefit a lot.

转载请注明原文地址:https://www.9cbs.com/read-116503.html

New Post(0)