Results 1 to 6 of 6
  1. #1
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Thread at wait() state , shared data (volatile? confused ;/ )

    i have a thread that has just called notifyAll(); wait();
    is it possible to find out on what method was the wait() executed?

    e.g.
    threads a,b,c

    1 thread b has 2 synchronized methods.
    2 thread a calls one of the two methods of b
    3 thread c gets notified from thread b
    4 thread c must be able to know what method did call the notify

    constraints: i am trying to avoid having a silly variable in thread b ( eg String method = "method 1")

    btw something irrelevant, don't you think is a bit strange creating a new thread to ask about threads?:D

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    other than interrupting (killing) the waiting thread to get a stack trace, or before doing the notify throw/catch an exception to print the stack trace - but this only fetches it for a log out put to be read, not communicates it to the other thread to see it.

    i think you would need to create a threadsafe state variable some place that could be read (e.g. a stack structure that you maintain with method entry and exit) - bonus points if you use aspect oriented programming patterns to apply this as advice to methods in your threads. at least that way it would not be a variable in thread b. but still a variable some place.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sort of sounds like you're trying to implement the observer pattern indirectly. Maybe there's a good reason you're trying to do this, but if not, you might want to read up on the observer pattern wiki.

  4. #4
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    thank you for your replies , those were the ways i tried to avoid, i'll stick to the observer, the point is that thread b is compiled after the program is run and its a bit of a mess. It seems i'll have to do it with observer or an ungly static var :D

  5. #5
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    here it is
    int x = new Exception().getStackTrace()[0].getLineNumber();

    my point was to track what line of code is the thread stoped at i thought there would be a way that the thread could tell. i know i wansn't too accurate, i try my posts to be a bit generic to help other users too

  6. #6
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    better way, the first element found, is the line that caused interrupted exception
    Java Code:
    StackTraceElement[] elements = myThread.getStackTrace();
    for(StackTraceElement ele:elements)
    {
          if(ele.toString().contains(className))
    		System.out.println(ele.getLineNumber()); // first element matching here
    }

Similar Threads

  1. making a thread wait
    By imorio in forum New To Java
    Replies: 5
    Last Post: 11-16-2010, 06:25 PM
  2. Thread wait for another thread
    By jitheshmenon in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-05-2010, 02:13 PM
  3. Thread Wait
    By jiexx in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 05:26 PM
  4. use of volatile
    By venkatallu in forum New To Java
    Replies: 3
    Last Post: 09-23-2008, 09:59 PM
  5. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM

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
  •