NotesThread (Java)

The NotesThread class extends java.lang.Thread to include special initialization and termination code for Notes/Domino. This extension to Thread is required to run Java programs that make local calls to the Notes/Domino classes. It is not necessary for remote calls. An application that makes both local and remote calls can determine dynamically when to use the static methods sinitThread and stermThread. Remote calls can be made while a local thread is running; however, do not use an object obtained through one session in a call to the other session. Applets that do not start threads and agents that extend AppletBase and AgentBase do not need to code for local versus remote calls, as this capability is provided in the base code.

Executing threads through inheritance

To execute threads through inheritance, extend NotesThread instead of Thread and include a runNotes method instead of run:

public class myClass extends NotesThread {
    public void runNotes() {
        // my code
    }
}

Start a Domino® thread in the same way as any other thread:

myClass m = new myClass();
m.start();

Executing threads through the Runnable interface

To execute threads through the Runnable interface, implement Runnable and include a run method as you would for any class using threads:

public class myClass implements Runnable {
    public void run() {
        // my code
    }
}

You might include both run and runNotes methods so that the class works whether you specify "implements Runnable" or "extends NotesThread":

public class myClass implements Runnable {
    public void run() {
        this.runNotes();
    }
    public void runNotes() {
        // my code
    }
}

Executing threads through the static methods

To execute threads through the static methods, call sinitThread() to initialize a thread and stermThread() to terminate the thread. Call stermThread() exactly one time for each call to sinitThread(); putting stermThread in a finally block is recommended.

public class myClass
{
    public static void main(String argv[])
    {
        try
        {
            NotesThread.sinitThread();
            // my code
        }
        //my code
        finally
        {
            NotesThread.stermThread();
        }
    }
}

Multithreading issues

You should avoid multithreading unless you have good reason to use it, such as proceeding while file input/output and Web requests are processing. Observe the following guidelines:

  • Within a session, Domino® Objects are shared, synchronized, and recycled across threads. Using a different session on each thread loses these capabilities; you must explicitly manage synchronization and recycle on a per-thread basis.
  • Do not use DbDirectory across threads.
  • Accessing an existing document on multiple threads is permissible, but accessing it on just one thread simplifies memory management. Restricting accesses to one thread allows you to recycle without checking the other threads. Creating documents across threads is always safe and these objects can be recycled without reference to the other threads.
  • Profile documents are cached on a per-thread basis. In the event of an update contention, the last thread updating wins.
  • Take care not to delete a document needed for navigation by a view or collection on another thread.
  • When child objects are used on threads other than the parent, keep the parent thread alive until all child threads terminate. This is particularly important when using Domino® Objects in AWT event handlers.

Specification of NotesThread

The specification of the class NotesThread is as follows:

public class NotesThread extends java.lang.Thread {
    public NotesThread();
    public NotesThread(Runnable t);
    public NotesThread(String name);
    public NotesThread(Runnable t, String name);
    public NotesThread(ThreadGroup group, String name);
    public NotesThread(ThreadGroup group, Runnable t, String name);
    public NotesThread(ThreadGroup group, Runnable t);
    public void initThread();
    public void termThread();
    public static void sinitThread();
    public static void stermThread();
    public final void run();
    public void runNotes() throws NotesException;
    public void finalize();
    public static void load(boolean debug) throws NotesException;
}

Example