Headline

Multithreading in Language:Java

Characteristics

Multithreading enables parallel or concurrent data processing for the implementation of functional requirements of the system:Company.

Illustration

The data model is implemented as POJOs, as usual. A sequential implementation of the operations for totaling and cutting salaries is also included. For instance:

/**
 * A company has a name and consists of (possibly nested) departments.
 */
public class Company {
    
    private String name;
    private List<Department> depts = new LinkedList<Department>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Department> getDepts() {
        return depts;
    }

    /**
     * Compute total, sequentially
     */
    public Double total() {
        double total = 0;
        for (Department d : getDepts()) {
            total += d.total();
        }
        return total;
    }

    /**
     * Perform cut, sequentially
     */
    public void cut() {
        for (Department d : getDepts()) {
            d.cut();
        }
    }

}

Concurrent computations rely on the identification of actions suitable for multithreading. Department-level actions for totaling and cutting salaries are chosen as the scope here. The following context class shows how an action is not performed sequentially but rather by submission to a thread pool.

/**
 * Run an action via submission to thread pool, i.e., concurrently
 */
public class ConcurrentContext implements Context {

    private static final int POOL_SIZE = 7;

    private ExecutorService pool;

    public ConcurrentContext() {
        pool = Executors.newFixedThreadPool(POOL_SIZE);
    }

    public synchronized <X, Y> void execute(final Action<X, Y> action,
            final X param) {

            final ConcurrentContext context = this;
            pool.submit(new Runnable() {
                public void run() {
                    action.execute(context, param);

                }
            });

    }

    public void waitForTermination() {
        try {
            pool.awaitTermination(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.