Callable interface return value

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/longgeqiaojie304/article/details/91648698

Callable interface return value

Manufacturers face questions:

1, you talk about the understanding of the volatile?

2, CAS Did you know?

3, ABA Atomic AtomicInteger class to talk about? Atomic update references know?

4, we all know that ArrayList is not thread-safe, please write an insecure coding case and gives the solution?

5, lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.

6, CountDownLatch, CyclicBarrier, Semaphore used it?

7, blocking queue know?

8 , the thread pool used it? ThreadPoolExecutor talk about your understanding?

9, the thread pool used it? Production How do you set reasonable parameters?

10, location analysis and coding deadlock?

 

Callable interface code verification return value

One Demo: a Callable get the return value on the last (futureTask.get ())

package com.wwl.juc;

 

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

import java.util.concurrent.TimeUnit;

 

/**

 * Today to demonstrate Callable way to create threads (Callable return value)

 */

class MyThreadByCallable implements Callable<Integer> {

 

    @Override

    public Integer call() throws Exception {

        System.out.println (. Thread.currentThread () getName () + "\ t to simulation");

        // Simulation Business

        TimeUnit.SECONDS.sleep(3);

        return 1024;

    }

 

}

/**

 * Three ways to create a thread way

 * 1 , inheritance Thread class

 * 2 , implement Runnable

 * 3 , using Callable <String> Interface

 * Today to demonstrate Callable way to create threads (Callable return value)

 */

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<Integer> futureTask = new FutureTask<>(new MyThreadByCallable());

        Thread thread = new Thread(futureTask, "t1");

        thread.start();

        // Callable return value if acquired on here, it will block the main thread computing, concurrency can affect the efficiency

//        Integer t1Result = futureTask.get();

        // main thread simulation

        Integer mainResult = 1000;

        System.out.println ( "main thread has been calculated, mainResult =" + mainResult);

 

 

        // Callable return value to obtain the best for last, or if t1 computing time is too long, it will block the main thread computing

        Integer t1Result = futureTask.get();

        System.out.println ( "t1 thread has been calculated, t1Result =" + t1Result);

        System.out.println ( "is calculated as the sum of two threads:" + (mainResult + t1Result));

    }

}

Program execution results are as follows: main thread is first calculated, then the thread is finished before computing t1


Two Demo: a Callable return value obtained on the front main thread (futureTask.get ())

package com.wwl.juc;

 

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.FutureTask;

import java.util.concurrent.TimeUnit;

 

/**

 * Today to demonstrate Callable way to create threads (Callable return value)

 */

class MyThreadByCallable implements Callable<Integer> {

 

    @Override

    public Integer call() throws Exception {

        System.out.println (. Thread.currentThread () getName () + "\ t to simulation");

        // Simulation Business

        TimeUnit.SECONDS.sleep(3);

        return 1024;

    }

 

}

/**

 * Three ways to create a thread way

 * 1 , inheritance Thread class

 * 2 , implement Runnable

 * 3 , using Callable <String> Interface

 * Today to demonstrate Callable way to create threads (Callable return value)

 */

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<Integer> futureTask = new FutureTask<>(new MyThreadByCallable());

        Thread thread = new Thread(futureTask, "t1");

        thread.start();

        // Callable return value if acquired on here, it will block the main thread computing, concurrency can affect the efficiency

Integer t1Result = futureTask.get();

        // main thread simulation

        Integer mainResult = 1000;

        System.out.println ( "main thread has been calculated, mainResult =" + mainResult);

 

 

        // Callable return value to obtain the best for last, or if t1 computing time is too long, it will block the main thread computing

        //Integer t1Result = futureTask.get();

        System.out.println ( "t1 thread has been calculated, t1Result =" + t1Result);

        System.out.println ( "is calculated as the sum of two threads:" + (mainResult + t1Result));

    }

}

Program execution results are as follows: t1 thread takes longer to compute, they blocked the main thread computing

Guess you like

Origin blog.csdn.net/longgeqiaojie304/article/details/91648698