5 Replies - 3606 Views - Last Post: 20 December 2009 - 12:48 PM Rate Topic: -----

#1 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

How do I run multiple tasks one after another after each one finishes?

Posted 19 December 2009 - 02:12 AM

for(int i = 0;i < this.totalItems();i++){
   model.setCurrentPhoto(i+1);
   messagebox("current photo: "+model.getCurrentPhoto());
   model.setWaiting(this.totalItems()-model.getUploaded());
   model.setProcPhoto(this.getfile(i));
   task = new Task();
   task.addPropertyChangeListener(this);
   task.execute();
 
  ///while( !task.isDone() == true ){} // HAHA>>> made this attempt...didn't work out to well...lol

}



and this loop may instigate one task creation (easy and works now) OR it could loop twice in which case the tasks just collide and my progress bar ( in the other part of the application ) is ...uhh...for lack of a better word...FUBAR!

So here is my nice and easy question (for all you geniuses out there), how can I make these tasks go one after the other...I am not completely hopless cause my attempts (though pathetic. trust me..i know :) would be something like:

taskwait() // but here what will it wait for?
TaskQueue queue = new TaskQueue;// use this??

Thanks - I am so lost...this will complete my program when I get this working...help me out please and thank you,

Adam

Is This A Good Question/Topic? 0
  • +

Replies To: How do I run multiple tasks one after another after each one finishes?

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: How do I run multiple tasks one after another after each one finishes?

Posted 19 December 2009 - 03:13 AM

The simplest answer is to have one task call the following one. If you look at the Executor documentation, the example given (SerialExecutor) does exactly that.

This post has been edited by EdwinNameless: 19 December 2009 - 03:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

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

Re: How do I run multiple tasks one after another after each one finishes?

Posted 19 December 2009 - 08:20 PM

I presume that Task is a thread
If you want to run them, as your topic title suggest, one after the other one finishes why do you use threads ?
Was This Post Helpful? 0
  • +
  • -

#4 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: How do I run multiple tasks one after another after each one finishes?

Posted 20 December 2009 - 10:43 AM

Alright got it to work (yes pbl...i should have done that :) )

I created the new task and executed it in a function

then in the Task class in the don() function I called this class again if there was another item that was put in the array (with filenames as strings)...so it would check the next index in the array if not null cal the function to establish a new task.

If anyone is interested heres da code (used the simpleFTP package for this...hoping to find a new one)

The task class
	class Task extends SwingWorker<Void, Void> {
		FileInputStream fis;
		@Override
		public Void doInBackground() {
			Random random = new Random();
			int progress = 0;
			// Initialize progress property.
			setProgress(0);
			while (progress < 100) {

				try{

					// Put path into input filestream
					fis = new FileInputStream(getpath(model.getCurrentPhoto()-1));

					// Attempt to upload - failure
					if( ! ftp.stor(fis, getfile(model.getCurrentPhoto()-1))){
						messagebox("can't upload!");
					}

				}catch(IOException e){
					messagebox(""+e);
				}

				// Make random progress.
				progress += random.nextInt(10);
				setProgress(Math.min(progress, 100));
			}

			return null;

		}

		/**
		 * Executed in event dispatching thread
		 */
		@Override
		public void done() {
			Toolkit.getDefaultToolkit().beep();
			cutext.setText("All uploads Finished");
			// There are no more items in the array
			if(totalItems() == model.getCurrentPhoto()){
				try{
					ftp.disconnect();
				}catch(Exception e){
					messagebox("this should not happen");
				}
			}else{
				// next task
				callTask();
			}
		}
	} // end task class



callTask()
	public void callTask(){
	// Create the task and execute
	task = new Task();
	task.addPropertyChangeListener(this);
	task.execute();
	}


This post has been edited by scottyadam: 20 December 2009 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: How do I run multiple tasks one after another after each one finishes?

Posted 20 December 2009 - 12:44 PM

Just wanted to point this out. In your method definition here: public Void doInBackground() {, the Void should be lowercase to read void.
Was This Post Helpful? 0
  • +
  • -

#6 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: How do I run multiple tasks one after another after each one finishes?

Posted 20 December 2009 - 12:48 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 11:44 AM, said:

Just wanted to point this out. In your method definition here: public void doInBackground() {, the Void should be lowercase to read void.


thx man - wow...didn't even notice that (must have been lacking sleep) :D made the change to my code

This post has been edited by scottyadam: 20 December 2009 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1