• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

chat program!

 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

--Mark
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic