Multi-threaded through Callable Interface

A. Multi-threaded through Callable Interface

c. implement Callable override the call method 
to achieve Callable and achieve similar Runnable, but more powerful, specific performance in 
a. may provide a return value after the end of the task, Runnable not 
b.call method may throw an exception, Runnable's run method No 
c. You can monitor the results of the method invocation call target thread running through Fulture objects Callable get, get the return value, (fulture.get (), will block the call until get to the return value)

1.Callable Interface Description:

(1) java.util.concurrent.Callable is a generic interface, only one call () method

(2) call () method throws an exception Exception exception and returns a generic class object specified

2.Callable interface multithreaded application scenarios

(1) When you want to get a parent thread running thread fruit

Step 3. Use Callable interface multithreaded

(1) The first step: create an instance of an object Callable subclass

(2) Step 2: Create FutureTask object and Callable object passed in the constructor's FutureTask

(Note: FutureTask implement the Runnable interface and Future interfaces)

 (3) Third step: Examples of the Thread object, and pass an object constructor FurureTask

 (4) The fourth step: starting a thread

Example 1 (using Callable interface thread):

Create a child thread class using the Callable Interface:

package com.my.frame;

import java.util.concurrent.Callable;

public class ThreadCall implements Callable<String> {

    @Override
    public String call() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("=====");
        return "9999";
    }


}
package com.my.frame;

import java.util.concurrent.FutureTask;

public class TestThread {
    public static void main(String[] args) {
        
        FutureTask<String> ft = new FutureTask<>(new ThreadCall());
        new Thread(ft).start();
    }

}

Example 2 (anonymous class category implement Callable interface to create a child thread):

Anonymous class category implement Callable interface to create a child thread class and implement:

package call;

 

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

//匿名类部类实现Callable接口创建子线程

public class AnonyCallable {

 

    public static void main(String[] args) {

    Callable<String> cl = new Callable<String>() {

     

    @Override

    public String call() throws Exception {

            System.out.println(Thread.currentThread().getName() + "正在行军~~~");

            System.out.println (. Thread.currentThread () getName () + "encounter enemy ~ ~ ~" ); 

            . System.out.println (Thread.currentThread () getName () + "bravely fighting the enemy !!!! " ); 

            return " battle victory, captured 50,000 enemy " ; 

            } 

 

}; 

        a FutureTask, <String>. ft = new new a FutureTask, (Cl); 

        new new the Thread (. ft," Transformation by force " ) .start (); 

the try { 

        Thread.currentThread () .setName ( "Cunxu forces" ); 

        Thread.sleep ( 3000 ); 

        System.out.println (. Thread.currentThread () getName () + "rest 3000ms" );

} catch(InterruptedException E) { 

    e.printStackTrace (); 

} 

    System.out.println (Thread.currentThread () getName (). + "Reorganization is completed, waiting friendly message ..." ); 

the try { 

            String STR = ft.get ( ); 

            System.out.println ( "Cunxu friendly forces that message:" + STR); 

} the catch (InterruptedException | ExecutionException E) { 

    e.printStackTrace (); 

    } 

} 
}

 

Guess you like

Origin www.cnblogs.com/lukelook/p/11128266.html