Java中的多线程

发布于:2024-08-09 ⋅ 阅读:(56) ⋅ 点赞:(0)

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。