多线程基础

实现多线程的四种方式

  1. 继承Thread类
  2. 实现Runnable接口
  3. 实现Callable接口
  4. 使用线程池

常用方法

run(),start(),currentThread(),getName(),
yield()(放弃cpu的当前执行权),
wait(),notify()/notifyAll(),join()
sleep(),isAlive(),stop()(已过时)

优先级

MAX_PRIORITY:10(Thread.MAX_PRIORITY)
MIN_PRIORITY:1
NORM_PRIORITY:5

线程的同步(线程的安全问题)

  1. 操作共享数据的代码,即为被同步的代码。
  2. 共享数据:多个线程抢夺的资源
  3. 同步监视器,俗称:锁。

Callable接口

  1. 实现类实现Callable接口

  2. 将实现类作为参数创建FutureTask

  3. 将FutureTask对象作为参数创建Thread并启动线程

    代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;

    public class CallableTest {

    public static void main(String[] args) {

    NumSum ns = new NumSum();
    FutureTask f = new FutureTask(ns);

    new Thread(f).start();
    Object object;
    try {
    object = f.get();
    System.out.println(object);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (ExecutionException e) {
    e.printStackTrace();
    }

    }
    }

    class NumSum implements Callable{
    int sum = 0;
    @Override
    public Object call() throws Exception {
    for(int i = 0 ; i<50 ; i++) {
    sum++;
    }
    return sum;
    }
    }

    结果:
    callable接口

    线程池

  4. JDK5.0提供线程池相关API:ExecutorService(接口)和Excutors(工具类)

  5. 常用方法

    • Executors.newFixedThreadPool(int i)创建固定数量的线程池。

    • excute()执行,无返回值

    • submit()执行,可以有返回值

    • shutdown()关闭线程池

      代码:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      import java.util.concurrent.Callable;
      import java.util.concurrent.ExecutionException;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;
      import java.util.concurrent.Future;
      import java.util.concurrent.FutureTask;

      public class ThreadPool {

      public static void main(String[] args) {
      MyTest myTest = new MyTest();
      ExecutorService pool = Executors.newFixedThreadPool(10);
      pool.execute(new MyThread());
      Future submit = pool.submit(myTest);
      Object object;
      try {
      object = submit.get();
      System.out.println(object);
      } catch (InterruptedException e) {
      e.printStackTrace();
      } catch (ExecutionException e) {
      e.printStackTrace();
      }
      //关闭连接池
      pool.isShutdown();
      }
      }
      class MyTest implements Callable{
      int m = 0;
      @Override
      public Object call() throws Exception {
      for (int i = 0; i < 50; i++) {
      m++;
      }
      return m;
      }
      }
      class MyThread implements Runnable{
      @Override
      public void run() {

      for (int i = 0; i < 5; i++) {
      System.out.println(i);
      }
      }
      }

      结果:
      线程池

设置线程池属性

corePoolSize:核心池大小
maximumPoolSize:最大线程数
keepAliveTime:无任务终止时间数