How can I use the same thread pool implementation for different tasks?

Dear java programmers,
I have written a class which submits Callable tasks to a thread pool while illustrating the progress of the overall procedure in a JFrame with a progress bar and text area. I want to use this class for several applications in which the process and consequently the Callable object varies. I simplified my code and looks like this:
        threadPoolSize = 4;
        String[] chainArray = predock.PrepareDockEnvironment();
        int chainArrayLength = chainArray.length;
        String score = "null";
        ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
        CompletionService<String> referee = new ExecutorCompletionService<String>(executor);
        for (int i = 0; i < threadPoolSize - 1; i++) {
            System.out.println("Submiting new thread for chain " + chainArray);
referee.submit(new Parser(chainArray[i]));
for (int chainIndex = threadPoolSize; chainIndex < chainArrayLength; chainIndex++) {
try {
System.out.println("Submiting new thread for chain " + chainArray[chainIndex]);
referee.submit(new Parser(chainArray[i]));
score = referee.poll(10, TimeUnit.MINUTES).get();
System.out.println("The next score is " + score);
executor.shutdown();
int index = chainArrayLength - threadPoolSize;
score = "null";
while (!executor.isTerminated()) {
score = referee.poll(10, TimeUnit.MINUTES).get();
System.out.println("The next score is " + score);
index++;
My question is how can I replace Parser object with something changeable, so that I can set it accordingly whenever I call this method to conduct a different task?
thanks,
Tom                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

OK lets's start from the beginning with more details. I have that class called ProgressGUI which opens a small window with 2 buttons ("start" and "stop"), a progress bar and a text area. It also implements a thread pool to conducts the analysis of multiple files.
My main GUI, which is much bigger that the latter, is in a class named GUI. There are 3 types of operations which implement the thread pool, each one encapsulated in a different class (SMAP, Dock, EP). The user can set the necessary parameters and when clicking on a button, opens the ProgressGUI window which depicts the progress of the respective operation at each time step.
The code I posted is taken from ProgressGui.class and at the moment, in order to conduct one of the supported operations, I replace "new Parser(chainArray)" with either "new SMAP(chainArray[i])", "new Dock(chainArray[i])", "new EP(chainArray[i])". It would be redundant to have exactly the same thread pool implementation (shown in my first post) written 3 different times, when the only thing that needs to be changed is "new Parser(chainArray[i])".
What I though at first was defining an abstract method named MainOperation and replace "new Parser(chainArray[i])" with:
new Callable() {
  public void call() {
    MainOperation();
});For instance when one wants to use SMAP.class, he would initialize MainOperation as:
public abstract String MainOperation(){
    return new SMAP(chainArray));
That's the most reasonable explanation I can give, but apparently an abstract method cannot be called anywhere else in the abstract class (ProgressGUI.class in my case).
Firstly it should be Callable not Runnable.Can you explain why? You are just running a method and ignoring any result or exception. However, it makes little difference.ExecutorCompletionService takes Future objects as input, that's why it should be Callable and not Runnable. The returned value is a score (String).
Secondly how can I change that runMyNewMethod() on demand, can I do it by defining it as abstract?How do you want to determine which method to run?The user will click on the appropriate button and the GUI will initialize (perhaps implicitly) the body of the abstract method MainOperation accordingly. Don't worry about that, this is not the point.
Edited by: tevang2 on Dec 28, 2008 7:18 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • How can you use the same e-mail address for multiple ipads?

    I have two iPads.. an iPad and an ipad 2.  I registared my new ipad 2 and now my old ipad is will not let me log on and is telling me that my e-mail address is already in use.  how can you use the same e-mail address for multiple ipads?

    And by using the same Apple ID you can also share purchases.  If you have a different Apple ID for each iPhone then you can't share purchases.

  • How can I use the same thread to display time in both JPanel & status bar

    Hi everyone!
    I'd like to ask for some assistance regarding the use of threads. I currently have an application that displays the current time, date & day on three separate JLabels on a JPanel by means of a thread class that I created and it's working fine.
    I wonder how would I be able to use the same thread in displaying the current time, date & day in the status bar of my JFrame. I'd like to be able to display the date & time in the JPanel and JFrame synchronously. I am developing my application in Netbeans 4.1 so I was able to add a status bar in just a few clicks and codes.
    I hope somebody would be able to help me on this one. A simple sample code would be greatly appreciated.
    Thanks in advance!

    As you're using Swing, using threads directly just for this kind of purpose would be silly. You might as well use javax.swing.Timer, which has done a lot of the work for you already.
    You would do it something like this...
        ActionListener timerUpdater = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                // DateFormat would be better, but this is an example.
                String timeString = new Date().toString();
                statusBar.setText(timeString);
                someOtherLabel.setText(timeString);
        new Timer(1000, timerUpdater).start();That code will update the time once a second. If you aren't going to display seconds, you might as well increase the delay.
    The advantage of using a timer over using an explicit thread, is that multiple Swing timers will share a single thread. This way you don't blow out your thread count. :-)

  • How can I use the same "I Tunes" on two different computers

    I’m using two different kinds of Personal Computer: One is Desktop PC And the other one is Notebook .
    In each computer there are the same songs and when I get the new ones to the Desktop I want them to be placed on the Notebook or reverse.
    Where does the I Tunes save the playlist on the hard drive ?
    I have prepared a playlist on my Notebook and want it to be seen on the Desktop but I dont want to fallow the steps File > Library > Export Playlist on the Notebook. Is there any other way to make this issue instead of the mentioned steps.
    How can I see the same Playlists and songs on two different computers ( notebook and desktop) including the updating processes.
    Operating systems of the computers:
    Desktop: XP Pro
    Notebook: Vista Bussiness

    I use SyncToy 2.0 to keep two instances of my library in sync. SyncToy's preview will show which files it is about to update giving you a chance to spot unexpected changes and during the run only the changed files will be copied saving lots of time.
    As long as I sync after making updates at one instance of the library before making any updates at the other then everything works just fine. I periodically run iTunes Folder Watch to check for any orphaned entries or new items that haven't been imported into the active library. If your machines are regularly networked together this process is fairly straightfoward. Mine are in disparate locations so I use one of the (physically) small WD My Passport host powered drives which I take between home & work, synchronising at either end. This gives me three complete copies of my library so I'm covering backup & synchronisation in the same operation.
    When connecting my iPod at either location iTunes recognises itself as the "home" library for the iPod so I'm able to use the sync with selected playlists option without getting warning messages about the iPod being synced with a different library.
    tt2

  • How can I use the same Apple Id on two different devices without having to re-enter my payment details

    I have a Macbook Pro and an iPad2 which I sync to the Macbook. Sometimes I buy apps straight from the iPad and other times from the Macbook but each time I change from one to the other I have to re-enter my payment details. Can I set my account up to recognise that I may buy from both of these devices?

    Apple "verifies" your Apple ID account information with every purchase of all free or paid apps whether iOS devics or computers.
    Apple wants to make sure someone else hasn't compromised your account.

  • How can i use the same cursor in a loop fro multipletimes.

    I am using two cursors.One to fetch sites and the other to fetch participants under each site.I am performing some job with that participants data.Now the problem is i am using the 2nd cursor in a loop.So it fetches the data of participants falling under one state.But when it comes to the second state,as the second cursor is already open it is unable to fetch the records.Please help me .How can i use the same cursor in a loop fro multipletimes.
    I am sending the code which i have written in When-Button-Pressed-Trigger...
    declare
         sid number;
         pid number;
    cursor csid is select distinct(site_id) from cyber_ppt;
    cursor cpid is select pc_id,st_dt,ed_dt from cyber_ppt where site_id = sid;
         stdt varchar2(10);
         eddt varchar2(10);
         nom number;
         stmonth varchar2(10);
         edmonth varchar2(10);
         cjan number:=0;
         cfeb number:=0;
         cmar number:=0;
         capr number:=0;
         cmay number:=0;
         cjun number:=0;
         cjul number:=0;
         caug number:=0;
         csep number:=0;
         coct number:=0;
         cnov number:=0;
         cdec number:=0;
         i number:=1;
    begin
         open csid ;
         loop
         fetch csid into sid;
              exit when csid %notfound;
              message(sid);
         open cpid;
         loop
         fetch cpid into pid,stdt,eddt ;
         exit when cpid %notfound;
         message(sid||'-'||pid);
         stmonth:=substr(stdt,4,3);
         edmonth:=substr(eddt,4,3);
         nom:= months_between(eddt,stdt);
    while i <= round(nom)
         loop
         stmonth:=substr(stdt,4,3);
    if stmonth='JAN' then
              cjan:=cjan+1;
    elsif stmonth='FEB' then
              cfeb:=cfeb+1;
    elsif stmonth='MAR' then
              cmar:=cmar+1;
    elsif stmonth='APR' then
              capr:=capr+1;
    elsif stmonth='MAY' then
              cmay:=cmay+1;
    elsif stmonth='JUN' then
              cjun:=cjun+1;
    elsif stmonth ='JUL' then
              cjul:=cjul+1;
    elsif stmonth ='AUG' then
              caug:=caug+1;
    elsif stmonth ='SEP' then
              csep:=csep+1;
    elsif stmonth ='OCT' then
              coct:=coct+1;
    elsif stmonth ='NOV' then
              cnov:=cnov+1;
    elsif stmonth ='DEC' then
              cdec:=cdec+1;
    end if;
         stdt:=add_months(stdt,1);
         i:=i+1;
         end loop;
         end loop;
         end loop;
         end;
         

    try this /* untested */
    DECLARE
    sid           NUMBER;
    pid           NUMBER;
    CURSOR csid IS SELECT DISTINCT(site_id) FROM cyber_ppt;
    CURSOR cpid(nSid NUMBER) is SELECT pc_id,st_dt,ed_dt FROM cyber_ppt WHERE site_id = nSid;
    stdt        VARCHAR2(10);
    eddt        VARCHAR2(10);
    nom         NUMBER;
    stmonth     VARCHAR2(10);
    edmonth     VARCHAR2(10);
    cjan         NUMBER:=0;
    cfeb         NUMBER:=0;
    cmar         NUMBER:=0;
    capr         NUMBER:=0;
    cmay         NUMBER:=0;
    cjun         NUMBER:=0;
    cjul         NUMBER:=0;
    caug         NUMBER:=0;
    csep         NUMBER:=0;
    coct         NUMBER:=0;
    cnov         NUMBER:=0;
    cdec         NUMBER:=0;
    i            NUMBER:=1;
    BEGIN
    FOR rec IN csid
    LOOP
                      sid := rec.csid;
    FOR cRec IN cpid(sid)
    LOOP
                     pid := cRec.pc_id;
                     stdt := cRec.st_dt;
                     eddt := cRec.ed_dt;
    stmonth:=  SUBSTR(stdt,4,3);
    edmonth:= SUBSTR(eddt,4,3);
    nom:= months_between(eddt,stdt);
    WHILE i <= round(nom)
    LOOP
              stmonth := SUBSTR(stdt,4,3);
    IF stmonth='JAN'
    THEN
             cjan:=cjan+1;
    ELSIF stmonth='FEB' THEN
             cfeb:=cfeb+1;
    ELSIF stmonth='MAR' THEN
              cmar:=cmar+1;
    ELSIF stmonth='APR' THEN
              capr:=capr+1;
    ELSIF stmonth='MAY' THEN
              cmay:=cmay+1;
    ELSIF stmonth='JUN' THEN
              cjun:=cjun+1;
    ELSIF stmonth ='JUL' THEN
              cjul:=cjul+1;
    ELSIF stmonth ='AUG' THEN
              caug:=caug+1;
    ELSIF stmonth ='SEP' THEN
              csep:=csep+1;
    ELSIF stmonth ='OCT' THEN
              coct:=coct+1;
    ELSIF stmonth ='NOV' THEN
              cnov:=cnov+1;
    ELSIF stmonth ='DEC' THEN
              cdec:=cdec+1;
    END IF;
             stdt:=add_months(stdt,1);
             i:=i+1;
    END LOOP;
    END LOOP;
    END LOOP;
    END;

  • How can i use the same front panel graph in more than one events in an event structure?

    i want to display the signals from my sensorDAQ in a graph.but i have more than one event in the event structure to acquire the signal and display it in the graph.the first event is to acquire the threshold signals and its displayed in the graph as a feedback.after the first event is executed, i will call the second event,where the further signals are acuired and compared with the threshold signals from the event 1.my question is how can i use the same front panel control in more than two events in the event structure?please answer me i'm stuck.
    Solved!
    Go to Solution.

    Hi,
    I have attached here an example of doing the same using shift registers and local variables. Take a look. Shift register is always a better option than local variables.
    Regards,
    Nitzz
    (Give kudos to good answers, Mark it as a solution if your problem is Solved) 
    Attachments:
    Graph and shift registers.vi ‏12 KB
    graph and local variables.vi ‏12 KB

  • How can we use the same package in our report used by some other report

    how can we use the same package in our report used by some other report

    Hi,
    You just need to assign package while saving your report.
    No extra is required providing you are aware of package to be used.

  • HT204053 Can I use the same Apple ID on two different iphones?

    Can I use the same Apple ID on two different iphones and use facetime to communicate ?

    Yes.
    http://ipad.about.com/od/iPad_Guide/ss/How-To-Use-FaceTime-On-The-iPad_2.htm

  • Multiple iphones each with an apple id - can i use the same laptop and itunes for back up, restore and updates?

    We have two iphones each with a different apple id - can i use the same laptop and itunes for back up, restore and updates without risking having my apps overwritten.  Basically does itunes keep the two iphones as two separate entities ?  Does iTunes differentiate between the two devices and keep two different SYNCs ?

    Yes, you can use the same computer for BACK UPS, RESTORING AND UPDATING. But that is it.
    You cannot, however, use it to sync the different devices. iTunes will only recognize one device to sync with and if you sync any other devices with that iTunes library, it will replace what is on the phone with whatever is in the library. It cannot differentiate between devices. At most, it will jsut recognize that the phone has been synced with a different iTunes library.
    So if you would like to use the same computer for backing up, restoring and updating, just make sure to turn of Automatic Syncing in your iTunes Preferences.

  • I have a Ipod Nano and want to get another one , and use both. Can I use the same computer and library for both?

    I have a Ipod Nano and want to get another one , and use both. Can I use the same computer and library for both?

    Yes

  • Can I use the same email address on two different iPads?

    Can I use the same email address on two different iPads?
    Jackie

    I use AOL for my work account, I can open an email on my iPad, my iPod Touch, my iMac, another iMac in the office and there are no issues at all. However, my AOL account is IMAP. I read email on the iPad, turn around and read and print from the iMac and someone else is accessing the email at the same time.
    We access the same emails all the time as we have one generic email address that we both use. We like to multitask with AOL.

  • How can i use the amp designer or pedalboard for software instruments in garageband 10

    how can i use the amp designer or pedalboard for software instruments in garageband 10?
    and how can i customize the effects for the master track? in different factory presets are different effects, but how can i choose them manually?

    hongconga wrote:
    I want to record a podcast using my Mackie mixer and have the mix sent to Garageband through the USB I/0, but it doesn't seem to give me the option.
    i think the first thing to do would be to check with Mackie support. two things noted on Mackie's website:
    DRIVERS::
    No Driver Required for Supported Windows (PC) or OS X (Mac) Versions
    (note "supported")
    and
    For the Mac::
    Mac OS X 10.4.11 – 10.7.1
    ask if their firmware currently supports OS X 10.9.x

  • Can I use the "same" button multiple times for multiple galleries?

    OK so I am extremely untrained in CS4 and Actionscript. However I have managed to get along fairly well until I started to dynamically upload images as a gallery. This works great if I have one gallery, but for my site I have 9 galleries!!! I have a back and next button, but I want to be able to use those same buttons for all of the galleries so they look the same. I have split them up and renamed them, but I am clueless on how to script the buttons to work. Please help...and don't laugh at my poor scripting. This is what I have now because I do not know where to put the other button names without getting errors.
    stop();
    next_btn .addEventListener(MouseEvent.CLICK, nextImage);
    var imageNumber: Number=1;
    function checkNumber(): void{
        next_btn.visible=true;
        back_btn.visible=true;
        if(imageNumber==15){
            trace(imageNumber);
        next_btn.visible=false;
        if(imageNumber==1){
            trace(imageNumber);
        back_btn.visible=false;
    function nextImage(evtObj:MouseEvent):void {
        imageNumber++;
        mc_engagement.source= "photo/engagement/en0"+imageNumber+".jpg";
        mc_amish.source= "photo/amish/Amish"+imageNumber+".jpg";
        mc_chicago.source= "photo/chicago/ch"+imageNumber+".jpg";
        mc_landscapes.source= "photo/landscapes/land"+imageNumber+".jpg";
        mc_goodvsevil.source= "photo/goodvsevil/ge"+imageNumber+".png";
        mc_animals.source= "design/animals/an"+imageNumber+".png";
        mc_icons.source= "design/icons/icon0"+imageNumber+".png";
        mc_objects.source= "design/objects/pc"+imageNumber+".png";
        mc_typography.source= "design/typography/type"+imageNumber+".png";
        checkNumber();
    back_btn .addEventListener(MouseEvent.CLICK, backImage);
    function backImage(evtObj:MouseEvent):void {
        imageNumber--;
        mc_engagement.source= "photo/engagement/en0"+imageNumber+".jpg";
        mc_amish.source= "photo/amish/Amish"+imageNumber+".jpg";
        mc_chicago.source= "photo/chicago/ch"+imageNumber+".jpg";
        mc_landscapes.source= "photo/landscapes/land"+imageNumber+".jpg";
        mc_goodvsevil.source= "photo/goodvsevil/ge"+imageNumber+".png";
        mc_animals.source= "design/animals/an"+imageNumber+".png";
        mc_icons.source= "design/icons/icon0"+imageNumber+".png";
        mc_objects.source= "design/objects/pc"+imageNumber+".png";
        mc_typography.source= "design/typography/type"+imageNumber+".png";
        checkNumber();

    I'm still a novice with Flash myself, but I have two comments.
    First, at this point, won't your buttons control all galleries at the same time? Which means, if I go to image 5 in one gallery, then move to another gallery, I'll start at image 5, because the actions are all connected.
    Second, you can definitely use the same buttons for multiple galleries. It seems to me that as long as you've assigned the buttons an instance name, these actions should already work to control all the galleries (see the note above). I guess I would need to understand a bit more about how your project is built.

  • How can I use the same sleepimage over and over?

    Hi, this was asked a while ago for Tiger, but had no real answer. Now I hope things have changed in Leopard 10.5.4.
    I have a STATIC system on a car that should boot over and over in exactly the same way. The idea is to make a good sleepimage (/var/vm/sleepimage) and then force the computer to read it at every startup AS IF it was waking up from sleep mode all the times.
    This MUST be possible because OSX does it every time it wakes up from safesleep (sudo pmset hibernatemode 1). All I need it to find out WHERE OSX writes down that at the next wake it should use the sleepimage instead of a normal boot. Then, once I know where it write this, I simply force that flag to be active all the times.
    Please also be aware of the following notes and constraints:
    0. The boot should be as fast as possible
    1. I can't to use the regular sleep function: the system must drain zero power while shut down
    2. There are no dangers in using the same sleepimage multiple times: the disk contents do not change. The system should be absolutely identical at every boot (or "wake")
    3. The system is ALWAYS shut down by pulling the plug of the MacMini. Thereshould be no possibility for the system to "update" the sleepimage
    I hope some of you OSX experts will give me a hint concerning where this information is stored (and maybe how to override it)
    thanks a lot
    Lele
    Message was edited by: Leelx

    Hi, this was asked a while ago for Tiger, but had no real answer. Now I hope things have changed in Leopard 10.5.4.
    I have a STATIC system on a car that should boot over and over in exactly the same way. The idea is to make a good sleepimage (/var/vm/sleepimage) and then force the computer to read it at every startup AS IF it was waking up from sleep mode all the times.
    This MUST be possible because OSX does it every time it wakes up from safesleep (sudo pmset hibernatemode 1). All I need it to find out WHERE OSX writes down that at the next wake it should use the sleepimage instead of a normal boot. Then, once I know where it write this, I simply force that flag to be active all the times.
    Please also be aware of the following notes and constraints:
    0. The boot should be as fast as possible
    1. I can't to use the regular sleep function: the system must drain zero power while shut down
    2. There are no dangers in using the same sleepimage multiple times: the disk contents do not change. The system should be absolutely identical at every boot (or "wake")
    3. The system is ALWAYS shut down by pulling the plug of the MacMini. Thereshould be no possibility for the system to "update" the sleepimage
    I hope some of you OSX experts will give me a hint concerning where this information is stored (and maybe how to override it)
    thanks a lot
    Lele
    Message was edited by: Leelx

Maybe you are looking for

  • Problem with display of content in Windows XP - help!

    Hi, I recently created a Windows XP partition on my Mac using Boot Camp and am having a problem. Everything that I open in Windows is way too big and in some cases, it means that I can't access the whole page or scroll up or down. Even when I open Gm

  • Repair or Change Motherboard

    Hi, I've got a Lenovo 3000 N200 0769-AH9 Laptop with a T9300 Penryn-CPU and broke my Mainboard(CPU-Cooler doesn't work any more).  I saw a Mainboard for 150 €, which is very cheap, as there are no replacement mainboards to buy under 400€. I would buy

  • OSM O2A - Using extensible attributes in rules

    In our RODOD implementation using OSM 7.0.3, we are using the concept of "extensible attributes" intensively. The concept is great: lots of data coming from Siebel or gathered during the fulfillment process is stored in my order lines' "extensible at

  • I have a HP Pavilion which has a touch sound bar above the keyboard. The display is no longer there

    HP Pavilion Entertainment PC  Windows 7 Wich has a touch sound bar above the keyboard and a display shows on the bottom of the screen when I touch the bar. This display is not there any longer. This question was solved. View Solution.

  • Datum transformation method used by Oracle Spatial

    Hi, Anyone knows what datum transformation method is used by Oracle Spatial? Molodensky, Bursa-Wolf, others? It's was changed over versions 9i, 10g and 11g? Tanks, André Martins [email protected]