Java中的多线程

Java中的多线程

1. 实现多线程的几种方式

1-1. 继承 Thread 类,重写run方法,使用start方法启动。

public class Test {
    static class MyThread extends Thread {
        public void run() {
            System.out.println("MyThread Running...");
        }
    }
    public static void main(String[] args) {
        TestThread.MyThread myThread = new TestThread.MyThread();
        // 启动线程池
        myThread.start();
        System.out.println("MainThread Running...");
    }
}

1-2. 实现 Runnable 接口。

public class TestRunnable {
    static class MyRunnable implements Runnable {
        public void run() {
            System.out.println("Runnable Running...");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
        System.out.println("MainThread Running...");
    }
}

1-3. 实现 Callable 接口, FutureTask接收返回值。

public class TestCallable {
    static class MyCallable implements Callable<String> {
        public String call() throws Exception {
            System.out.println("Callable Running...");
            return "Callable Result...";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable callable = new MyCallable();
        FutureTask<String> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println("MainThread Running...");
        System.out.println("Result: " + futureTask.get());

    }
}

1-4. ExecutorService线程池。

public class TestExecutor {
    public static Integer count = 0;
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 1000; i++){
            executor.submit(() -> {
                count++;
                System.out.println("Now Count Is:" + count);
            });
        }
        // 等待所有任务完成
        executor.shutdown();
        // 判断是否还有线程未终止
        while (!executor.isTerminated()) {
        }
        System.out.println(count);

    }
}

2. 举例

以继承Thread为例,实现并验证多线程

public class TestThread {
    public static Integer count = 0;
    public static Integer max_count = 0;
    static class MyThread extends Thread {
        public void run() {
            System.out.println("MyThread Running:");
            for (int i = 0; i < 10000; i++) {
                count++;
                System.out.println("MyThread Count Now Value: " + count);
                max_count = Math.max(max_count, count);
            }
        }
    }
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        for (int i = 0; i < 10000; i++) {
            count++;
            System.out.println("MainThread Count Now Value: " + count);
            max_count = Math.max(max_count, count);
        }
        System.out.println(max_count);
    }
}

多运行几遍,就会发现count的值不一定是20000,就是因为多线程的情况下,可能会同时取数,同时+1。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值