Java -- 并发

  并发是用于多处理器编程的基本工具。Java的线程机制是抢占式的,这表示调度机制会周期性地中断线程,将上下文切换到另一个线程,从而为每个线程都提供时间片,使得每个线程都会分配到数量合理的时间去驱动他的任务。
  并发程序使我们可以将程序划分为多个分离的、独立运行的任务。通过多线程机制,这些独立任务中的每一个都将由执行线程来驱动。在使用线程时,CPU将轮流给每个任务分配其占用时间。每个任务都觉得自己一直占用CPU,但事实上CPU时间是划分成片段分配给了所有任务。

1、基本的线程机制
1.1 Thread类
public class LiftOffThread extends Thread {
   
    protected int countDown = 10;
    private static int taskCount = 0;
    private final int id = taskCount++;

    public LiftOffThread () {
    }

    @Override
    public void run() {
   
        while (countDown-- > 0) {
   
            System.out.print("#" + id + " (" + (countDown > 0 ? countDown : "LiftOff!") + "), ");
            Thread.yield();
        }
    }

    public static void main(String[] args) {
   
        LiftOffThread t = new LiftOffThread ();
        t.start();
        System.out.println("waiting for liftoff!");
    }
}
1.2 Runnable接口

  想要定义任务,只需要实现Runnable接口并实现编写run()方法,使得该任务可以执行命令,并将Runnbale对象提交给Thread构造器。

public class LiftOffRunnable implements Runnable {
   
    protected int countDown = 10;
    private static int taskCount = 0;
    private final int id = taskCount++;

    public LiftOffRunnable () {
    }

    @Override
    public void run() {
   
        while (countDown-- > 0) {
   
            System.out.print("#" + id + " (" + (countDown > 0 ? countDown : "LiftOff!") + "), ");
            Thread.yield();
        }
    }

    public static void main(String[] args) {
   
        Thread t = new Thread(new LiftOffRunnable ());
        t.start();
        System.out.println("waiting for liftoff!");
        // LiftOffRunnable liftOff = new LiftOffRunnable();
        // liftOff.run();
    }
}

运行结果:

waiting for liftoff!
#0 (9), #0 (8), #0 (7), #0 (6), #0 (5), #0 (4), #0 (3), #0 (2), #0 (1), #0 (LiftOff!), 

  标识符id可以区分任务的多个实例,因为是final的,一旦初始化之后不能修改。Thread.yiled()可以将CPU从一个线程转移给另一个线程。

1.3 Executor

  java.util.concurrent包中的执行器(Executor)将为你管理Thread对象,从而简化了并发编程。使用线程池的优点有:重用存在的线程,减少对象创建、消亡的开销;可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞;提供定时执行、定期执行、单线程、并发数控制等功能。

    public static void main(String[] args) {
   
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i=0 ; i<5 ; i++){
   
            executorService.execute(new LiftOffRunnable());
        }
        executorService.shutdown();
    }

运行结果:

#0 (9), #1 (9), #1 (8), #0 (8), #1 (7), #1 (6), #0 (7), #1 (5), #1 (4), 
#1 (3), #1 (2), #1 (1), #1 (LiftOff!), #0 (6), #0 (5), #0 (4), #0 (3), 
#0 (2), #0 (1), #0 (LiftOff!), #4 (9), #4 (8), #4 (7), #4 (6), #3 (9), 
#2 (9), #3 (8), #2 (8), #3 (7), #2 (7), #3 (6), #2 (6), #3 (5), #2 (5), 
#3 (4), #2 (4), #3 (3), #2 (3), #3 (2), #2 (2), #3 (1), #2 (1), #3 (LiftOff!), 
#2 (LiftOff!), #4 (5), #4 (4), #4 (3), #4 (2), #4 (1), #4 (LiftOff!), 

  shutdown()方法的调用可以防止新任务被提交给这个Executor。
  CachedThreadPool()在程序执行过程中通常会创建与所需数量相同的线程,然后再它回收旧线程时停止创建新线程,因此是合理的Executor首选。只有当这种方式会引发问题时,才需要切换至FixedThreadPool。
Executors提供四种线程池:
  1)newFixedThreadPool:创建固定数目线程的线程池。
  2)newCachedThreadPool:创建一个可缓存的线程池,调用execute将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有60秒钟未被使用的线程。
  3)newSingleThreadExecutor:创建一个单线程化的Executor。
  4)newScheduledThreadPool:创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。

1.4 Callable接口–从任务中产生返回值

  如果希望任务完成时能够返回一个值,那么可以实现Callable接口的call()方法,且必须使用ExecutorService.submit()方法调用它。submit()方法会产生Future对象,使用get()方法可以获取结果,如果Future没有执行完成,get()方法将阻塞,直至结果准备就绪。

public class TaskWithResult implements Callable<String> {
   
    private int id;

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

    @Override
    public String call() throws Exception {
   
        return "result of TaskWithResult " + id;
    }

    public static void main(String[] args) {
   
        ExecutorService executorService = Executors.newCachedThreadPool();
        ArrayList<Future<String>> results = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
   
            results.add(executorService.submit(new TaskWithResult(i)));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值