三种实现线程“异步”

这篇博客探讨了Java中三种实现线程异步的方法:使用join()等待线程结束,wait()和notify()进行线程间通信,以及Callable接口配合FutureTask获取返回结果。Callable能直接返回处理结果或抛出异常,调用FutureTask.get()会阻塞主线程直到得到结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

join()

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        ThreadCase t  = new ThreadCase();
        
        t.start();
        System.out.println("start...");
        long before = System.currentTimeMillis();
        t.join();
        
        long after = System.currentTimeMillis();
        System.out.println("total is: " + t.count + " cost: " + (after-before) + " ms");
    }
    
    static class ThreadCase extends Thread{
        
        int count;
        
        public void run() {
            
            for(int i=0; i<10; i++) {
                count += i;
            }
            System.out.println("caculate completely.");
        }
    }

wait(), notify

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        ThreadCase t  = new ThreadCase();
        
        t.start();
        
        long before = System.currentTimeMillis();
        synchronized(t) {
            
            System.out.println("start...");
            
            t.wait();
            
        }
        
        long after = System.currentTimeMillis();
        System.out.println("total is: " + t.count + " cost: " + (after-before) + " ms");
    }
    
    static class ThreadCase extends Thread{
        
        int count;
        
        public void run() {
            
            synchronized(this) {
                
                for(int i=0; i<10; i++) {
                    count += i;
                }
                System.out.println("caculate completely.");
                notify();
            }
        }
    }

callable

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // TODO Auto-generated method stub
        
        CallableCase c = new CallableCase();
        
        FutureTask<Integer> task = new FutureTask<>(c);
        
        new Thread(task).start();
        long before = System.currentTimeMillis();
        System.out.println("waiting for calculating...");
        
        Integer result = task.get();
        long after = System.currentTimeMillis();
        System.out.println("total is: " + result + " cost: " + (after-before) + " ms");
        
    }
    
    static class CallableCase implements java.util.concurrent.Callable<Integer> {

        int count = 0;

        @Override
        public Integer call() throws Exception {
            // TODO Auto-generated method stub
            
            for(int i=0; i<10; i++) {
                count += i;
            }
            
            System.out.println("calculate completely");
            
            return Integer.valueOf(count);
        }
    }

Callable 与 Runnable

callable可以直接返回处理结果或抛出异常,runnable自己消化异常

callable,调用FutureTask.get()来获取返回结果,此时会阻塞主线程来获取“将来”的结果,否则不会阻塞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值