This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes how to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "how to "control" the sequence of two threads ?" Watch "how to "control" the sequence of two threads ?" New topic
Author

how to "control" the sequence of two threads ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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 ]
    Mr. C Lamont Gilbert
    Ranch Hand

    Joined: Oct 05, 2001
    Posts: 1170

    Thread 2 should not start thread 1 but instead notify thread 1 that it can perform its task.
    ben oliver
    Ranch Hand

    Joined: Mar 28, 2006
    Posts: 374
    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 ?
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    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.
    Allen Liu
    Greenhorn

    Joined: Jul 03, 2005
    Posts: 5
    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
    Leo Velazquez
    Greenhorn

    Joined: Apr 27, 2006
    Posts: 13
    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

    Joined: Apr 27, 2006
    Posts: 13
    Oh you need a join( ) at the end of each Runnable target too.
    Leo Velazquez
    Greenhorn

    Joined: Apr 27, 2006
    Posts: 13
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to "control" the sequence of two threads ?
     
    Similar Threads
    Help in Multi Threading!
    Swings, Threads, MVC: plz clarify
    UrlyBird 1.3.2 locking with no cookie
    Calling wait() when we have obtained two or more locks
    When would you use synchronized methods as opposed to synchronized blocks?