Results 1 to 2 of 2
Thread: passing parameter to a thread
- 12-30-2009, 09:50 PM #1Member
- Join Date
- Dec 2009
- Rep Power
passing parameter to a thread
I am trying to write a simple multithreaded server-client application.
Application logic is as follows:
server opens a socket for each client.
server starts a thread for each client.
for each client in a loop
-server sleeps arbitrary time (random duration)
-after sleeping server sends an arbitrary message(random message)
If i want to start a thread whenever server sends a message to client there is no problem.
But what i want to do is creating 1 thread per 1 socket (client) (not per 1 message)
and send arbitrary message using this thread.
The problem is i am not able to pass my arbitrary message to a thread which has already started.
what might be the solution?
- 01-02-2010, 08:58 PM #2
You don't want to pass a parameter to the thread. You want to update an attribute of the Runnable that the thread is executing. The best way to do that is declare an instance attribute in the Runnable class that is marked as "volatile". This eliminates synchronization issues. Create a setter for the attribute. In the run() method, loop with a Thread.sleep(). Check to see if the attribute has been updated. One way to do this is to check to see if the attribute is null.
- By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 06:46 AM
- By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM
- By hisouka in forum Java ServletReplies: 1Last Post: 09-06-2008, 12:45 PM
- By javanewbie83 in forum New To JavaReplies: 16Last Post: 07-16-2008, 05:27 AM
- By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 01:44 PM