Current location: Hot Scripts Forums » Programming Languages » Everything Java » threading pooling


threading pooling

Reply
  #1 (permalink)  
Old 04-01-08, 02:01 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
threading pooling

Hello everyone,

please see the below code first

java Code:
  1. package com.test;
  2. class DemoAlive extends Thread {
  3.  
  4.     int value;
  5.  
  6.     public DemoAlive(String str)
  7.     {
  8.         super(str);
  9.         value=0;
  10.         start();   
  11.     }
  12.  
  13.  
  14.      public void run()
  15.      {
  16.    
  17.         try
  18.      {
  19.             while (value < 5) {
  20.              
  21.       System.out.println(getName() + ": " + (value++));
  22.       test(getName());
  23.                 Thread.sleep(250)
  24.             }
  25.         } catch (Exception e) {}
  26.  
  27.         System.out.println("Exit from thread: " + getName());
  28.     }
  29.     
  30.      public void test(String str){
  31.           System.out.println(str +": testing threading");
  32.      }
  33. }
  34.  
  35. package com.test;
  36.  
  37. import java.io.File;
  38.  
  39. public class threadimpl {
  40. public static void main(String[] args) {
  41.         // TODO Auto-generated method stub
  42.         threadimpl obj=new threadimpl();
  43.         obj.listDirectory(new File("D:\\Sample"));
  44.     }
  45.    
  46.     private void listDirectory(File path){
  47.         if(path.isDirectory()){
  48.             String[] listDir=path.list();
  49.             for(int dirCount=0; dirCount<listDir.length;dirCount++){
  50.                 File filePath=new File(listDir[dirCount]);
  51.                 if(filePath.isDirectory()){
  52.                     listDirectory(filePath);
  53.                 }else{
  54.                     processFile(filePath);
  55.                 }
  56.             }
  57.         }else{
  58.             processFile(path);
  59.         }
  60.     }
  61.    
  62.     private void processFile(File datFile){
  63.         DemoAlive obj= new DemoAlive(datFile.getName());
  64.     }
  65. }

this is working properly but now i want to create threads for first 5 file when one thread complete then another thread for new file should start.

example. i have 10 files. this code should process first 5 file then when any file complete the process then 6th file should enter for processing.

any help.

Thanks,

Last edited by Nico; 04-01-08 at 10:06 AM.
Reply With Quote
  #2 (permalink)  
Old 04-02-08, 04:50 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
There's really no point in doing it like that: You keep adding a new File object to your DemoAlive class, when you can easily pass on all data. If you want to do it your way, you will have to create a new class, also threaded, to check what the current value of the DemoAlive threaded class is, and, if value=4, add a new File object. This way you will overcomplicate such a simple task.

Can you explain a little more what you're trying to do? Maybe I can give you some advice on using threads (or maybe not ).

At first sight it seems lke you're about to process files, correct? And instead of waiting till 1 File is processed so you can proceed the scanning, you want to do it all at once, right?
If this all is true, I'd use a Queue (FIFO=first in-first out) to store all File objects you found during the scanning of a directory. You store the Queue (different link than the one above) in a threaded class, suchas your DemoAlive class, and keep appending files to it while you scan. There's probably a limit to the size of the Queue, so you might have to create several instances of the DemoAlive class, and spread the scanned File objects over the threads. There's no point on "updating" the Queue in one of the existing threads, just leave the thread to die once it has processed its Queue.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 04-03-08, 07:17 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
yes .. you are bit close but i di it in different way see the code


java Code:
  1. private void processFile(File datFile){
  2.        
  3.         DemoAlive obj= new DemoAlive(datFile.getName(), count++);
  4.         try{
  5.             while (Thread.activeCount()>5) {
  6.                  System.out.println("Sleeping: " + obj.getName());                 
  7.                  obj.join();
  8.             }
  9.          }catch(Exception ex){
  10.           
  11.          }
  12.        
  13.     }

it is working for me.
it create 5 threads the it wait to finish first five thread then after finishing all first 5 threads then it create next 5 threads.

but actually i wanted that one thread complete among 5 threads i could start 6th one.

please let me know how can i do that.

see my plan is that.
i have created a list of files from system.
i need to scan all files and one thread should scan one file and so on.
and also 5 thread should only be active at a time.

Please suggest me.

Thanks

Last edited by mohit; 04-03-08 at 07:25 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
thread pooling and synchronization siha Windows .NET Programming 0 07-30-05 04:39 AM
Does Apache perform connection pooling? navinchetna@yahoo.com PHP 0 03-25-05 07:03 AM
PreparedStatement Pooling vinu Everything Java 0 01-06-04 07:02 AM


All times are GMT -5. The time now is 05:44 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.