• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to "control" the sequence of two threads ?

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically I want to simulate two threads to operate on the same object. Suppose these two threads will have "task 1" and "task 2" operations on this single object. How can I "control" these two threads so that they operate on this object in the following sequence --

1. thread 2 performs task 1; after it is done --
2. while thread 1 performs task 1; thread 2 performs task 2; after that --
3. thread 1 performs task 2;

How can I do that ? Can someone show me some snippet ?

thanks.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread 1 should do this:
  • Perform task 1
  • Wait for Thread 2 to end
  • Perform task 2
  • Thread 2 should do this:
  • Perform task 1
  • Start Thread 1
  • Perform task 2
  • And the controlling program should start Thread 2.
    [ June 14, 2006: Message edited by: Paul Clapham ]
     
    Ranch Hand
    Posts: 1170
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thread 2 should not start thread 1 but instead notify thread 1 that it can perform its task.
     
    ben oliver
    Ranch Hand
    Posts: 375
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Paul Clapham:
    Thread 1 should do this:

  • Perform task 1
  • Wait for Thread 2 to end
  • Perform task 2
  • Thread 2 should do this:
  • Perform task 1
  • Start Thread 1
  • Perform task 2
  • And the controlling program should start Thread 2.

    [ June 14, 2006: Message edited by: Paul Clapham ]


    how do I do --

    a) wait for Thread 2 to end ?
    b) let Thread 2 start Thread 1 ?
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm makin this up as I go and not testing so it may be close or, um, not.

    x1, x2 and x3 are any objects that T1 and T2 both have access to. Read up on wait and notify to see how they work. Also read up on locks in the Java5 concurrent library. They offer a more elegant way to do the same thing.
     
    Stan James
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Coming back later I see I didn't try to make run T2-task2 while T1-task1 is running. That will make things more interesting, fer sure.
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by ben oliver:
    how do I do --

    a) wait for Thread 2 to end ?
    b) let Thread 2 start Thread 1 ?

    a) Call Thread 2's join() method.
    b) Have Thread 2 call Thread 1's start() method.
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    According to the question:
    1.T2 do task1
    2.T1 do task1 while T2 do task2
    3.T1 do task2

    ~If T1 should exit task1 as soon as T2 finished task2, no matter whether task1 of T1 is completed, then T2 should send a signal to T1 as command.
    ~If step 3 starts after both T1's task1 and T2's task2 is completed, then you might need another signal
     
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In your Runnable for thread1 have Task1 come first and then Task2. In your
    Runnable for thread2 have Task2 come first and then Task1. In between thread1's
    Task1 and Task2 code have it join( ) to the end of thread2. In between thread2's
    Task2 and Task1 have IT join( ) to the end of thread1. It seems that you don't
    want these two threads to outrace each other as they swap tasks and join( )
    may do the trick. Let me know if it works if not there may be another way.
     
    Leo Velazquez
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh you need a join( ) at the end of each Runnable target too.
     
    Leo Velazquez
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry........Maybe this will work. Forget my other post.
    Have thread1's Runnable do Task1 first and then Task2 and have thread2's
    sequence be the reverse. Put these two tasks in a while(true) or instead of
    true in while's parameter put a flag that ends the program.

    Have two static flags in your program class: one flipped to true by thread1 when
    it completes any of the two task code blocks and one to be flipped to true by
    thread2 whenever it completes any of its two tasks which should be the same as
    thread1's only reversed in order.

    Have each flip their flag to true signalling to the other a completed task then
    have them check their partner thread's flag to see if they've completed theirs.

    Thread2 completes its first task -- the opposite of thread1's and then does this:
    thread2taskcomplete = true;// Ive completed my first task
    if (thread1taskcomplete == true) thread1.interrupt();// have you? If you have you're
    // probably sleeping let me wake
    else sleep( ); //you up --- if you're still working I'll sleep you wake me when
    //you CATCH UP.
    thread2taskflag = false //reset flag before diving into next task

    And Thread1 does this after flipping it's Ive-completed-my-first-task-flag to true
    if (thread2taskcomplete == true) thread2.interrupt();
    else sleep( );
    thread2taskcompleted == false;
    task code...

    You do this twice in each Runnable -- once after each task. You could also use
    wait( ) called from the object instead of sleep( ) and leave interrupt() as it is.

    If the other thread isn't through in other words you sleep until he wakes you up
    otherwise you wake HIM up and you're both off again.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic