侧边栏壁纸
博主头像
DJ's Blog博主等级

行动起来,活在当下

  • 累计撰写 133 篇文章
  • 累计创建 51 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

【Java】实现多线程方式

Administrator
2022-04-04 / 0 评论 / 0 点赞 / 71 阅读 / 8062 字

【Java】Java实现多线程方式

继承Thread

public class ThreadWithExtends extends Thread {
    private String name;
    public ThreadWithExtends() {
    }
    public ThreadWithExtends(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        // 获取当前线程的名称
        String threadName = Thread.currentThread().getName();
        System.out.println(threadName + "线程的run方法被调用……");
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(random.nextInt(10) * 100);
                System.out.println(threadName + "...." + name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ThreadWithExtends threadWithExtends1 = new ThreadWithExtends("a");
        ThreadWithExtends threadWithExtends2 = new ThreadWithExtends("b");
        // 开启一个新的线程,执行run()方法
        threadWithExtends1.start();
        threadWithExtends2.start();
        // 只执行主线程中的run()方法
        //threadWithExtends1.run();
        //threadWithExtends2.run(); }
    }
}

一定要使用start()方法来开启一个新的线程,然后自动执行run()方法。切不可直接调用实例的run()方法,因为这样只是一个普通的方法调用,不会开启新的线程。

实现Runnable接口

public class ThreadPoolWithRunnableTest implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Thread currentThread = Thread.currentThread();
            System.out.println("thread name: " + currentThread.getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ThreadPoolWithRunnableTest test = new ThreadPoolWithRunnableTest();
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            cachedThreadPool.execute(test);
        }
    }
}

实现Callable接口

public class ThreadPoolWithCallableTest implements Callable<String> {
    private int flag;
    public ThreadPoolWithCallableTest() {
    }
    public ThreadPoolWithCallableTest(int flag) {
        this.flag = flag;
    }
    @Override
    public String call() throws Exception {
        Thread currentThread = Thread.currentThread();
        System.out.println(currentThread.getName() + "正在执行" + this.getFlag() + "任务");
        Thread.sleep(5000);
        return "当前线程名称:" + currentThread.getName();
    }
    public int getFlag() {
        return flag;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolWithCallableTest test = new ThreadPoolWithCallableTest();
        int cpuNums = Runtime.getRuntime().availableProcessors();
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(cpuNums);
        for (int i = 0; i < 5; i++) {
            test.setFlag(i);
            Future<String> future = fixedThreadPool.submit(test);
            // 从Future中get结果,这个方法是会被阻塞的,一直要等到线程任务返回结果
            String result = future.get();
            System.out.println(result);
        }
        fixedThreadPool.shutdown();
    }
}

Callable方式跟Runnable方式的区别:

  1. Runnablerun()方法不会有任何返回结果,所以主线程无法获得任务线程的返回值。
  2. Callablecall()方法可以返回结果,但是主线程在获取时是被阻塞,需要等待任务线程返回结果之后才能拿到结果。

代码示例

实现了Runnable接口的任务

public class TaskRunnable implements Runnable {
    private int flag;
    public TaskRunnable() {
    }
    public TaskRunnable(int flag) {
        this.flag = flag;
    }
    public int getFlag() {
        return flag;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
    @Override
    public void run() {
        Thread currentThread = Thread.currentThread();
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentThread.getName() + " 启动时间:" + currentTimeMillis / 1000);
        System.out.println(currentThread.getName() + " is working..." + flag);
        try {
            Thread.sleep(5 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(currentThread.getName() + " finished working..." + flag);
    }
}

实现Callable接口的任务

public class TaskCallable implements Callable<String> {
    private int flag;
    public TaskCallable() {
    }
    public TaskCallable(int flag) {
        this.flag = flag;
    }
    public int getFlag() {
        return flag;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
    @Override
    public String call() throws Exception {
        Thread currentThread = Thread.currentThread();
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentThread.getName() + " 启动时间:" + currentTimeMillis / 1000);
        System.out.println(currentThread.getName() + " is working..." + getFlag());
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(currentThread.getName() + " finished working..." + getFlag());
        return getFlag() + "";
    }
}

线程池使用测试类

public class ThreadPoolTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        // 记录各线程的返回结果
        ArrayList<Future<?>> results = new ArrayList<>();
        int cpuNums = Runtime.getRuntime().availableProcessors();
        // 创建固定数量线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(cpuNums);
        // 创建调度线程池
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(cpuNums);
        for (int i = 0; i < 9; i++) {
            // 情况1:FixedThreadPool线程池处理Runnable任务
            //Future<?> runnableSubmit = fixedThreadPool.submit(new TaskRunnable(i));
            //results.add(runnableSubmit);

            // 情况2:FixedThreadPool线程池处理Callable任务
            //Future<String> callableSubmit = fixedThreadPool.submit(new TaskCallable(i));
            //results.add(callableSubmit);

            // 情况3:ScheduledThreadPool线程池处理Callable任务
            //Future<String> callableSubmit = scheduledThreadPool.submit(new TaskCallable(i));
            //results.add(callableSubmit);
            // 任务提交之后,延迟5s执行。
            Future<String> callableSubmit = scheduledThreadPool.schedule(new TaskCallable(i), 5, TimeUnit.SECONDS);
            results.add(callableSubmit);
        }
        Thread.sleep(10 * 1000);
        // 打印执行结果
        System.out.println("开始逐个获取future结果:");
        int i = 1;
        for (Future f : results) {
            String done = f.isDone() ? "已完成" : "未完成";
            System.out.println("第" + i + "个线程" + done);
            // 从结果的打印顺序可以看到,即使未完成,也会阻塞等待
            System.out.println("第" + i + "个线程返回future结果: " + f.get());
            i++;
        }
        System.out.println("所有任务都执行完毕");
        // 阻止新来的任务提交,对已经提交了的任务不会产生任何影响。当已经提交的任务执行完后,它会将那些闲置的线程(idleWorks)进行中断。
        scheduledThreadPool.shutdown();
    }
}
0

评论区