Implement Callable interface

/** * Thread creation method 3: implement Callable interface. Creating multiple threads using the Callable interface is more powerful than using the Runnable interface. * 1.call () can have a return value. * 2. Call () can throw an exception, which is caught by an external operation. * 3
Create an implementation class that implements Callable
class NumThread implements Callable{
    //2. Implement the call method and declare the operation that this thread needs to perform in call()
    @Override
    public Object call(a) throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if(i % 2= =0){ System.out.println(i); sum += i; }}returnsum; }}public class ThreadNew {
    public static void main(String[] args) {
        //3. Create an object for the Callable interface implementation class
        NumThread numThread = new NumThread();
        //4. Pass the object of this Callable interface implementation class as a parameter to the FutureTask constructor to create the object of FutureTask
        FutureTask futureTask = new FutureTask(numThread);
        //5. Pass the FutureTask object as an argument to the Thread constructor, create the Thread object, and call start()
        new Thread(futureTask).start();

        try {
            //6. Get the return value of the call method in Callable
            The return value of get() is the return value of call() implemented by the FutureTask constructor Callable.
            Object sum = futureTask.get();
            System.out.println("The sum is:" + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch(ExecutionException e) { e.printStackTrace(); }}}Copy the code

Using thread pools

/** * Thread pool ** Benefits: * 1. Improved response time (reduced time to create new threads) * 2. Reduced resource consumption (reuse of threads in the thread pool, not need to be created every time) * 3. * corePoolSize: the size of the core pool * maximumPoolSize: the maximum number of threads * keepAliveTime: the maximum amount of time a thread can hold without a task before terminating * * * Four! * /

class NumberThread implements Runnable{

    @Override
    public void run(a) {
        for(int i = 0; i <=100; i++){if(i % 2= =0){
                System.out.println(Thread.currentThread().getName() + ":"+ i); }}}}class NumberThread1 implements Runnable{

    @Override
    public void run(a) {
        for(int i = 0; i <=100; i++){if(i % 2! =0){
                System.out.println(Thread.currentThread().getName() + ":"+ i); }}}}public class ThreadPool {

    public static void main(String[] args) {
        //1. Provide a thread pool with the specified number of threads
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
        // Set the properties of the thread pool
// System.out.println(service.getClass());
// service1.setCorePoolSize(15);
// service1.setKeepAliveTime();


        //2. Execute the operation of the specified thread. You need to provide objects that implement the Runnable or Callable interface implementation classes
        service.execute(new NumberThread());// Fit applies to Runnable
        service.execute(new NumberThread1());// Fit applies to Runnable

// service.submit(Callable callable); // Suitable for Callable
        //3. Disable the connection poolservice.shutdown(); }}Copy the code