Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Threads and global variable prolems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and global variable prolems" Watch "Threads and global variable prolems" New topic
Author

Threads and global variable prolems

james barrett
Greenhorn

Joined: Mar 07, 2009
Posts: 8
hi all,

After spending some time trying to build a java IM application i have come up against a problem i just cant seem to fix. im new to java Threads and how they interact with global variables and each other. i have done some reading and found i should be using vectors and synchronizing methods, but this hasnt solved the problem.

So far i am able to connect a client to the server, verify the user by means of a login method(s) and send the client the most recent list of all the currently connected users so that the new client can populate its contact list JTree.

The problem im having is i try to update the clients that were connected before the new client connects, by sending them the new list of current users, but reguardless of the server actually seeming to send the correct list (verified by means of several System.out.println"'s the clientt keep their old lists,

I have read that there is often problems with threads and global variables, which i have tried to solve via means of the synchronized method, but no joy. Im totaly out of ideas, any fresh insite would be much appreciated.

Thanks in advance

Server code:-

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

I have read that there is often problems with threads and global variables, which i have tried to solve via means of the synchronized method, but no joy. Im totaly out of ideas, any fresh insite would be much appreciated.


I just see the "synchronized" keyword used on one method -- what global variable are you trying to protect?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
james barrett
Greenhorn

Joined: Mar 07, 2009
Posts: 8
to be honest, i wasnt sure what making that synchronized method would do, im trying to make sure that that global variable "currentUsers" is the same between all threads. but as things stand, the clients arnt getting updated currentUsers lists. they just seem to keep getting the old lists that were valid when the client connects.

i have spent the day putting together a much simpler demonstration of the problem i am facing, im happy to post the complete code for it here if you think that will help you to understand what the problem is that im getting.

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40



Simply put, synchronization doesn't protect anything. Synchronization just means that a thread needs to own a lock before it is allowed to do anything. So, if threads make sure that they own the lock, before executing code that have race-conditions, they can "protect" variables from corruption.

IOWs, synchronization is something very simple that can be used for something very complex. You have to understand how it is to be used to understand it. You can't just add a synchronized keyword here and there and see if it works.

Anyway, if you haven't already done so.... The Sun tutorial on threads is a good place to start.

http://java.sun.com/docs/books/tutorial/essential/concurrency/

Henry
james barrett
Greenhorn

Joined: Mar 07, 2009
Posts: 8

Anyway, if you haven't already done so.... The Sun tutorial on threads is a good place to start.


thanks for the link, i have already taken a look at it and some others that i have found, and think i understand how to use serialization properly now.

i tried to build a much simpler application just to test out if i could get a new lest sent to all connected clients. This code is below to illustrate the issue im having.

i am no longer certain where the fault lie, with the client code or with the server, or if im still not understanding threads properly. im geting quite confused


thanks for any help


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and global variable prolems