Results 1 to 2 of 2
  1. #1
    adammyth is offline Member
    Join Date
    Dec 2009
    Rep Power

    Exclamation 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?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    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.

Similar Threads

  1. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  2. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM
  3. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 PM
  4. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 05:27 AM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts