GithubHelp home page GithubHelp logo

nareshmiriyala / threads Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caichangqi/threads

0.0 3.0 0.0 432 KB

java thread support library

License: GNU Lesser General Public License v3.0

Java 100.00%

threads's Introduction

threads

Threads utils

Examples LimitThreadPool

When you have limited, independent tasks, and you have limited amount of threads and you want to control when all jobs done use LimitThreadPool

  • limited number of tasks
  • tasks are independend and can be processed parallelry
  • need to know when all tasks done

threads

package com.github.axet.threads;

import java.util.concurrent.atomic.AtomicInteger;

import com.github.axet.threads.LimitThreadPool;

public class LimitThreadPoolExample {

    static AtomicInteger c = new AtomicInteger();

    public static void main(String[] args) {
        try {
            // create 4 threads
            LimitThreadPool l = new LimitThreadPool(4);

            // put 100 tasks
            for (int i = 0; i < 100; i++) {
                l.blockExecute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        c.addAndGet(1);
                    }
                });
            }

            // wait until all tasks ends
            l.waitUntilTermination();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(c);
    }
}

Example Recursive Thread Pool

When you do not know how many tasks here, and tasks can produce dependend tasks, and you have limited threads use RecursiveThreadPool.

  • tasks which produce another tasks and here is no way you can make it linear or independent
  • limited threads to process jobs
  • some tasks indepeneded and can be processed parralely

threads

package com.github.axet.threads;

import java.util.concurrent.atomic.AtomicInteger;

public class RecursiveThreadTest {

    // create 4 working threads. all tasks will be distributed between.
    static final RecursiveThreadExecutor threadsHolder = new RecursiveThreadExecutor(4);

    // Recursive function, which do some job, and produce more job
    public static int recurciveTaks(final int level, final int maxLevel) {
        RecursiveThreadTasks threadTasks = new RecursiveThreadTasks(threadsHolder);

        final AtomicInteger r = new AtomicInteger(0);

        for (int i = 0; i < 100; i++) {
            // put first task
            threadTasks.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(10);
                        r.incrementAndGet();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // create depened tasks which should ends before current
                    // task, which can also create another tasks
                    recurciveTaks(level + 1, maxLevel);
                }
            });
        }
        try {
            threadTasks.waitTermination();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return r.get();
    }

    public static void main(String[] args) {
        int result = recurciveTaks(0, 5);
        System.out.println(result);
    }
}    

Cetral Maven Repo

<dependency>
  <groupId>com.github.axet</groupId>
  <artifactId>threads</artifactId>
  <version>0.0.9</version>
</dependency>

threads's People

Contributors

axet avatar

Watchers

James Cloos avatar Naresh Chandra Miriyala avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.