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
  • 221
  • 1
  • 3
  • 8

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,131
  • 17
  • 34