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 ?
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
Joined: Jan 29, 2003
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.
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
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.
Joined: Apr 27, 2006
Oh you need a join( ) at the end of each Runnable target too.
Joined: Apr 27, 2006
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.
subject: how to "control" the sequence of two threads ?