01-27-2011 09:22 AM
I believe wait() and notify() is what I want to use. But I could be wrong. I don't know. I'm not sure. So I will ask.
I am trying to do an update process on an App when it starts up. What I want it to do is to creat a new directory, finish, create another directory within the just-made directory, finish, then download a file to the first directory, and then download a series of files to the second-created directory.
sort of like this
create /file/
create /file/fileinFile/
Download fileX to file
Download files to /file/fileinFile/
Pop a new screen
all at the click of one button on the front screen of the page.
When I do it bare, just like that, the first file gets created but the runtime debugger is all ready trying to create the second file before the other one is done being built. Throwing a runtime exception.
I've been doing a lot of research on the issue and I come to think that I need to do it with Wait()'s and Notify()'s. Even through reading about it, my head is spinning.
I can tell you that when run separately all of the methods im trying to do run successfully.
It's only when I try to get them to run consecutively that I get errors.
I have a code sample of the bare item above:
public class Updater implements Runnable
{
FolderMaker Folders = new FolderMaker("file:///SDCard/file/");
FolderMaker pFolder = new FolderMaker("file;///SDCard/file/fileinFile/");
FileListDownload Filelist = new FileListDownload();
XMLfileDownloader XML = new XMLfileDownloader();
public Updater()
{
this.run();
}
public void run() {
Folders.run();
pFolder.run();
Filelist.run();
XML.run();
UiApplication.getUiApplication().pushScreen(new TestMenuScreen());
}
}
This Class is called from a MainScreen button listener using invokeandwait.
Any sample code to help me do this, or maybe a 3-year old (lol) level explanation would be greatly appreciated.
01-27-2011 10:20 AM
wait blocks the current thread, it should never be used from the event thread.
creating folders and other file operations are I/O and should be done in a separate thread.
you can use a so-called sync-object for wait and notify. it is a complicated topic, but you can use all references available about desktop java to learn more about it.
01-27-2011 11:23 AM
So this wouldn't work
pFolder.wait();
Folders.run();
pFolder.notify();
01-27-2011 11:26 AM
no, it makes no sense
01-27-2011 11:28 AM
Can you think of a simpler way to accomplish what I'm trying to do? (assuming you understand what I want to do)
Can you or anyone else make any other suggestions?
01-27-2011 11:33 AM
creating files or folders are blocking operations, the control flow continues after it has finished.
put it all into a separate thread, i don't see the need for wait/notify
01-27-2011 11:42 AM - edited 01-27-2011 11:43 AM
As Simon stated.
public class Updater implements Runnable
{
public Updater()
{
this.run();
}
public void run()
{
FolderMaker Folders = new FolderMaker();
Folders.makeFolder("file:///SDCard/file/");
Folders.makeFolder("file;///SDCard/file/fileinFile /");
FileListDownload Filelist = new FileListDownload();
XMLfileDownloader XML = new XMLfileDownloader();
}
}
There's no need to create two other FolderMakers threads. Same goes with the FileListDownload and XML fileDownloader.
01-27-2011 12:01 PM
After making the folders, I need to download an XML file to the first file. Then I need to parse the XML file that I downloaded and download information based on that.
I figured out the folders. and have the XML downloaded, but it's skipping over step where it parses the Downloaded XML file and does more downloads off of that.
01-27-2011 12:25 PM
http://download.oracle.com/javase/tutorial/essenti
Please read through Guarded Blocks, I've implememented a similar idea but with JSON data and a SQLite database.
01-27-2011 12:37 PM
In order to achieve what you need, you have to:
1) On the click of a button, create a separate Thread with all blocking operations inside its run() method (don't do anything blocking in the constructor or member initializations - those are executed in the event thread). Start that thread.
2) Also, push a "Please, wait..." screen which will have no user interaction (maybe let the user cancel the whole operation, but I would postpone that until you have the first iteration working).
3) At the very end of your background Thread's run() method, push your new screen (wrap the pushScreen in invokeLater - otherwise it won't work).