ubuntu

Ubuntu Java多线程实现方式

小樊
44
2025-03-20 11:10:59
栏目: 编程语言
Ubuntu服务器限时活动,0元免费领! 查看>>

在Ubuntu上使用Java实现多线程,主要有以下几种方式:

1. 继承Thread

通过继承Thread类并重写run()方法来实现多线程。

class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " - " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();
        thread1.start();
        thread2.start();
    }
}

2. 实现Runnable接口

通过实现Runnable接口并将其传递给Thread对象来实现多线程。

class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " - " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        thread1.start();
        thread2.start();
    }
}

3. 使用ExecutorService

通过ExecutorService来管理线程池,可以更高效地管理线程。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyTask implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + " - " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(new MyTask());
        executorService.submit(new MyTask());
        executorService.shutdown();
    }
}

4. 使用ForkJoinPool

对于可以分解成更小任务的计算密集型任务,可以使用ForkJoinPool

import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;

class MyRecursiveTask extends RecursiveTask<Integer> {
    private int start;
    private int end;

    public MyRecursiveTask(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        if (end - start <= 10) {
            int sum = 0;
            for (int i = start; i < end; i++) {
                sum += i;
            }
            return sum;
        } else {
            int mid = (start + end) / 2;
            MyRecursiveTask leftTask = new MyRecursiveTask(start, mid);
            MyRecursiveTask rightTask = new MyRecursiveTask(mid, end);
            leftTask.fork();
            int rightResult = rightTask.compute();
            int leftResult = leftTask.join();
            return leftResult + rightResult;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        MyRecursiveTask task = new MyRecursiveTask(0, 100);
        int result = forkJoinPool.invoke(task);
        System.out.println("Sum: " + result);
    }
}

总结

选择合适的方式取决于具体的应用场景和需求。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:Ubuntu Java如何实现多线程

0
看了该问题的人还看了