Are OWLAPI APIs thread-safe? - multithreading

I am interested to know whether APIs of OWLAPI are thread-safe. I am using OWLAPI version 5.1.10 in my project to interact with OWL ontology. I am planning to use OWLAPI functionalities like:
addAxiom(OWLOntology, OWLAxiom) method of OWLOntologyManager
getOWLObjectProperty() method of OWLDataFactory
applyChange() method of OWLOntologyManager
from multiple threads, however I am not sure if the changes to ontology will be consistent in this case. Any help would be appreciated.

OWLDataFactory is thread safe.
OWLOntologyManager must be created with OWLManager.createConcurrentOWLOntologyManager() to be thread safe.

Related

Are Server-Side JavaScript objects thread-safe and/or synchronized?

Since I haven't found any documentation of IBM's com.ibm.jscript.std.ObjectObject or com.ibm.jscript.std.ArrayObject I wanted to ask if they are thread-safe and/or synchronized, i.e. if I can safely use them in a multi-thread environment without getting problems with ConcurrentModificationExceptions etc.
PS: I know I could for example replace these objects with Maps and Lists from java.util.concurrent, but this question does not aim at finding a workaround.

Security of scala runtime

I'm developer of Robocode engine. We would like to make Robocode
multilingual and Scala seems to be good match. We have Scala plugin prototype here.
The problem:
Because users are creative programmers, they may try to win battle
different ways. As well robots are downloaded from online database
where anyone could upload one. So gap in security may lead to security
hole into users computer. Robots written in Java are running in
restricted sandbox. Almost everything is prohibited [network, GUI,
disk (limited), threads (limited), classloaders and reflection]. The
sandbox is similar to browser applet. We use SecurityManager, custom
ClassLoader per robot, etc ...
There are two ways how to host Scala runtime in Robocode:
1) load it together with robot inside of sandbox. Pretty safe for us,
preferred solution. But it will damage Scala runtime abilities because runtime uses reflection. Maybe generates classes at runtime ? Use threads to do some internal cleanup ? Access to JVM/internals ? (I would not like to limit abilities of language)
2) use Scala runtime as trusted code, outside the box, security on
same level as JDK. Visibility to (malicious)
robot. Are the Scala runtime APIs safe ? Do methods they have security
guards ? Is there any safe mode ? Is there any singleton in Scala runtime,
which could be abused to communicate between robots ? Any concurency/threadpool/messaging which could simulate threads ? (Is there any security audit for Scala runtime?)
3) Something in between, some classes of runtime in and some out. Which classes/packages must be visible to robot/which are just private implementation ? (this seems to be future solution)
The question:
Is it possible to enumerate and isolate the parts of runtime which must run in
trusted scope from the rest ? Specific packages and classes ? Or better idea ?
I'm looking for specific answer, which will lead to secure solution. Random thoughts welcome, but not awarded. There is ongoing discussion at scala email group. No specific answer yet.
I think #1 is your best bet and even that is a moving target. As brought up on the mailing list, structural types use reflection. I don't think structural types are common in the standard library, but I don't think anyone keeps track of where they are.
There's also always the possibility that there are other features using reflection behind the scenes. For example, for a while in the 2.8 branch some array functionality was using reflection. I think that's been changed after benchmarking, but there's always the possibility that there's some problem where someone said "Aha! I will use reflection to solve this."
The Scala standard library is filled with singletons. Most of them are immutable, but I know that the Scheduler object in the actors library could be abused for communication because it is essentially a proxy for an actual scheduler so you can plug your own custom scheduler into it.
At this time I don't think Scala requires using a custom class loader and all of its classes are produced at compile time instead of runtime, but then again that's probably a moving target. Scala generates a lot of class files, and there is always talk of making it generate some of them at runtime when they are needed instead of at compile time.
So, in short, I do not think it's possible (within reasonable constraints on effort) to enumerate and isolate the pieces of Scala that can (and should) be trusted.
As you mentioned other J* language implementations which all may make use of reflections, it would be a ban for all those languages as long as reflection is not part of the game.
I guess that would be JVM's problem not to have a way to partition the scope of reflection API, such that you could sort of "sandbox" the part of code that could be reflected within.

How to create a thread in a class?

Is there a template that can be used to create threads when we program in
OO language ?
How to go about designing a threading package for an OO language?
Support
C++0x will support threads in the standard library.
As of now, each platform has its own way of implementing threads (Windows, POSIX) but you can use something such as boost::thread to not have to worry about platform-specific stuff.
In Java, there is a Thread class.
Methods
In general, to put a class into another thread, you will create a thread while passing that class into the thread. Then the thread will call a function in that class. Here is some pseudo-C++-code:
main()
{
Object myObject;
thread = CreateThread(threadFunction, myObject);
thread.join(); // wait for thread
}
threadFunction(Object theObject)
{
theObject.doSomething();
}
This is all simplified by the use of boost (or C++0x threads) in C++, and the Thread class in Java handles this for you.
Related Information
A large problem in threaded applications is synchronization of threads. This includes problems like race conditions and deadlocks, to name a couple.
Methods/object exist to help these problems, such as a mutex. A mutex can be locked by one thread, and any other threads that try to lock the mutex will be blocked until the original thread releases the mutex.
A semaphore is a generalized mutex.
There are other useful concepts as outlined in Eric's post.
Each framework has its own way of dealing with threads. I suggest you look up the java thread class in the the java documentation, and then perhaps look at the C++ standard headers for thread details.
Each programming language has specific ways of handling threads.
C++ relies a lot on Boost so you might wanna check that
Java
Basically everything you learn about concurrency should apply whatever the OS or language you're using. There are roughly 4 problems you must learn to avoid
Deadlocks
Livelocks
Race Conditions
Hunger
This isn't directly related to your questions but these are subjects you should learn in parallel to learning a particular syntax in a given language. Java is of course quite easy while C++ might be a little trickier, your pick
There are also a number of "well known methods" for synchronizing threads such as
Events
Locks
Monitor
Mutex
Semaphore
Barrier
...
this list goes on and on, but are basically helping "objects" or variables that will help you solve the 4 problems mentionned
In C/C++, threads are often implemented using functions. ("Start a new thread and run this function inside, destroys the thread when the function ends.")
Thread frameworks often allow to pass parameters to the function. One way of doing OO with threads is to pass the object pointer ("this") as the function parameters and then call a specific method on the object.
If you are using MFC then MFC provides some framework helper APIs to work with multi threading. Have a look at this article: Multithreading with C++ and MFC
In Java you can use the Thread class or the Executor interface, to which you can send a runnable object. Check out the java.util.concurrent package. And the concurrency trail of the Java Tutorial. Good Luck.
The main difference when it comes to OOP and multi-threading programming is that you would like your new thread to have as its start-up function a (non-static) method of an object.
Now there are a few programming models for this: implement a "Runnable" interface is the most common used one. Your class implements a standard "Runnable" interface that has a start-up method which is usually a virtual method called "run". You put your start-up code in that virtual method and when you need to spawn a thread you create a thread object (usually called Thread) and you pass to it an object of your class that implements the "Runnable" interface. Then you call the "start" method of that "Thread" object and that will take care that the start-up code you have written in your class will be run a a new thread. In this example both "Runnable" and "Thread" are provided by the chosen threading library or by the standard library/framework of the programming language of your choice. Also please note that the names of these two classes can vary.
There is a big difference between C++ and Java when it comes to multi-threading programming: C++ has not support for multi-threading as opposed to Java which has built-in support for that.
So for C++ you will need to use a specialized threading library such as Pthreads (http://en.wikipedia.org/wiki/POSIX_Threads) or more general purpose libraries such as Boost, ACE, POCO which already have support for threading. The least recommended way is to call directly the multi-threading related services of the OS.
For Java, no matter what edition you are using you will already have the support for multi-threading programming built-in.

What are all the way that you try to make your code functional like?

so that you can make your program concurrent easily in the future.
I focus on making items Immutable. Immutable objects allow you to reason about multi-threaded code a lot easier than "thread safe" objects. The object has one visible state that can be passed between threads without any synchronization. It takes the thought out of multi-threaded programming.
If you're interested, I've published a lot of my work with immutable objects, in particular immutable collections on code gallery. The name of the project is RantPack. In the collection area I have
ImmutableCollection<T>
ImmutableMap<TKey,TValue>
ImmutableAvlTree<T>
ImmutableLinkedList<T>
ImmutableArray<T>
ImmutableStack<T>
ImmutableQueue<T>
There is an additional shim layer which (CollectionUtility) which will produce wrapper objects that implement BCL interfaces such as IList<T> and ICollection<T>. They can't fully implement the interfaces since they are immutable but all possible methods are implemented.
The source code (C#) including the unit testing is also available on the site.
I program mainly in Java. I'm waiting patiently for the day where closures will be added to the language. But as I am still stuck on Java 1.4.2, even if they get added, that's not going to be for me for a long time !
That said, my main "functional" way of programming is making a lot of use of the "final" keyword. I try to have as many classes as possible completely immutable, and for the rest to have a clear distinction between what's transient and what's immutable.
Don't use member variables or global variables. Use the local stack of functions/methods. When a method uses only internally scoped variables and call parameters and returns all information using out/inout/reference parameters or return values, it is functional.
Make everything asynchronic.
Use immutable objects, messages, etc.
Communicate via queues.
Here's a talk on rubyconf 2008 about the subject, it's mostly ruby centered, but several concepts remain valid.
http://rubyconf2008.confreaks.com/better-ruby-through-functional-programming-2.html

Cross thread communication in Delphi

Is there any documentation on cross thread communication in Delphi? How can I send message to the thread that doesn't have a window?
You can only send (Windows) messages to threads that implement a standard message loop, which will automatically be created once a window handle is realized.
It is however not necessary to use messages to communicate with a thread. Just let it wait on an event object (TEvent in VCL), and signal this event when you want the thread to perform a function.
But if you are new to multi-threading - don't go into all these details on your own, unless you want to for the learning effect. Just use the OmniThreadLibrary and be done with it. There's much good to be learned by digging into its internals, once you know how to use it.
Edit:
See also the answers to this question which is very similar.
Edit 2:
Regarding the comment asking "What does [OmniThreadLibrary] make easier, and at what cost?" I can only advise you to check it out for yourself - that is if you are using at least Delphi 2007. There are several samples to illustrate the concepts, but for a quick "real-life" example you could have a look at this blog post - you don't even need to install the library for that.
I do also agree that using a library for multi-threading does require a certain act of faith. OTOH making do with what the VCL provides is hardly an alternative. The sample code does still use the ill-conceived Synchronize() call. There is no support for things like thread-safe producer-consumer-queues, which are much more suited to multi-threaded programming. And if you do agree that you need a more solid fundament for your multi-threaded programs than the VCL provides - why reinvent that particular wheel?
As for the cost of using the library: You will have to time yourself whether it is fast enough for you. It does abstract the communication between threads in a good way IMHO, but every abstraction costs performance, obviously.
If you decide that it is not for you after all - write the code yourself. I did the same for Delphi 4, and I have been using that code for nearly 10 years now. And judging by the amount of bugs I found and corner cases I experienced in that time, I would definitely advise anybody new to multi-threading to not write their own library code for it. And if you really really want to, please take the rules in this posting to heart.
The question Delphi Multi-Threading Message Loop also contains a few examples of communication between threads
If you have a reference to the thread object, you can just call it direct, and have the procedure store information or update accordingly. Obviously you have to be careful to do things in a thread safe manner.
Alternatively, you could use a central control object through which the threads communicate when they aren't busy. I have an app where threads have particular purposes, and are allocated a thread-ID. Any thread can "post" a message with a message-ID and a string for parameters to another thread-ID and then get on with its work. The other thread the picks it up at its leisure, and acts accordingly.

Resources