Thread_如何给run方法传参_如何实现处理线程的返回值



如何给run方法传参


1.构造函数传参


2.成员变量传参


3.回调函数传参



如何实现处理线程的返回


1.主线程等待法:缺点代码臃肿


package com.interview.javabasic.thread;

public class CycleWait implements Runnable{
    private String value;
    public void run() {
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        value = "we have data now";
    }

    public static void main(String[] args) throws InterruptedException {
        CycleWait cw = new CycleWait();
        Thread t = new Thread(cw);
        t.start();
       while (cw.value == null){
           Thread.currentThread().sleep(100);///主线程等待法
       }
 
        System.out.println("value : " + cw.value);
    }
}


2.thread类中join()阻塞当前线程以等待子线程处理完毕:缺点是粒度不够细致


package com.interview.javabasic.thread;

public class CycleWait implements Runnable{
    private String value;
    public void run() {
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        value = "we have data now";
    }

    public static void main(String[] args) throws InterruptedException {
        CycleWait cw = new CycleWait();
        Thread t = new Thread(cw);
        t.start();

        t.join();
        System.out.println("value : " + cw.value);
    }
}


3.通过callable接口进行实现,通过futureTask类Or线程池获取:


package com.interview.javabasic.thread;

import java.util.concurrent.Callable;

public class MyCallable implements Callable {
    @Override
    public String call() throws Exception{
        String value="test";
        System.out.println("Ready to work");
        Thread.currentThread().sleep(5000);
        System.out.println("task done");
        return  value;
    }

}
 


package com.interview.javabasic.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask task = new FutureTask(new MyCallable());
        new Thread(task).start();
        if(!task.isDone()){//线程未执行完成,执行以下代码
            System.out.println("task has not finished, please wait!");
        }
        System.out.println("task return: " + task.get());//task.get()会等待线程执行完成后执行

    }
}


4.线程池获取返回值:可以提交多个MyCallable,并发处理结果


package com.interview.javabasic.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();//创建线程池
        Future future = newCachedThreadPool.submit(new MyCallable());//线程池中提交MyCallable
        if(!future.isDone()){//判断任务是否完成
            System.out.println("task has not finished, please wait!");
        }
        try {
            System.out.println(future.get());//等待线程执行完成后完成
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            newCachedThreadPool.shutdown();
        }
    }
}


 

你可能感兴趣的:(Thread)