4 Replies - 3773 Views - Last Post: 10 March 2012 - 03:22 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

How to reuse a thread multiple times

Posted 10 March 2012 - 02:58 PM

Hi,

I have one thread that is needed to be used multiple times. I dont want to create multiple instnces of this thread but I would like it to be used multiple time by another class.

Here's my code:

 public class Restaurant
 { 
 
    public static void main(String args[])throws InterruptedException
   {
      int i=0;
      final int NUMTHREADS = 3;              // number of threads 

      Customer thr[] = new Cutomer[NUMTHREADS];      
      Thread myThread[] = new Thread[NUMTHREADS];    
      
	  Waiter r_thr = new Waiter();
	  Thread rThread = new Thread(r_thr); 
	  
	   rThread.start(); 
      // create threads
      for( i = 0; i < NUMTHREADS; ++i ) 
      {
	     thr[i] = new Customer(i);
         myThread[i] = new Thread( thr[i] );   // eah array hold an instance of sem1
         myThread[i].start();                  // start the thread
         
	  }
    
    }
}





As of now, my waiter thread runs once and quits. I want it to run multiple times ( in this case, 3 times).


Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to reuse a thread multiple times

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to reuse a thread multiple times

Posted 10 March 2012 - 03:15 PM

I don't think it is feasible in an easy way.
Then a Thread finishes (exit the run() method) its Thread.State passes from Thread.State.RUNNABLE to Thread.State.TERMINATED and the Thread class does not have a setState(Thread.State) method. (Actually wouldn't be a good idea to let a programmer to change on the fly a Thread Thread.State :))

But you can keep the Thread in its run() method waiting for a certain condition, set by the ones who want to use that thread, and then act accordingly

public void run() {
  while(true) { 
      ... wait for condition
      execute code
  }
}


Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5466
  • View blog
  • Posts: 11,742
  • Joined: 02-June 10

Re: How to reuse a thread multiple times

Posted 10 March 2012 - 03:15 PM

So you're saying you want them to run sequentially? (When one quits, the next starts) Is that right? [just trying to understand the question before throwing out advice that doesn't match your need]
Was This Post Helpful? 1
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: How to reuse a thread multiple times

Posted 10 March 2012 - 03:17 PM

Quote

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
~ Thread API

Putting a number on the amount of times a thread runs seems like bad design, but not necessarily. Care to explain?
Was This Post Helpful? 2
  • +
  • -

#5 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

Re: How to reuse a thread multiple times

Posted 10 March 2012 - 03:22 PM

pbl, you got it!

Thanks everyone for your response. I tried what pbl advised and it worked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1