0
点赞
收藏
分享

微信扫一扫

情报学基础教程简要笔记

 

序言

 问题

1 基础介绍

什么是多线程

什么是进程

2 Java多线程

1 继承Thread类

public class ThreadDemo {
    public static void main(String[] args) {
        // 创建10个线程并启动
        for (int i = 0; i < 10; i++) {
            MyThread thread = new MyThread(i);
            thread.start();
        }
    }
}

class MyThread extends Thread {
    private int id;

    public MyThread(int id) {
        this.id = id;
    }

    public void run() {
        System.out.println("Thread " + id + " is running");
        try {
            Thread.sleep(1000);  // 模拟任务执行时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class RunnableDemo {
    public static void main(String[] args) {
        // 创建10个线程并启动
        for (int i = 0; i < 10; i++) {
            Runnable task = new MyTask(i);
            Thread thread = new Thread(task);
            thread.start();
        }
    }
}

class MyTask implements Runnable {
    private int id;

    public MyTask(int id) {
        this.id = id;
    }

    public void run() {
        System.out.println("Thread " + id + " is running");
        try {
            Thread.sleep(1000);  // 模拟任务执行时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3 Executor框架 

代码讲解: 

Callable实现多线程

import java.util.concurrent.*;

public class CallableDemo {
    public static void main(String[] args) throws Exception {
        // 创建一个线程池
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // 提交10个Callable任务给线程池执行
        Future<Integer>[] results = new Future[10];
        for (int i = 0; i < 10; i++) {
            Callable<Integer> task = new MyTask(i);
            results[i] = executor.submit(task);
        }

        // 输出Callable任务的执行结果
        for (int i = 0; i < 10; i++) {
            Integer result = results[i].get();
            System.out.println("Task " + i + " result is " + result);
        }

        // 关闭线程池
        executor.shutdown();
    }
}

class MyTask implements Callable<Integer> {
    private int id;

    public MyTask(int id) {
        this.id = id;
    }

    public Integer call() throws Exception {
        System.out.println("Task " + id + " is running");
        Thread.sleep(1000);  // 模拟任务执行时间
        return id * 10;
    }
}

Future实现多线程

代码示例:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class FutureDemo {
    public static void main(String[] args) throws Exception {
        // 创建一个线程池
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // 提交10个Callable任务给线程池执行
        List<Future<Integer>> results = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Callable<Integer> task = new MyTask(i);
            Future<Integer> result = executor.submit(task);
            results.add(result);
        }

        // 输出Callable任务的执行结果
        for (int i = 0; i < 10; i++) {
            Integer result = results.get(i).get();
            System.out.println("Task " + i + " result is " + result);
        }

        // 关闭线程池
        executor.shutdown();
    }
}

class MyTask implements Callable<Integer> {
    private int id;

    public MyTask(int id) {
        this.id = id;
    }

    public Integer call() throws Exception {
        System.out.println("Task " + id + " is running");
        Thread.sleep(1000);  // 模拟任务执行时间
        return id * 10;
    }
}

示例讲解

线程池实现多线程

代码示例: 

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) throws Exception {
        // 创建一个包含10个线程的线程池
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // 提交10个任务给线程池执行,并记录每个任务的执行结果
        List<Future<Integer>> results = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Callable<Integer> task = new MyTask(i);
            Future<Integer> result = executor.submit(task);
            results.add(result);
        }

        // 等待所有任务执行完成
        executor.shutdown();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

        // 输出所有任务的执行结果
        int total = 0;
        for (int i = 0; i < 10; i++) {
            try {
                Integer result = results.get(i).get();
                System.out.println("Task " + i + " result is " + result);
                total += result;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                System.out.println("Task " + i + " execution error: " + e.getCause().getMessage());
            }
        }
        System.out.println("Total result is " + total);
    }
}

class MyTask implements Callable<Integer> {
    private int id;

    public MyTask(int id) {
        this.id = id;
    }

    public Integer call() throws Exception {
        System.out.println("Task " + id + " is running");
        Thread.sleep(2000);  // 模拟任务执行时间
        if (id % 2 == 0) {
            throw new RuntimeException("Task " + id + " execution error");
        }
        return id * 10;
    }
}

示例讲解:

总结

图书推荐

图书介绍 

内容介绍

卷| 与 卷|| 的区别

参与方式

中奖名单 

举报

相关推荐

0 条评论