Results 1 to 3 of 3
- 12-05-2008, 03:57 PM #1Member
- Join Date
- Dec 2008
- Rep Power
Does one thread invokes other thread ..
I have a problem, there is mtd A() in a class which purpose is arrange strings. As there are 5 thread invoking that mtd A(), thread one arranges that string in ascending order, thread 3 in desending order, again thread 5 in ascending order and so on...
Now my question is once the thread 1 arranges in string in ascending order after finshing work, thread 1 should call thread 3 for desending order after it finshing the work, this thread should call thread 5 and so on...
Thank in advance...
Hoping it this solution
- 12-06-2008, 05:15 PM #2
Actually, the solution to the challenge is not Threading, you can use:Java Code:
// Tests if this thread is alive. isAlive();
The correct solution to this design is to invoke various methods on the list from the thread you would think of as starting the threads.
To do the above with several threads requires trapping in a while, which is ineffective in some designs, but may be the correct design depending on what it is you want to do .Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 12-07-2008, 07:56 PM #3Senior Member
- Join Date
- Nov 2008
- Rep Power
See BlockingQueues or Lock/Condition
There are different ways to do this, but essentially your problem is "signalling" to another thread. Depending on exact requirements:
(1) You can use the Lock/Condition classes. Create a ReentrantLock, then from that call newCondition() to create Conditions that represent the "things needing to be done" on your string. Then have Thread 1 continually awaiting the condition to "sort", Thread 2 continually awaiting the condition to "reverse" etc. Each one, when it has performed the apporpriate operation, calls signal() on the lock representing the next thing that needs doing.
(2) Have a BlockingQueue that you post "commands" to saying what next needs doing with the string. Have a bunch of threads sitting in a loop waiting for commands from that queue, each time executing the command, then posting the next command to the queue. You could potentially even use a ThreadPoolExecutor, that will create the queue and threads for you. Also consider using a SynchronousQueue, which is a type of BlockingQueue that actually doesn't queue -- it effectively just "passes" the command to another thread, meaning Thread X blocks while waiting for Thread Y (or some other waiting thread) to pull the command off the queue.
Last edited by neilcoffey; 12-07-2008 at 07:58 PM.Neil Coffey
Javamex - Java tutorials and performance info
- By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM
- By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM
- By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM
- By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 08:02 AM