Home
Home Page
The menu of a choice in forms
The loader in HTML
Konfigurirovanie a site with the help of a file .htaccess
Options-Indexes
Perenapravlenie on other address
Replacement with the image. Method Farnera
Example the first. Headings of pages
Replace cautiously
Method of overlapping by a background (Cover-up Method)
WAP (Wireless Application Protocol)
Reduction of time of loading of webs - pages with help CSS
Mnogozadachnost` in JavaMe at HTTP connection
Interaction of streams.
Nine advice for job with Web 2.0
Beginning php to the programmer or how to start to earn
Substantiation of necessity of Storehouse of the data
Misses in the data
The directory by mistakes Oracle
Forms in HTML documents
Links
 

Mnogozadachnost` in JavaMe at HTTP connection

Introduction


Developing simple applications for JavaMe we usually do not require use mnogozadachnosti. But as soon as from midleta it is required to make such actions as communication{connection} with servers on HTTP the report or SMS, to show animation or to expect fulfilment of any event without mnogozadachnosti to not do without. In clause{article} we shall be focused on the decision following, really arisen practical, problems{tasks}. The mobile application should send the information on the server under report HTTP, receive the answer, and in a timeout to show an animated picture. Further it is supposed, that the reader is familiar with JavaMe, its{her} restrictions and opportunities. Further it is meant MIDP 2.0. In the first part of clause{article} we shall try to answer a question, whether and so it is necessary mnogozadachnost` at job with network connection. Further we shall consider two ways of an establishment of connection in a separate stream. In thirds of part we shall consider, how different streams can cooperate with each other. In the conclusion, there will be not a big story how all listed can be used by development of the real application.



HTTP - what for mnogozadachnost`?


Really, why simply to not put a call of connection in a code of performance what or commands. For example so:



public class Simple extends MIDlet implements CommandListener {

...

...


private void connectWOThread () {

String urlString = input.getString ();

HttpConnection conn = null;

try {

conn = (HttpConnection) Connector.open (" http: // " +urlString);

int resp = conn.getResponseCode ();

output.setString (new Integer (resp) .toString ());

} catch (IOException e) {

e.printStackTrace ();

} finally {

if (conn! =null) {

try {

conn.close ();

} catch (IOException e) {

e.printStackTrace ();

}

}

}

}


public void commandAction (Command command, Displayable arg1) {

if (command.equals (connectD)) {

connectWOThread ();

}

}


}


At first sight anything wrong in an above mentioned code no. However, on the majority of real devices, and also on some emulators this code will be executed will not be, there is a blocking or dedlok. We shall consider, how this code on some platforms and the reasons on which arises dedlok is carried out. In the first, the code is carried out in a system stream or a stream of events. The system blocks a stream called search about an establishment of connection until when connection will be actually established, in our case the system stream is blocked. During this moment system of safety of "sandpit", and as is known all midlety if they are not signed, are carried out in "sandpit", can demand from the user of the sanction to performance of operation of connection. The search about the sanction should execute a system stream which in turn expects end of an establishment of connection. We receive dedlok. Therefore, it strongly recommended all actions connected to network searches to bear{take out} in a separate stream.



Two ways of the decision of a problem.


The elementary multitask realization of an above mentioned code can look so:



public class Simple extends MIDlet implements CommandListener {

...

...

private void connectWThread () {

String urlString = input.getString ();

HttpConnection conn = null;

try {

conn = (HttpConnection) Connector.open (" http: // " +urlString);

int resp = conn.getResponseCode ();

output.setString (new Integer (resp) .toString ());

} catch (IOException e) {

e.printStackTrace ();

} finally {

if (conn! =null) {

try {

conn.close ();

} catch (IOException e) {

e.printStackTrace ();

}

}

}

}


public void commandAction (Command command, Displayable arg1) {

if (command.equals (connectSimple)) {

SimpleConnector sc = new SimpleConnector ();

new Thread (sc) .start ();

}

}


private class SimpleConnector implements Runnable {

public void run () {

connectWThread ();

}


}

}


We modified our code a little. We have born{have taken out} operation of an establishment of connection in a separate stream. The above mentioned code already works, he does not result to dedloku if we carry out connectSimple command. Thus, we can establish network connection with external resources inside our program. Now it is necessary to recollect, that we speak about the application working on the mobile device. MIDP 2.0 guarantees to us only that the application can start simultaneously at least 10 streams, and also open at least one network connection. Thus, if the logic of the application allows to make some connections simultaneously, for example, to send on the server some data, to receive from the server a picture on some platforms we make it we can not. Besides it, creation of a new stream rather resursoemkaja operation.


If we want, that our program worked on as it is possible a lot of devices the most simple decision will be the following: To start the parallel stream responding only for installation of network connections. During that moment when we need this connection, we simply activate this stream, he carries out necessary actions and "falls asleep".


Example of the changed code:... protected void startApp () throws MIDletStateChangeException {connecto = new ConnectionThread (); new Thread (connecto) .start (); Display.getDisplay (this) .setCurrent (mainForm);} private void connectWOThread () {String urlString = input.getString (); HttpConnection conn = null; try {conn = (HttpConnection) Connector.open (" http: // " +urlString); int resp = conn.getResponseCode (); output.setString (new Integer (resp) .toString ());} catch (IOException e) {e.printStackTrace ();} finally {if (conn! =null) {try {conn.close ();} catch (IOException e) {e.printStackTrace ();}}}} public void commandAction (Command command, Displayable arg1) {if (command.equals (connectSimple)) {connecto.conn ();}} private class ConnectionThread implements Runnable {private Object mut = new Object (); private boolean shouldRun = true; public void run () {while (shouldRun) {synchronized (mut) {try {mut.wait ();} catch (InterruptedException e) {}} if (! input.getString () .equals (" ")) {connectWOThread (); input.setString (" ");}}} public void conn () {synchronized (mut) {mut.notify ();}}}...


Apparently from a code, at the moment of start or activation of the application the new stream is started, which is there and then put on expectation mut.wait (). Activation of a stream and his{its} production in expectation is realized with the help of the monitor. As the monitor any object, except for primitive types and lines, in some realizations can act. Activation of a stream occurs by a call of a method mut.notify (). As this call is in the block synchronized it means, that the monitor is blocked for all other streams which want to it{him} will address. At us one stream is started only, therefore he and will be activated. The activated stream should block there and then the monitor for itself, that is reached{achieved} by synchronized the block.


What has turned out in result? At start or activation of the application the new sleeping stream which is activated at a choice by the user of the certain command is created. It allows to spend economically resources, as against an infinite cycle. After performance this stream becomes sleeping. Naturally in a method pauseApp () it is necessary to stop performance of an additional stream.