Search code examples
javamultithreadingterminate

java threads are null when join() is called


For homework, I am writing a stop() method for a server that managers multiple threads that perform transactions. Stop() is supposed to 1. submit a stop command to a queue of commands and 2. call join() on all the threads so that transactions have a chance to be completed.

However, join() is not working as expected. When I try to call join(), I get a NullPointerException. I modified the server thread to sit and wait a second after I submit stop commands, but that's a poor substitute when I should be using join().

public void stop() throws InterruptedException {
    // TODO Auto-generated method stub

    System.out.println("server stop()");
    for (CommandExecutionThread thread : threads) { 
        System.out.println(threads.length);
    }

    for (CommandExecutionThread thread : threads) { 

        Command e = new CommandStop();
        queue.add(e);

    }   

    for (CommandExecutionThread thread : threads) { 
        if (thread != null) 
        {
            thread.join(); 
        } 
        if (thread == null)
        {
            System.out.println("NULL thread"); //threads are always null at this point 
        }
    }

    for (CommandExecutionThread thread : threads) { 
        System.out.println(threads.length);
    }
    wait(1000);//simulate the wait that I would have had from join() 

}

CommandExecutionThreads poll() commands from the commandQueue, and when it encounters a stop command, it returns:

public void run() {
    while (true) { 
        synchronized (commandQueue) {
            while (commandQueue.isEmpty()) { 
                try {
                    commandQueue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }//end while(commandQueue.isEmpty())
        }//end synchronized(commandQueue)

        Command c; 

        synchronized (commandQueue) { 

            c = commandQueue.poll(); 

            if (c.isStop()==true)
            { 
                System.out.println("\tSTOP");
                return;
            } 
            else 
            { 
                //System.out.println("\tTRANSACTION"); 
            } 

            if (executeCommandInsideMonitor) { 
                try {
                    c.execute(bank);
                } catch (InsufficientFundsException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            commandQueue.notifyAll();
        } // end sync commandQueue 
    } //end while(true)
}//end run() 

I initialize my array of threads earlier when I create the bank server. The threads themselves pull commands off the queue when they become available. For diagnostic purposes, I've put a printout of thread.isAlive() and that works. By the time I'm in the stop method, thread.isAlive() throws a null pointer error :

public class BankServerImpl implements BankServer {
Queue<Command> queue = new LinkedList<Command>(); 
CommandExecutionThread[] threads; 
boolean executeCommandInsideMonitor; 
Bank bank; 

/**
 * Constructor for BankServerImpl, which implements BankServer
 * 
 * @param bank
 * @param serverThreads
 * @param executeCommandInsideMonitor
 */
public BankServerImpl(Bank bank, int serverThreads, boolean executeCommandInsideMonitor) {

    this.bank = bank; 
    this.executeCommandInsideMonitor = executeCommandInsideMonitor;
    threads = new CommandExecutionThread[serverThreads]; 

    for(CommandExecutionThread thread : threads) { 
        thread = new CommandExecutionThread(bank, queue, executeCommandInsideMonitor);
        thread.start(); 
        System.out.println(thread.isAlive());   

    }
}

Solution

  • When you create your threads, you never insert them into the array.

    for(CommandExecutionThread thread : threads) { 
        thread = new CommandExecutionThread(bank, queue, executeCommandInsideMonitor);
        thread.start(); 
        System.out.println(thread.isAlive());   
    }
    

    This code does not cause the array to be updated. You need something like this:

    for (int i = 0; i < serverThreads; i += 1) {
        CommandExecutionThread newThread = new CommandExecutionThread(bank, queue, executeCommandInsideMonitor);
        threads[i] = newThread;
        newThread.start();
        System.out.println(newThread.isAlive()); 
    }