Java多线程创建的三种方式

2018-07-07  本文已影响0人  BHPam

创建线程

public class MyThread extends Thread{
    @override
    public void run(){
        //do something
    }
}

//调用
MyThread myThread = new MyThread();
myThread.run();
public class MyThread implements Runnable{
    @override
    public void run(){
        //do something
    }
}

//调用
MyThread myThread = new MyThread();
new Thread(myThread).start();

//Lambda style
new Thread(()->{
    //do something 
    return xxx;
}
).start();
public class MyThread<Integer> implements Callable<Integer>{
    //may have constructor
    @override
    public Integer call(){
        //do something
        return xxx;
    }
}
/*******************非线程池方式*************************/
//调用
Mythread myThread = new MyThread();
FutureTask<Integer> result = new FutureTask<>(myThread);
Thread thread = new Thread(result);
thread.start();
try{
    //get result
    Integer res = result.get();
}catch(InterruptedException|ExecutionException e){
    e.printStackTrack();
}

//Lambda style
new Thread(new FutureTask<Integer>((Callable<Integer>)()->{
    //do something 
    return xxx;
})
).start();
/**********************线程池方式**********************/

ExecutorService executor = Executors.newCachedThreadPool();
Future<Integer> result = executor.submit(new MyThread());

Runnable和Callable实现的区别

Future

Future能够异步保留执行结果,提供下面的几种方法来管理线程和县城结果

Future+Callable实现多线程计算实例

代码来源:https://www.cnblogs.com/MOBIN/p/6185387.html

public class FileSearchTask {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String path = args[0];
        String keyword = args[1];
        int c = 0;
        File[] files = new File(path).listFiles();
        ArrayList<Future<Integer>> rs = new ArrayList<>();
        for(File file: files){  //每个文件启动一个task去查找
            MatchCount count = new MatchCount();
            count.file = file;
            count.keyword = keyword;
            FutureTask<Integer> task = new FutureTask(count);
            rs.add(task); //将任务返回的结果添加到集合中
            Thread thread = new Thread(task);
            thread.start();
        }

        for(Future<Integer> f: rs){
            c += f.get(); //迭代返回结果并累加
        }
        System.out.println("包含关键字的总文件数为:" + c);
    }
}

public class  MatchCount implements Callable<Integer>{
    public File file;
    public String keyword;
    private  Integer count = 0;

    public Integer call() throws Exception {   //call封装线程所需做的任务
        if(search(file))
              count ++;
        return count;
    }

    public boolean search(File file){
        boolean founded = false;
        try(Scanner scanner = new Scanner(new FileInputStream(file))){
            while(!founded && scanner.hasNextLine()){
                if (scanner.nextLine().contains(keyword))
                    founded = true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return  founded;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读