Example usage for java.util.concurrent Semaphore acquire

List of usage examples for java.util.concurrent Semaphore acquire

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore acquire.

Prototype

public void acquire(int permits) throws InterruptedException 

Source Link

Document

Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is Thread#interrupt interrupted .

Usage

From source file:it.infn.ct.ParallelSemanticSearch_portlet.java

public void doGet(final ActionRequest request, final ActionResponse response, final App_Input appInput,
        final int numberRecords, final PortletPreferences portletPreferences) {
    testLookup();//from  w  w w.j  a v a  2  s.  c om
    int numThread = countTab(portletPreferences);
    System.out.println("About to submit tasks to " + tp);
    // PortletPreferences portletPreferences = request.getPreferences();

    final Semaphore s = new Semaphore(0);
    Thread thread_openAgris = null;
    Thread thread_culturaItalia = null;
    Thread thread_engage = null;
    Thread thread_europeana = null;
    Thread thread_isidore = null;
    Thread thread_pubmed = null;
    Thread thread_chain = new Thread("CHAIN_THREAD") {
        @Override
        public void run() {

            System.out.println("Executing task in " + Thread.currentThread());

            System.out.println("################### init_thread chain");
            try {
                handlerTabCHAIN(request, response, appInput, numberRecords);
            } catch (RepositoryException ex) {
                Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedQueryException ex) {
                Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (QueryEvaluationException ex) {
                Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedURLException ex) {
                Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            s.release();
            System.out.println("thread_chain isAlive: " + Thread.currentThread().getName() + "---"
                    + Thread.currentThread().isAlive());
            System.out.println("###################### finish thread chain" + Thread.currentThread().isAlive());

        }
    };

    if (portletPreferences.getValue("OpenAgris", "").equals("true")) {
        //  if (appPreferences.OpenAgris.equals("true")) {
        thread_openAgris = new Thread("OPENAGRIS_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread OpenAgris");
                try {
                    handlerTabOpenAgris(request, response, appInput, numberRecords, portletPreferences);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread OpenAgris");

            }
        };
    }

    if (portletPreferences.getValue("CulturaItalia", "").equals("true")) {
        //  if (appPreferences.CulturaItalia.equals("true")) {
        thread_culturaItalia = new Thread("CULTURAITALIA_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread CulturaItalia");
                try {
                    handlerTabCulturaItalia(request, response, appInput, numberRecords, portletPreferences);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread CulturaItalia");

            }
        };
    }

    if (portletPreferences.getValue("Engage", "").equals("true")) {
        //  if (appPreferences.Engage.equals("true")) {
        thread_engage = new Thread("ENGAGE_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread Engage");
                try {
                    handlerTabEngage(request, response, appInput, numberRecords, portletPreferences);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (IOException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread Engage");

            }
        };
    }

    String EuropeanaSet = portletPreferences.getValue("Europeana", "");
    System.out
            .println("EuropeanaSet--" + EuropeanaSet + " appPreferences.Europeana" + appPreferences.Europeana);

    if (EuropeanaSet.equals("true")) {
        thread_europeana = new Thread("EUROPEANA_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread Europeana");
                try {
                    handlerTabEuropeana(request, response, appInput, numberRecords, portletPreferences);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (IOException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread europeana");

            }
        };
    }

    if (portletPreferences.getValue("Isidore", "").equals("true")) {
        //if (appPreferences.Isidore.equals("true")) {
        thread_isidore = new Thread("ISIDORE_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread Isidore");
                try {
                    handlerTabIsidore(request, response, appInput, numberRecords, portletPreferences);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (IOException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread Isidore");

            }
        };
    }

    if (portletPreferences.getValue("Pubmed", "").equals("true")) {
        //if (appPreferences.Pubmed.equals("true")) {
        thread_pubmed = new Thread("PUBMED_THREAD") {

            @Override
            public void run() {

                System.out.println("Executing task in " + Thread.currentThread());

                System.out.println("################### init_thread Pubmed");
                try {
                    handlerTabPubmed(request, response, appInput, numberRecords, portletPreferences);
                } catch (MalformedQueryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (QueryEvaluationException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (RepositoryException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                } catch (IOException ex) {
                    Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null,
                            ex);
                }

                s.release();

                System.out.println("###################### finish thread Pubmed");

            }
        };
    }
    if (tp != null) {
        tp.execute(thread_chain);
        if (thread_openAgris != null) {
            tp.execute(thread_openAgris);
        }
        if (thread_culturaItalia != null) {
            tp.execute(thread_culturaItalia);
        }
        if (thread_engage != null) {
            tp.execute(thread_engage);
        }
        if (thread_europeana != null) {
            tp.execute(thread_europeana);

        }
        if (thread_isidore != null) {
            tp.execute(thread_isidore);
        }
        if (thread_pubmed != null) {
            tp.execute(thread_pubmed);
        }

        try {
            s.acquire(numThread);
        } catch (InterruptedException ex) {
            Logger.getLogger(ParallelSemanticSearch_portlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {

        thread_chain.start();

    }
    //tp.shutdown();
    //while (!tp.isTerminated()) {}
    System.out.println("###################### finish threadPoolMio");
}