0

here is my code of main class, because only thread 3 will change the shutdown and main will not read return until thread 3 close so there no need to synchronize it. When I try to run it, the "shutdown" is printed but eclipse say that the program is still running

static Boolean shutdown = false;

    public static void main(String[] args) throws InterruptedException, IOException{
        System.out.println("Start Server");
        final int SocketListSize = 100000;
        final int AccListSize = 1000000;
        ServerSocket serverSocket;


        List<Socket> socketList = Collections.synchronizedList(new ArrayList<Socket>(SocketListSize));
        List<Request> requestList = Collections.synchronizedList(new ArrayList<Request>(SocketListSize));
        ArrayList<BankAccount> bankAccList = new ArrayList<BankAccount>(AccListSize);

        serverSocket = new ServerSocket(1234);
        serverSocket.setSoTimeout(50000);

        Thread Thread_1 = new Thread(new scanSocketThread(serverSocket, socketList));
        Thread Thread_2 = new Thread(new getRequestThread(socketList, requestList));
        Thread Thread_3 = new Thread(new ServiceThread(requestList, bankAccList));
        Thread_1.start();
        System.out.println("thread 1 start");
        Thread_2.start();
        System.out.println("thread 2 start");
        Thread_3.start();
        System.out.println("thread 3 start");

        Thread_3.join();
        System.out.println("thread 3 close");

        Thread_1.interrupt();
        System.out.println("thread 1 close");
        Thread_2.interrupt();
        System.out.println("thread 2 close");

        if(shutdown == true){
            System.out.println("shutdown");
            return;
        }

Here is what I get

thread 3 close
thread 1 close
thread 2 close
shutdown
aukxn
  • 231
  • 1
  • 4
  • 8
  • 1
    The main thread will have ended. But one of the others hasn't. The main thread is just one arbitrary thread that happens to be the first. – zapl Jun 08 '16 at 16:36
  • 1
    do you know that your threads actually respond to interruption? if they're blocked on a network read they may not. – Nathan Hughes Jun 08 '16 at 16:40
  • As pointed by Nathan, you're not assuring that thread 1 and 2 have finished just by invoking interrupt http://stackoverflow.com/questions/3590000/what-does-java-lang-thread-interrupt-do – RubioRic Jun 08 '16 at 16:57

1 Answers1

0

If you're looking to kill your program, you can use System.exit(0); instead of the return statement you have now.

More: What are the differences between calling System.exit(0) and Thread.currentThread().interrupt() in the main thread of a Java program?

Community
  • 1
  • 1
lucasvw
  • 1,345
  • 17
  • 36