ObjectBox Production Release

Martin Klang <[email protected]> Tue, 20 Apr 2004 19:38:59 +0100
Newsgroups gmane.text.xml.o-xml
Message-ID <[email protected]>
Good news - ObjectBox 1.0.0 final is scheduled to be released on May 1.
This date also marks the second anniversary of o:XML, yet another 
reason to celebrate!

A pre-release is available now, 1.0.0 release candidate 2.
As with the previous release candidate there is multi-thread support, 
now with a handy shorthand for easily creating threaded applications - 
simply wrap them in an <o:thread> element!

For those of you who want to know more about threads in o:XML I've 
attached below the upcoming new chapter from the programming guide.

Those who want to download rc2 and get going, head straight to the 
download site ->
http://download.pingdynasty.com/o-xml/ObjectBox/ObjectBox-1.0.0rc2/

As usual, obox.jar contains all the relevant class files and replaces 
your current obox.jar.
objectbox.jar is a self-contained executable jar, ie you can run it 
from the command line with
java -jar objectbox.jar [options] [file]


comments, feedback, bug reports, feature requests are appreciated as 
always -
enjoy!

/m

Martin Klang
o:XML - object-oriented XML




Chapter 13. Threads

Computer programs are fundamentally based on instructions, generally 
written as a sequence of commands. This reflects how the machine 
executes the instructions, one after the other. However it is often 
necessary for an application to perform several tasks at the same time. 
To represent concurrent execution paths we use threads. Each 
application thread can run in parallel with every other.

To make things easy, o:XML provides a Thread type. Threads can be 
started and put to sleep, or made to wait for other threads. They can 
be given higher or lower priority, depending on how important or 
time-consuming they are.

To make things easier still, you can create and use threads without 
even using types. The thread element tells the machine to run all 
instructions inside the element concurrently with the current thread of 
execution.

Example 13.1. Threads Made Easy


   <o:thread>
     ... concurrent code ...
   </o:thread>



What the thread element actually does is to create a new Thread object, 
start it, and carry on with what it was doing. The output of executing 
thread is that object, which can be captured and further manipulated.

Example 13.2. Asynchronous Functions


   <o:function name="fire-and-forget">
     <o:do>
       <o:thread>
         ... do something ...
       </o:thread>
     </o:do>
   </o:function>

   <o:set thread="fire-and-forget()"/>

   ... do something else ...

In the above example, calling fire-and-forget will immediately return a 
Thread object. While not very useful in itself, the function body is 
executed while the current thread carries on.

As you know by now, each thread represents a sequence of instructions 
that are executed when the thread runs. With o:XML Thread objects, 
these instructions reside in the Thread.run() function. The Thread type 
itself has a run function that doesn't do anything. When you use the 
element thread, a Thread object is created that knows to execute the 
instructions inside the element when its run() function is called. 
Another way to implement concurrent behaviour is to create a type with 
Thread as parent type, and declare a run() function that takes care of 
business. Objects of this type can then easily be created and 
controlled using the usual Thread functions.

Example 13.3. Thread Types


   <o:type name="MyThread">
     <o:parent name="Thread"/>

     <o:function name="run">
       <o:do>
         <o:while test="true()">
           <o:do select="$this.sleep(1000)"/>
           <o:log msg="i sleep all day!"/>
         </o:while>
       </o:do>
     </o:function>
   </o:type>



Now for another example of asynchronous functions: Consider that we 
want to collect some information from our servers, but we don't want to 
wait with what we're currently doing to get the results. Instead we 
create a thread that collects the data for us. Subsequently calling the 
function return() causes the program to wait for the thread to finish, 
and then return its output, or return value.

Example 13.4. Thread Results


   <o:function name="collect">
     <o:do>
       <o:thread>
         <o:eval select="collect('server-1')"/>
         <o:eval select="collect('server-2')"/>
         <o:eval select="collect('server-3')"/>
       </o:thread>
     </o:do>
   </o:function>

   <o:set thread="collect()"/>

   ... do something else ...

   <o:set result="$thread.return()"/>


At the beginning of this chapter we made mention of thread priorities. 
You can set or get the priority of a thread by calling its 
priority(Number) or priority() function respectively. Meaningful 
priorities are in the range from 1 to 10, with 1 representing the 
lowest precedence and 10 the highest.

The purpose of some threads is to work in the background, performing 
maintenance tasks or provide services to the application. These threads 
are called daemon threads, and they operate more or less independently 
of the main program. Daemons are automatically terminated when all 
other threads have finished, to avoid the program running indefinitely. 
To mark a thread as daemon, or determine its daemon status, there are 
the functions daemon(Boolean) and daemon().

When creating threads using the thread element, both the priority and 
daemon status can be set with attributes.

Example 13.5. Thread Control Made Easy


   <o:thread priority="1" daemon="true()">
     ... slow code ...
   </o:thread>


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

_______________________________________________
o-xml mailing list
o-xml-zRfLyl9bSvv/[email protected]
http://lists.pingdynasty.com/mailman/listinfo/o-xml