ok, here is my problem people and i would appreciate any help. i am making a chat program using regular sockets and streams to pass the data. im having a database that holds the name and passwords of the users, and a hashMap in the server that holds the users who are online. Whenever a user logs in or logs off, his name is added to the HashMap or is removed from it. The methods from adding to the HashMap or removed arent synchronzied right now so i thought this can cause problems if 2 users will log in and out in the same time (or even 2 will log on together). now, i want to synchronize them but i have a question: say 1 user logs in and another logs off at the same time. both send a message to the server to update their status. however if one managed to get the lock of the server will the other message wait till thread is finished or will it just skip the method all together. to clarify it will both message get executed or only one??
Well, when I did this, we had a thread pool to handle incoming requests. Each user would send a message to the server, say for logging in. When the message reached the server, a thread would pick it up and porcess it. Now, as you noted, both threads will have code which try to access the HashMap. One of the threads will get the lock for it first. The other thread, when it reaches the block of syncrhonized code, will not be able to execute, until the first thread completes the block of synchronized code and releases the lock.
thanks Mark for answering, but i still have a qeustion: i have a singelton class which is the server. the server spawns threads that deal with the messaging with the clients. every client has its own server thread to talk to. when a client sends a message , the thread recieves it and activates a function in the singelton class to update the HashMap. my question is : the activation of the method will happen only once - what if during that one time the method that accesses the HashMap was blocked? will the method like hang in memory till it can exceute or will it not?