Hi ! My midlet sends some data to a servlet using Httpconnection. Then it recieves a response from the server and shows a message based on that response. Following is how I do it: 1) Main thread displays the Busy/Progress screen 2) A child thread is created that carries out network communication. It receives a response from the server and then displays new screens.
My questions are: A) Should the child thread that does the networking, display GUI's. Or it should only do network communication. I can't change the GUIs in the main thread so I have to change them in the networking thread.
B) What about synchronization issues. Suppose I have a Form object in my main thread. Then it would be updated in the child thread. So do I need to serialize access to such code. BTW, i read somewhere that the MIDP user interface classes are thread-safe. I don't really understand what does it mean. C) When I press the "Send" button for the first time it takes around 20 seconds for the "Confirmation" screen to appear. In other words the Busy/Waiting screen stays for around 20 seconds. But if I use this feature again (without exiting or closing the emulator)I get the confirmation screen in less than 3 seconds. Why is this so ? Maybe the networking thread is still running and network connections have not been closed. This is why it takes less time if I use the app from the second time. But it takes lot of time when I use it for the first time.
D) If I do all the work (network communication, etc...) in main thread, I get the confirmation screen in less that 5 seconds. But I can't have a Busy screen. On the other hand if I use child thread for networking and get a progress screen in main thread then it takes around 20 seconds for the confirmation screen to appear. Since response time is critical so using the main thread do all work seems a better approach (ofcourse i won't get the Busy screen). ANy comments ? Regards Danish [ July 26, 2003: Message edited by: Danish Shaukat ] [ July 26, 2003: Message edited by: Danish Shaukat ]
A. The worker thread should update the view. Or, it must implement a Observer/Listener pair to notify the main thread. B. I do not understand the question. Both thread hold references to the value objects. They are inside the same JVM. I do not understand why you need sync. C. Make sure that you close all connects after you use them. The slow start up time might be caused by setting up the classes and connection infrastructure inside the JVM. I am not sure. It sounds odd through. D. You must do network and other blocking methods in a separate thread. You might get away by doing it in the main thread for MIDP 1 devices. But for MIDP 2 devices, it will often causes lock-up since another UI thread is used for permission management.