Question

Sorry for another vague example...but I have a single class where I'm starting a new thread instance. However, if I add a new thread instance, it interrupts (destroys?) the first.

But, if I run two instances of the class (separately, after I turn them into jar files) where each instance only opens up a single thread, they both run concurrently and fine.

I'm convinced the error is the way I'm implementing multi-threading.

Any suggestions for things to look for? Thanks! Sorry for the vague example.

Was it helpful?

Solution

You cannot assume that an arbitrary class is thread-safe.

Authors of a class should be explicit about the thread-safety of their classes, but it's very common that they do not. Given that environments such as Servlets may be intrinsically mulit-threaded this can be a real problem.

You need to study the class and discover which, if any, methods are thread safe. It is possible that the class InstanceOfClassIDontControl has static variables that are getting confused by multithreaded access. If you not only don't control the class, but can't even see its source then you are going to need the owners support.

OTHER TIPS

Ok, here's an example:

public class driver {

    public static void main(String args[])
    {
        Thread x;
        Thread y;

        x = new Thread(new pow());
        y = new Thread(new pow());

        x.start();
        y.start();  
    }
}

public class pow extends Thread {

    public void run() {
        InstanceOfClassIDontControl a = new InstanceOfClassIDontControl();
                a.doVariousProcesses();
    }
}

In the example, I (obviously) don't control the class whose instance is created and called in the thread. Each thread may run for minutes. But whenever a concurrent thread is ran (in this case, with y.start()), it destroys the actions of the object called in the run() instance of x.start().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top