Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:51:08+00:00 2026-06-05T04:51:08+00:00

I have two while conditions, one inside the other, with booleans to control them.

  • 0

I have two while conditions, one inside the other, with booleans to control them. Basically, one is for terminating a share and the other is to listen for a connection. A user can select to disable a share, and in this case, the server stops listening but it won’t terminate. If a user selects to terminate then both booleans are set to false and the cycle ends.

This is my code:

        public void run() {
            while (!terminate) {  
                while (listening) {
                    try {
                        // accept connection -> create a new thread for each client
                        ClientServerShareInstance clientServerShareInstance = new ClientServerShareInstance(serverSocket.accept(), ui);
                        Thread clientServerThread = new Thread(clientServerShareInstance);
                        clientSockets.add(clientServerShareInstance);
                        connectedClients++;
                        clientServerThread.start();
                    } catch (IOException ex) {
                    }
                }
            }
        }

        public void closeAllClientConnections() {
            for (Iterator it = clientSockets.iterator(); it.hasNext();) {
                ClientServerShareInstance clientServerShareInstance = (ClientServerShareInstance) it.next();
                clientServerShareInstance.closeAllConnections();
                it.remove();
            }
            try {
                this.serverSocket.close();
            } catch (IOException ex) {}
            this.setActive(false);
            this.connectedClients = 0;
        }


        public void openConnection() {
            try {
                serverSocket = new ServerSocket(portNumber, 0, Inet4Address.getLocalHost());
                setActive(true);
            } catch (IOException ex) {}
        }
    }

The closeAllClientConnections() method disables a share (does not terminate it), and the openConnection() reenables that share.

The problem is, if I disable a share, it should just loop the terminate while cicle indefinitely, testing the value of listening. When I set listening to true, it should re-enter that second while loop and start listening again, because I do open the server socket (although it’s not related to that, I’m just saying it has to be initialized again because I close it when I disable the share). However, after disabling, it never renters the listening loop, even when openConnection() is called.

Anyone know what’s wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:51:09+00:00Added an answer on June 5, 2026 at 4:51 am

    There’s not enough code shown to show any errors. But here are some comments that might help.

    • Both the shutdown and listening booleans need to be volatile. Any fields shared between threads need to be somehow synchronized otherwise changes to their values will not be seen by other threads.

    • serverSocket will also need to be volatile since it seems to be created by the caller of openConnection() but consumed in the while loop. You might consider just setting active to the true in openConnection() and have serverSocket managed entirely by the accept thread.

    • clientSockets looks to be a collection. That will need to be a synchronized connection since, again, it looks like it is accessed by multiple threads. Again, a better pattern would be for the closeAllClientConnections() call to just set a boolean and the thread itself would do the closing. That removes any race conditions around using the collections, etc..

    • Looks to be that if you are ! terminating that your accept thread will spin. At the very least you should put some Thread.sleep(100) or something to slow it down. A wait/notify would even be better.

    It is important to realize that it is not just about what happens “at the same time” in a threaded program. It is also about memory caching. The accept thread could have added something to the clientSockets ArrayList1 a minute ago and another thread may not see those changes if the list is not synchronized somehow. Even worse is the prospect that certain portions of the ArrayList may have been updated in memory but not others which might cause an exception. To get a synchronized collection, you should create your ArrayList like:

    List<...> clientSockets = Collections.synchronizedList(new ArrayList<...>());
    

    Sounds like you should read some of the documentation about why synchronization is necessary:

    http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two begininer programs, both using the 'while' function, one works correctly, and
I have two table i want to add one condition while using LEFT OUTER
I have two threads, one for data acquisition and the other one for display.
TL;DR version: I have two threads. One of them might need to Interrupt() the
I have two models: Show and Venue . Show has one venue while each
I have two divs: -A header bar, which is fixed while scrolling, and stuck
I have two sliding panels, panel1 is sliding down, while panel2 is sliding from
i have two list, i wanted to update the second list while adding a
I have a query to select from another sub-query select. While the two queries
We have had our glassfish instance go down every two weeks for a while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.