Results 1 to 3 of 3
- 12-05-2008, 02:57 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
Does one thread invokes other thread ..
Hi Friends,
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
With Regrds,
Santosh
- 12-06-2008, 04:15 PM #2
Actually, the solution to the challenge is not Threading, you can use:
but for recurrent sorting, that is not effective as once a Thread has completed work there is no way to re-invoke the thread.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, 06:56 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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 06:58 PM.
Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks