Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 82
Registered: ‎12-16-2010
My Device: BlackBerry Storm 9950

Can't wrap my head around wait() and notify()

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.

 

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can't wrap my head around wait() and notify()

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 82
Registered: ‎12-16-2010
My Device: BlackBerry Storm 9950

Re: Can't wrap my head around wait() and notify()

So this wouldn't work

 

 

pFolder.wait();

Folders.run();

pFolder.notify();

 

 

 

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can't wrap my head around wait() and notify()

no, it makes no sense

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 82
Registered: ‎12-16-2010
My Device: BlackBerry Storm 9950

Re: Can't wrap my head around wait() and notify()

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?

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can't wrap my head around wait() and notify()

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

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Highlighted
Developer
Posts: 53
Registered: ‎11-13-2009
My Device: Not Specified

Re: Can't wrap my head around wait() and notify()

[ Edited ]

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.

 

 

Developer
Posts: 82
Registered: ‎12-16-2010
My Device: BlackBerry Storm 9950

Re: Can't wrap my head around wait() and notify()

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.

 

 

Developer
Posts: 298
Registered: ‎05-02-2010
My Device: Bold 9700
My Carrier: Verizon

Re: Can't wrap my head around wait() and notify()

http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

 

Please read through Guarded Blocks, I've implememented a similar idea but with JSON data and a SQLite database.

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Can't wrap my head around wait() and notify()

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).

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!