Results 1 to 3 of 3
  1. #1
    SantoshBK09 is offline Member
    Join Date
    Dec 2008
    Rep Power

    Default 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,

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    Actually, the solution to the challenge is not Threading, you can use:
    Java Code:
    // Tests if this thread is alive.
    but for recurrent sorting, that is not effective as once a Thread has completed work there is no way to re-invoke the thread.

    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

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Rep Power

    Default 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.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 09:06 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts