Multithreaded programs in Java

The Java runtime environment is inherently multithreaded. WebSphere® MQ classes for Java allows a queue manager object to be shared by multiple threads but ensures that all access to the target queue manager is synchronized.

Multithreaded programs are hard to avoid in Java . Consider a simple program that connects to a queue manager and opens a queue at startup. The program displays a single button on the screen. When a user clicks that button, the program fetches a message from the queue.

The Java runtime environment is inherently multithreaded. Therefore, your application initialization occurs in one thread, and the code that executes in response to the button press executes in a separate thread (the user interface thread).

With the C based WebSphere MQ MQI client, this would cause a problem, because there are limitations to the sharing of handles by multiple threads. WebSphere MQ classes for Java relaxes this constraint, allowing a queue manager object (and its associated queue, topic and process objects) to be shared by multiple threads.

The implementation of WebSphere MQ classes for Java ensures that, for a particular connection (MQQueueManager object instance), all access to the target WebSphere MQ queue manager is synchronized. A thread that wants to issue a call to a queue manager is blocked until all other calls in progress for that connection are complete. If you require simultaneous access to the same queue manager from multiple threads within your program, create a new MQQueueManager object for each thread that requires concurrent access. (This is equivalent to issuing a separate MQCONN call for each thread.)
Note: Instances of the class com.ibm.mq.MQGetMessageOptions must not be shared between threads which are requesting messages concurrently. Instances of this class are updated with data during the corresponding MQGET request, which can result in unexpected consequences when multiple threads are operating concurrently on the same instance of the object.