Skip to content

input arguments supporting for java Runnable and Callable

License

Notifications You must be signed in to change notification settings

InCar/ac-func-tion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ac-func-tion

ac-func-tion

Java has 2 very common interfaces: Runnable and Callable

But both have none input argument

I created this project for interfaces like Runnable and Callable, but support accept input arguments.

maven

<dependency>
    <groupId>com.incarcloud</groupId>
    <artifactId>ac-func-tion</artifactId>
    <version>1.1.0</version>
</dependency>
import com.incarcloud.lang.*;

Action<String> actionShow = (String txt)->{
    System.out.println(String.format("action is running with argument '%s'", txt));
};
actionShow.run("Hello Action");

Func<Integer, String> funcSize = (String txt)->{
    System.out.println(String.format("'%s' has %d characters", txt, txt.length));
    return txt.length();
};

String txt = "Func";
int size = funcSize.call(txt);

There are total 10 interfaces, accepte 5 arguments at most.

Action<> like Runnable, no return value:

Action, Action2<T1,T2> ... Action5<T1,T2,T3,T4,T5>

Func<> like Callable, has return value:

Func<R,T>, Func<R,T1,T2> ... Func5<R,T1,T2,T3,T4,T5>

R is the return type, and T is the input type

There are also 2 classes wrapped thread pool.

Action2<Integer, Integer> action = (a, b)->{
    System.out.println(String.format("Action(%d, %d)", a, b));
};

ExecutorForAcFunction pool = new ExecutorForAcFunction(Executors.newFixedThreadPool(2));
for(int i=0;i<5;i++){
    pool.submit(action, i, 10-i);
}

ScheduledExecutorForAcFunction scheduler = new ScheduledExecutorForAcFunction(Executors.newSingleThreadScheduledExecutor());
scheduler.schedule(action, 1000, TimeUnit.MILLISECONDS, 15, 17);

Also defined 10 wrapper classes for compatible with Runnable and Callable

    Action<String> actHello = (x)->{ System.out.println(x); }
    RunnableAction<String> wrap = new RunnableAction<>(actHello, "Hello");
    wrap.run();

    Func2<Integer, Integer, Integer> fnAdd = (x,y)->{ return x+y; }
    CallableFunc2<Integer, Integer, Integer> wrap2 = new CallableFunc2<>(fnAdd, 5, 6);
    Assert.isTrue(13, wrap2.call());

more samples in src/test/java/com/incarcloud/lang/Basic.java

prerequisite

# 执行以下命令检查环境
java -version
gradle --version

Configuration

Users from China can copy gradle-sample.properties to gradle.properties for accelerating downloading speed via ali-yun mirror

Compile

gradle assemble

About

input arguments supporting for java Runnable and Callable

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages