java多線程實現方式

一.繼承Thread類重寫run方法

public class MultiThread extends  Thread{
    @Override
    public void run() {
        System.out.println("繼承thread");
    }

    public static void main(String[] args) {
        MultiThread thread=new MultiThread();
        thread.start();
    }
}

 

二.實現Runnable接口,實現run方法

​
public class RunnableThread implements Runnable {
    @Override
    public void run() {
        System.out.println("實現runnable接口");
    }

    public static void main(String[] args) {
        RunnableThread runnableThread = new RunnableThread();
        Thread thread=new Thread(runnableThread);
        thread.start();
    }
}

​

三.實現callable,可以獲取異常和返回值

public class MyCallable implements Callable{
    int i=0;
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"call:"+i);
        i++;
        if(i==3){
            throw new Exception("3異常");
        }
        return i;
    }

    public static void main(String[] args) {
        MyCallable myCallable=new MyCallable();
        for(int i=0;i<10;i++){
            FutureTask task=new FutureTask(myCallable);
            new Thread(task,"子線程:"+i).start();
            try {
                System.out.println(task.get() + ",");
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
    }

 

四.executor框架

實現類ThreadPoolExecutor

用CompletionService.submit提交callable,通過completionServe.take().get()可以拿到返回值

用threadpoolexecutor創建線程池

public class MyExecutor {
    public static int clientTotal=200;
    public static int count=0;
    public static  int threadTotal=10;
    public static void main(String[] args) throws InterruptedException {
        final Semaphore semaphore=new Semaphore(threadTotal);
        final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);
        ExecutorService executorService=Executors.newCachedThreadPool();
        CompletionService completionService = new ExecutorCompletionService(executorService);
        for(int i=0;i<clientTotal;i++){
            completionService.submit(new Callable() {
                @Override
                public Object call() throws Exception {
                    semaphore.acquire();
                    count++;
                    semaphore.release();
                    countDownLatch.countDown();
                    return count;
                }
            });
//            executorService.execute(()->{
//                try {
//                    semaphore.acquire();
//                    count++;
//                    semaphore.release();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                countDownLatch.countDown();;
//            });
        }
        countDownLatch.await();
        for(int i=0;i<clientTotal;i++){
            try {
                System.out.println(completionService.take().get());
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println(count);
        executorService.shutdown();
    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章