Results 1 to 2 of 2
  1. #1
    SarahB is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Sychronizing a part of a code in a method

    In the run() method of a thread, there is a line where the user is asked to enter some values (these values are different from thread to thread according to the user's input).

    I have a problem in running this program, when multiple threads are started, that line is printed for each thread one after the other giving no chance for the user to enter the value for each thread.

    I don't want to add a sleep(), I thought of making a kind of a lock to this part of the code in the method, so that no thread can access this part of the code until the other has finished it's output and input.

    So can you help me implement this ? I've seen codes for synchronized methods, but it's not a method, it's just two lines in the method. Any idea how i can do this ?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    so that no thread can access this part of the code until the other has finished it's output and input
    Study the producer–consumer example on Guarded Blocks.

Similar Threads

  1. What will be output by the following code? using trim method.
    By racewithferrari in forum New To Java
    Replies: 4
    Last Post: 11-17-2009, 09:40 PM
  2. Replies: 5
    Last Post: 10-13-2009, 01:35 PM
  3. Trouble implementing part of code into GUI
    By Flaresplitz in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 07:51 AM
  4. Disabling part of a method
    By juru in forum Advanced Java
    Replies: 10
    Last Post: 10-27-2008, 01:41 AM
  5. Replies: 9
    Last Post: 12-18-2007, 09:02 AM

Tags for this Thread

Posting Permissions

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