Callable and Runnable are used in a similar way, the difference being:
1, callable use the call () method, Runnable use the Run () method
2, call () has a return value, and the run () method does not return a value.
3, call () can throw the checked exception, such as ClassNotFoundException, and run () cannot throw the checked exception.
Callable examples are as follows:
Class Callableimpl implements callable {private int age;
Public Callableimpl (int age) {this.age = age; }//Concrete logic is implemented here, and there is a return value public Object call () throws Exception {Thread.CurrentThread (). Sleep (new Random (). Nextint (1000
));
String ss= "";
if (age < 0) {ss= = "=" + Aging + ", enter an error";
else if (age <) {ss= ' age = ' + age + ', hello, young man ';
else if (age <) {ss= ' age = ' + age + ', hello, middle-aged ';
else {ss= "age =" + year + ", Hello, old man";
} System.out.println (ss);
return SS; } public class Callabletest {public static void main (string[] args) throws Interruptedexception, Executionexception
{//Establish a thread pool executorservice exc = Executors.newfixedthreadpool (3);
Store returned results list<future<string>> futures = new arraylist<future<string>> ();
for (int i = -10; i < i + 3) {Callableimpl call = new Callableimpl (i);
Executes the call method of the callable and has the return value future<string> Future = Exc.submit (call); FutureS.add (future);
} System.out.println ("Wait for Result:"); for (future<string> future:futures) {//The call to the Get () method blocks the current thread until the result is returned SYSTEM.OUT.PRINTL
N ("-----------" +future.get ());
if (Future.isdone ()) {//System.out.println (Future.get ());
else {//System.out.println ("Future result isn't yet complete");
} System.out.println ("End ...");
Thread.CurrentThread (). Sleep (1000*30);
Exc.shutdown (); }
}
Results:
Wait Result: Age =-10, enter an error ages =-10, enter age =-4, enter age = =-1, input age = =
2, Hello, young The Age
=-7, the input ages is wrong
-----------the aged =-7, the input age is incorrect
-----------ages =-4, enter the age to be wrong
-----------ages =-1, the input age is wrong c9/>-----------age = 2, hello, young age
= 11, hello, young age
= 5, hello, young
-----------age = 5, hello, young age
= 8, Hello, young
-----------age = 8, hello, young
-----------age = 11, hello, young age =
17, hello, young age
= 14, hello, young
-----------age = 14, hello, young
-----------age = 17, hello, young age
= 26, hello, middle-aged age
= 23, hello, middle-aged age
= 20, Hello, middle-aged
-----------age = 20, hello, middle-aged
-----------age = 23, hello, middle-aged
-----------age = 26, hello, middle-aged age
= 3 5, Hello, middle-aged
age = 32, hello, middle aged =
41, hello, old man
= 29, hello, middle-aged
-----------aged = 29, hello, middle-aged
------- ----Age = 32, hello, middle-aged
-----------age = 35, hello, middle-aged age
= 38, hello, middle-aged
-----------age = 38, hello, middle-aged
----- ------age = 41, Hello, aged
= 44, hello, elderly
-----------age = 44, hello, old age
= 47, hello, old people
-----------age = 47, Hello, elderly
end ...
runnable Example:
Public class Liftoff implements Runnable {
protected int countdown = ten;
private static int taskcount = 0;
private final int id = taskcount++;
Public liftoff () {
} public
liftoff (int countdown) {
this.countdown = countdown;
}
Public String status () {return
"#" + ID + "(+ (Countdown > 0?) Countdown: "liftoff!") + ")";
}
@Override public
Void Run () {while
(countdown--> 0) {
System.out.print) (status ());
Thread.yield ();
}
System.out.println ();
}
public static void Main (string[] args) {
Executorservice exec = executors.newfixedthreadpool (1);
for (int i = 0; i < 5; i++) {
Exec.execute (new Liftoff ());
}
Exec.shutdown ();
}
Note that Executorservice is using submit () in the callable, and execute () is used in runnable.