how to temporary disable parallel working threads

Discussion in 'Java' started by Thorsten, Apr 23, 2004.

  1. Thorsten

    Thorsten Guest

    Hi,

    I have a lot of threads working parallel at my program. For time
    measurements of some passages of the threads I take same timestamps on
    an easy way like:

    long t1 = System.currentTimeMillis();
    System.out.println("something to do");
    long t2 = System.currentTimeMillis();
    System.out.println(t2-t1);

    But there is a fault: assume I have 100 threads. One thread has just
    printed "something to do" and the java vm "decides" to let all other
    99 threads run, which takes 10 sec. Now t2 is calculated and has a
    value which is 10 sec to high.

    Is there a way to say for a thread that some code has to be executed
    non-parallel? I think of something like:

    atomic {
    long t1 = System.currentTimeMillis();
    System.out.println("something to do");
    long t2 = System.currentTimeMillis();
    System.out.println(t2-t1);
    }

    Does anyone have a hint ?

    Regrads
    Thorsten
     
    Thorsten, Apr 23, 2004
    #1
    1. Advertising

  2. On 23 Apr 2004 09:50:06 -0700, Thorsten wrote:
    > Is there a way to say for a thread that some code has to be executed
    > non-parallel? I think of something like:
    >
    > atomic {
    > long t1 = System.currentTimeMillis();
    > System.out.println("something to do");
    > long t2 = System.currentTimeMillis();
    > System.out.println(t2-t1);
    > }
    >
    > Does anyone have a hint ?


    Synchronize the critical section on a common object:

    synchronized (foo) {
    long t1 = System.currentTimeMillis();
    System.out.println("something to do");
    long t2 = System.currentTimeMillis();
    System.out.println(t2-t1);
    }

    foo in the example can be any valid object reference, but it must
    refer to the *same* object in each of the threads that should be
    synchronized. The operation will be mutually atomic among only the
    threads sharing the same synchronization object.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 23, 2004
    #2
    1. Advertising

  3. Thorsten

    Thorsten Guest

    Hi Gordon,

    thanks a lot, this is a very good hint and it almost works if I get
    you right. But the synchronized (foo) {} sections are still
    interrupted by other threads if theses other threads are outside their
    own synchronized (foo) {} section.

    Regards
    Thorsten
     
    Thorsten, Apr 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    3,224
    James Kanze
    Feb 12, 2008
  2. Soren
    Replies:
    4
    Views:
    1,270
    c d saunter
    Feb 14, 2008
  3. aiwarrior

    Threads and temporary files

    aiwarrior, Mar 13, 2009, in forum: Python
    Replies:
    3
    Views:
    225
    aiwarrior
    Mar 14, 2009
  4. Vivek Menon
    Replies:
    5
    Views:
    3,361
    Paul Uiterlinden
    Jun 8, 2011
  5. Vivek Menon
    Replies:
    0
    Views:
    1,774
    Vivek Menon
    Jun 10, 2011
Loading...

Share This Page