Results 1 to 8 of 8
  1. #1
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Running only one thread at a time.

    Hello!

    I'm currently making a fire uploader which is based on a apache commons 3.1. When i'm lets say, queueing 500 files for upload the ftp client would get stuck and well. the program would simply stop working as it should.
    Therefore i want to create a "Thread control" so it does not create all the Upload threads at once. A queue if you want. Ive tried diffrent things now and gave up on Executor. (Which i have no clue why it doesnt work).

    So my question:

    How would i proceed in making a queue so only one thread would run at a time, Meaning all the other threads would either sleep or be created after another is done executing.
    I have tried to use
    Java Code:
    thread.wait();
    but this is a blocking function so my status bars / text would be frozen until all of the threads were done executing.

    The code:
    Java Code:
    ExecutorService threadPool = Executors.newSingleThreadScheduledExecutor();
                		threadPool.execute( new Thread() {
                		        public void run() {
    
                         		       		String record_name = (String) table.getModel().getValueAt(s, 0);
                            				String record_path = (String) table.getModel().getValueAt(s, 2);
                            				System.out.println(record_path);
                                    		    	try {
                                    					    FTPHandler ftpHan = new FTPHandler(record_path,record_name,s);
                                    					    System.out.println("UPLOADING: "+ record_name);
                                    					 
                                    				} catch (UnknownHostException | InterruptedException e1) {
                                    					    System.err.println("FTP Error");
                                    						e1.printStackTrace();
                                    					    FrameHandler.model.setValueAt("ERROR", s, 3);
                                    				}finally{
                                    						FrameHandler.model.setValueAt("Yes", s, 3); //TODO Make this update dynamically.
                                    						progressBar.setValue(progress+=1); //TODO Same as above.
                                    					    
                                    				}
                                    		
                                    		    }
                		        
                		    });
    Edit: i also tried to remove the thread running it without. this works fine for the Linear approach, but the Cells in my JTable would not update before the execution was done. (FrameHandler.model.setValueAt("Yes", s, 3); //TODO Make this update dynamically.) Maybe there is a way to update the JTable each loop?

    If there is any questions, ill be happy to answer them!

    Thanks
    Driiper.
    Last edited by driiper; 08-15-2012 at 09:16 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Running only one thread at a time.

    only one thread would run at a time,
    Why have separate threads if their execution won't overlap? How about having one thread with a loop the executes the jobs one at a time, one after the other?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Running only one thread at a time.

    Java Code:
     
                	SwingUtilities.invokeLater(new Runnable(){
    					@Override
    					public void run() {
    						if(chkAutoUpload.isSelected()){
    		            		//Automatic Upload
    		            		
    		            		 for(Iterator<Integer> i = not_uploaded.iterator(); i.hasNext();) {
    		           		      	//Do Something
    		            			 final int s = i.next();
    		            			 i.remove();
    		            			 String record_name = (String) table.getModel().getValueAt(s, 0);
    		            			 String record_path = (String) table.getModel().getValueAt(s, 2);
    		            			 //System.out.println(record_path);
    		            			 try {
    		            				 update(s);
    		            				 System.out.println("UPLOADING: "+ record_name);
    		            				 FTPHandler ftpHan = new FTPHandler(record_path,record_name,s);
    		            			 } catch (UnknownHostException | InterruptedException e1) {
    		            				 System.err.println("FTP Error");
    		            				 e1.printStackTrace();
    		            				 FrameHandler.model.setValueAt("ERROR", s, 3);
    		            			 }finally{
    		            				 FrameHandler.model.setValueAt("Yes", s, 3); //TODO Make this update dynamically.
    		            				 progressBar.setValue(progress+=1); //TODO Same as above.              					    
    		            			 }
    		            		 }
    		            	}
    						
    					}
                	});


    You mean somthing like this?

    This works fine for the FTP client (It uploads only one file at a time), but then i get a problem with labels and status messages in rest of the program. I want my program to show its status while its looping through the iterator, but currently it just blocks until its cone executing the whole loop. I'm probably horribad explaining this, but ill make a "visual" example on whats happening now:

    Currently:
    Uploading file 1,
    Uploading file 2
    Uploading file..
    End:
    Updating GUI/JTable etc

    I want it to:
    Uploading file 1
    Updating GUI/JTable
    Uploading file 2.
    Updating GUI/JTable
    Updating file 3
    Updating GUI/JTable
    End:

    Ive tried to seperate it with a Thread on the FTPhandler and a Thread on the JTable cell editing, with no luck.



    Driiper.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Running only one thread at a time.

    I want my program to show its status while its looping
    How would it show its status? Is there a GUI field that it would set? The SwingUtilities class has a method to use
    "when an application thread needs to update the GUI". You could call that inside the loop.

    Also see the SwingWorker class
    Last edited by Norm; 08-15-2012 at 10:52 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Running only one thread at a time.

    it should set
    Java Code:
     FrameHandler.model.setValueAt("Yes", s, 3); //TODO Make this update dynamically.
    every loop. but i dont think i fully understand how this works. :)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Running only one thread at a time.

    In the loop, you call the code to update the GUI in of a method controlled by the SwingUtilities invoke... method.
    See the API doc for sample code.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Running only one thread at a time.

    OhGOD finally!

    Thank you so much! it worked with Update inthe SwingUtilities!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: Running only one thread at a time.

    Glad you got it to work with the simpler solution.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Running a thread for a fixed period of time
    By ronyb85 in forum Threads and Synchronization
    Replies: 5
    Last Post: 12-16-2010, 07:26 PM
  2. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  3. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  4. need info on running thread during a particular time interval alone
    By karthikeyan_raju in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2009, 02:40 AM
  5. Calculating the Running time
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:32 PM

Posting Permissions

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