Java并发(1) 基本的线程机制

来源:互联网 发布:网络域名怎么注册 编辑:程序博客网 时间:2024/05/02 01:50

1. 基本的线程机制

1.1 定义任务

         线程可以驱动任务, 因此需要一种描述任务的方式, 可以由Runnable接口来提供.  实现Runnable接口并编写run方法, 就能够定义一个任务.
//: concurrency/LiftOff.java// Demonstration of the Runnable interface.public class LiftOff implements Runnable {  protected int countDown = 10; // Default  private static int taskCount = 0;  private final int id = taskCount++;  public LiftOff() {}  public LiftOff(int countDown) {    this.countDown = countDown;  }  public String status() {    return "#" + id + "(" +      (countDown > 0 ? countDown : "Liftoff!") + "), ";  }  public void run() {    while(countDown-- > 0) {      System.out.print(status());      Thread.yield();    }  }} ///:~
        Thread.yield()是对线程调度器的一种建议, 告诉它我已经完成了生命周期中最重要的部分了, 此刻应该把处理器给其他任务使用.

1.2 Thread类

        将Runnable对象转变为工作任务是把他作为参数传递给Thread的构造函数.
//: concurrency/BasicThreads.java// The most basic use of the Thread class.public class BasicThreads {  public static void main(String[] args) {    Thread t = new Thread(new LiftOff());    t.start();    System.out.println("Waiting for LiftOff");  }} /* Output: (90% match)Waiting for LiftOff#0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!),*///:~
        调用Thread的start()方法为该线程执行必须的初始化操作, 然后调用Runnable的run()方法, 以便在新线程中启动这个任务.start()方法会在调用后
        立即返回. 尽管没有任何对线程对象的引用, 直到它的run()方法执行结束, 它是不会被回收的. 因为每个Thread都会注册它自己, 实际上还是有对
        它的引用存在的.

1.3 使用Executor

        java.util.concurrent.Executor会帮助我们管理Thread对象, 它在客户端和执行任务间提供了一个中间层.
//: concurrency/CachedThreadPool.javaimport java.util.concurrent.*;public class CachedThreadPool {  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool();    for(int i = 0; i < 5; i++)      exec.execute(new LiftOff());    exec.shutdown();  }} /* Output: (Sample)#0(9), #0(8), #1(9), #2(9), #3(9), #4(9), #0(7), #1(8), #2(8), #3(8), #4(8), #0(6), #1(7), #2(7), #3(7), #4(7), #0(5), #1(6), #2(6), #3(6), #4(6), #0(4), #1(5), #2(5), #3(5), #4(5), #0(3), #1(4), #2(4), #3(4), #4(4), #0(2), #1(3), #2(3), #3(3), #4(3), #0(1), #1(2), #2(2), #3(2), #4(2), #0(Liftoff!), #1(1), #2(1), #3(1), #4(1), #1(Liftoff!), #2(Liftoff!), #3(Liftoff!), #4(Liftoff!),*///:~
        ChchedThreadPool为每个任务创建了一个线程, ExecutorService知道如何构建恰当的上下文来执行Runnable对象.
        调用shutdown方法, 能够防止新任务被提交给Executor.

        FixedThreadPool使用了有限的线程集来执行所提交的任务, 这样可以一次性预先执行线程的分配工作, 也可以限制线程的数量.

//: concurrency/FixedThreadPool.javaimport java.util.concurrent.*;public class FixedThreadPool {  public static void main(String[] args) {    // Constructor argument is number of threads:    ExecutorService exec = Executors.newFixedThreadPool(5);    for(int i = 0; i < 5; i++)      exec.execute(new LiftOff());    exec.shutdown();  }} /* Output: (Sample)#0(9), #0(8), #1(9), #2(9), #3(9), #4(9), #0(7), #1(8), #2(8), #3(8), #4(8), #0(6), #1(7), #2(7), #3(7), #4(7), #0(5), #1(6), #2(6), #3(6), #4(6), #0(4), #1(5), #2(5), #3(5), #4(5), #0(3), #1(4), #2(4), #3(4), #4(4), #0(2), #1(3), #2(3), #3(3), #4(3), #0(1), #1(2), #2(2), #3(2), #4(2), #0(Liftoff!), #1(1), #2(1), #3(1), #4(1), #1(Liftoff!), #2(Liftoff!), #3(Liftoff!), #4(Liftoff!),*///:~
        还有一种SingleThreadExecutor, 就像是线程数量为1的FixedThreadPool, 它非常适合在线程中执行短任务. 如果向它提交了多个任务, 那么这些任务将排队, 
        每个任务都会在下个任务执行开始之前结束. 所有的任务都使用相同的线程.
//: concurrency/SingleThreadExecutor.javaimport java.util.concurrent.*;public class SingleThreadExecutor {  public static void main(String[] args) {    ExecutorService exec =      Executors.newSingleThreadExecutor();    for(int i = 0; i < 5; i++)      exec.execute(new LiftOff());    exec.shutdown();  }} /* Output:#0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!), #1(9), #1(8), #1(7), #1(6), #1(5), #1(4), #1(3), #1(2), #1(1), #1(Liftoff!), #2(9), #2(8), #2(7), #2(6), #2(5), #2(4), #2(3), #2(2), #2(1), #2(Liftoff!), #3(9), #3(8), #3(7), #3(6), #3(5), #3(4), #3(3), #3(2), #3(1), #3(Liftoff!), #4(9), #4(8), #4(7), #4(6), #4(5), #4(4), #4(3), #4(2), #4(1), #4(Liftoff!),*///:~

1.4 从任务中产生返回值

       Runnable是执行工作的独立任务, 但它不返回任何值, 如果希望在任务完成时返回一个值, 可以考虑实现Callable接口. 它是一种具有类型参数的泛型, 它的类型参数
       是从call()方法返回的值, 并且必须使用ExecutorService.submit()方法调用它.
 
//: concurrency/CallableDemo.javaimport java.util.concurrent.*;import java.util.*;class TaskWithResult implements Callable<String> {  private int id;  public TaskWithResult(int id) {    this.id = id;  }  public String call() {    return "result of TaskWithResult " + id;  }}public class CallableDemo {  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool();    ArrayList<Future<String>> results =      new ArrayList<Future<String>>();    for(int i = 0; i < 10; i++)      results.add(exec.submit(new TaskWithResult(i)));    for(Future<String> fs : results)      try {        // get() blocks until completion:        System.out.println(fs.get());      } catch(InterruptedException e) {        System.out.println(e);        return;      } catch(ExecutionException e) {        System.out.println(e);      } finally {        exec.shutdown();      }  }} /* Output:result of TaskWithResult 0result of TaskWithResult 1result of TaskWithResult 2result of TaskWithResult 3result of TaskWithResult 4result of TaskWithResult 5result of TaskWithResult 6result of TaskWithResult 7result of TaskWithResult 8result of TaskWithResult 9*///:~
        submit()方法会产生Future对象, 任务完成时, Future对象有一个结果, 可以使用get()方法获取该结果, 如果结果还没有产生, 那么get()方法将阻塞, 
        直到结果准备就绪.

1.5 休眠

        调用sleep()方法使任务中止给定的时间.
//: concurrency/SleepingTask.java// Calling sleep() to pause for a while.import java.util.concurrent.*;public class SleepingTask extends LiftOff {  public void run() {    try {      while(countDown-- > 0) {        System.out.print(status());        // Old-style:        // Thread.sleep(100);        // Java SE5/6-style:        TimeUnit.MILLISECONDS.sleep(100);      }    } catch(InterruptedException e) {      System.err.println("Interrupted");    }  }  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool();    for(int i = 0; i < 5; i++)      exec.execute(new SleepingTask());    exec.shutdown();  }} /* Output:#0(9), #1(9), #2(9), #3(9), #4(9), #0(8), #1(8), #2(8), #3(8), #4(8), #0(7), #1(7), #2(7), #3(7), #4(7), #0(6), #1(6), #2(6), #3(6), #4(6), #0(5), #1(5), #2(5), #3(5), #4(5), #0(4), #1(4), #2(4), #3(4), #4(4), #0(3), #1(3), #2(3), #3(3), #4(3), #0(2), #1(2), #2(2), #3(2), #4(2), #0(1), #1(1), #2(1), #3(1), #4(1), #0(Liftoff!), #1(Liftoff!), #2(Liftoff!), #3(Liftoff!), #4(Liftoff!),*///:~
        对sleep的调用可能跑出InterruptedException, 它在run中被捕获, 因为异常不能跨线程传播到main, 所以必须在本地处理所有任务内部的异常.
        注意:不能依赖线程调度器来安排线程的执行顺序, 最好的办法是使用同步控制.

1.6 优先级

        调度器根据线程的优先级来安排执行顺序, 但并不意味着优先级低的线程不能执行, 只是执行的频率较低. 所有线程都应该以默认的优先级运行, 通常
        试图操纵线程的优先级都是错误的做法.
//: concurrency/SimplePriorities.java// Shows the use of thread priorities.import java.util.concurrent.*;public class SimplePriorities implements Runnable {  private int countDown = 5;  private volatile double d; // No optimization  private int priority;  public SimplePriorities(int priority) {    this.priority = priority;  }  public String toString() {    return Thread.currentThread() + ": " + countDown;  }  public void run() {    Thread.currentThread().setPriority(priority);    while(true) {      // An expensive, interruptable operation:      for(int i = 1; i < 100000; i++) {        d += (Math.PI + Math.E) / (double)i;        if(i % 1000 == 0)          Thread.yield();      }      System.out.println(this);      if(--countDown == 0) return;    }  }  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool();    for(int i = 0; i < 5; i++)      exec.execute(        new SimplePriorities(Thread.MIN_PRIORITY));    exec.execute(        new SimplePriorities(Thread.MAX_PRIORITY));    exec.shutdown();  }} /* Output: (70% match)Thread[pool-1-thread-6,10,main]: 5Thread[pool-1-thread-6,10,main]: 4Thread[pool-1-thread-6,10,main]: 3Thread[pool-1-thread-6,10,main]: 2Thread[pool-1-thread-6,10,main]: 1Thread[pool-1-thread-3,1,main]: 5Thread[pool-1-thread-2,1,main]: 5Thread[pool-1-thread-1,1,main]: 5Thread[pool-1-thread-5,1,main]: 5Thread[pool-1-thread-4,1,main]: 5...*///:~
        JDK中定义的优先级与多数操作系统都不能映射得很好, 唯一可移植的方法是当调整优先级的时候使用MAX_PRIORITY, NORM_PRIORITY和MIN_PRIORITY.

1.7 让步

        Thread.yield()暗示线程调度器, 可以让其他线程使用CPU了, 但没有任何机制保证它会被采纳,调用它时, 也是在建议具有相同优先级的线程可以运行了.

1.8 后台线程

        Daemon线程指在程序允许过程中在后台提供通用服务的线程, 并且这种线程不是程序中不可或缺的部分. 当所有的非后台线程结束时, 程序也就终止了.
//: concurrency/SimpleDaemons.java// Daemon threads don't prevent the program from ending.import java.util.concurrent.*;import static net.mindview.util.Print.*;public class SimpleDaemons implements Runnable {  public void run() {    try {      while(true) {        TimeUnit.MILLISECONDS.sleep(100);        print(Thread.currentThread() + " " + this);      }    } catch(InterruptedException e) {      print("sleep() interrupted");    }  }  public static void main(String[] args) throws Exception {    for(int i = 0; i < 10; i++) {      Thread daemon = new Thread(new SimpleDaemons());      daemon.setDaemon(true); // Must call before start()      daemon.start();    }    print("All daemons started");    TimeUnit.MILLISECONDS.sleep(175);  }} /* Output: (Sample)All daemons startedThread[Thread-0,5,main] SimpleDaemons@530daaThread[Thread-1,5,main] SimpleDaemons@a62fc3Thread[Thread-2,5,main] SimpleDaemons@89ae9eThread[Thread-3,5,main] SimpleDaemons@1270b73Thread[Thread-4,5,main] SimpleDaemons@60aeb0Thread[Thread-5,5,main] SimpleDaemons@16caf43Thread[Thread-6,5,main] SimpleDaemons@66848cThread[Thread-7,5,main] SimpleDaemons@8813f2Thread[Thread-8,5,main] SimpleDaemons@1d58aaeThread[Thread-9,5,main] SimpleDaemons@83cc67...*///:~
        一旦main完成了它的工作, 程序就终止了.

        通过编写定制的ThreadFactory可以定制由Executor创建的线程的属性.
//: net/mindview/util/DaemonThreadFactory.javapackage net.mindview.util;import java.util.concurrent.*;public class DaemonThreadFactory implements ThreadFactory {  public Thread newThread(Runnable r) {    Thread t = new Thread(r);    t.setDaemon(true);    return t;  }} ///:~
        然后可以使用这个类作为参数传递给Executor.newCachedThreadPool()
//: concurrency/DaemonFromFactory.java// Using a Thread Factory to create daemons.import java.util.concurrent.*;import net.mindview.util.*;import static net.mindview.util.Print.*;public class DaemonFromFactory implements Runnable {  public void run() {    try {      while(true) {        TimeUnit.MILLISECONDS.sleep(100);        print(Thread.currentThread() + " " + this);      }    } catch(InterruptedException e) {      print("Interrupted");    }  }  public static void main(String[] args) throws Exception {    ExecutorService exec = Executors.newCachedThreadPool(      new DaemonThreadFactory());    for(int i = 0; i < 10; i++)      exec.execute(new DaemonFromFactory());    print("All daemons started");    TimeUnit.MILLISECONDS.sleep(500); // Run for a while  }} /* (Execute to see output) *///:~
        一个后台线程创建的任何线程都是后台线程.
//: concurrency/Daemons.java// Daemon threads spawn other daemon threads.import java.util.concurrent.*;import static net.mindview.util.Print.*;class Daemon implements Runnable {  private Thread[] t = new Thread[10];  public void run() {    for(int i = 0; i < t.length; i++) {      t[i] = new Thread(new DaemonSpawn());      t[i].start();      printnb("DaemonSpawn " + i + " started, ");    }    for(int i = 0; i < t.length; i++)      printnb("t[" + i + "].isDaemon() = " +        t[i].isDaemon() + ", ");    while(true)      Thread.yield();  }}class DaemonSpawn implements Runnable {  public void run() {    while(true)      Thread.yield();  }}public class Daemons {  public static void main(String[] args) throws Exception {    Thread d = new Thread(new Daemon());    d.setDaemon(true);    d.start();    printnb("d.isDaemon() = " + d.isDaemon() + ", ");    // Allow the daemon threads to    // finish their startup processes:    TimeUnit.SECONDS.sleep(1);  }} /* Output: (Sample)d.isDaemon() = true, DaemonSpawn 0 started, DaemonSpawn 1 started, DaemonSpawn 2 started, DaemonSpawn 3 started, DaemonSpawn 4 started, DaemonSpawn 5 started, DaemonSpawn 6 started, DaemonSpawn 7 started, DaemonSpawn 8 started, DaemonSpawn 9 started, t[0].isDaemon() = true, t[1].isDaemon() = true, t[2].isDaemon() = true, t[3].isDaemon() = true, t[4].isDaemon() = true, t[5].isDaemon() = true, t[6].isDaemon() = true, t[7].isDaemon() = true, t[8].isDaemon() = true, t[9].isDaemon() = true,*///:~

1.10 术语

        Thread类本身不执行任何操作, 它只负责驱动赋予它的任务.

1.11加入一个线程

        一个线程可以在其他线程之上调用join()方法, 效果是, 等待一段时间直到第二个线程结束后才继续执行. 也可以在调用join()时带超时参数, 如果目标线程在这段时间
        还没有结束, join()方法总能返回. 如果在调用线程上调用interrupt()方法, 则对join()的调用将被中断.
//: concurrency/Joining.java// Understanding join().import static net.mindview.util.Print.*;class Sleeper extends Thread {  private int duration;  public Sleeper(String name, int sleepTime) {    super(name);    duration = sleepTime;    start();  }  public void run() {    try {      sleep(duration);    } catch(InterruptedException e) {      print(getName() + " was interrupted. " +        "isInterrupted(): " + isInterrupted());      return;    }    print(getName() + " has awakened");  }}class Joiner extends Thread {  private Sleeper sleeper;  public Joiner(String name, Sleeper sleeper) {    super(name);    this.sleeper = sleeper;    start();  }  public void run() {   try {      sleeper.join();    } catch(InterruptedException e) {      print("Interrupted");    }    print(getName() + " join completed");  }}public class Joining {  public static void main(String[] args) {    Sleeper      sleepy = new Sleeper("Sleepy", 1500),      grumpy = new Sleeper("Grumpy", 1500);    Joiner      dopey = new Joiner("Dopey", sleepy),      doc = new Joiner("Doc", grumpy);    grumpy.interrupt();  }} /* Output:Grumpy was interrupted. isInterrupted(): falseDoc join completedSleepy has awakenedDopey join completed*///:~
       java.util.concurrent.CyclicBarrier可能比join更加适合.

1.13 捕获异常

        一旦异常逃出任务的run()方法, 他就会向外传播到控制台, 为了解决这个问题, 需要修改Executor产生线程的方式. Thead.UncaughtExcepitonHandler允许在
       每个Thread对象附着一个异常处理器.Thead.UncaughtExcepitonHandler.uncaughtException()会在线程因未捕获异常而临近死亡之前被调用.
//: concurrency/CaptureUncaughtException.javaimport java.util.concurrent.*;class ExceptionThread2 implements Runnable {  public void run() {    Thread t = Thread.currentThread();    System.out.println("run() by " + t);    System.out.println(      "eh = " + t.getUncaughtExceptionHandler());    throw new RuntimeException();  }}class MyUncaughtExceptionHandler implementsThread.UncaughtExceptionHandler {  public void uncaughtException(Thread t, Throwable e) {    System.out.println("caught " + e);  }}class HandlerThreadFactory implements ThreadFactory {  public Thread newThread(Runnable r) {    System.out.println(this + " creating new Thread");    Thread t = new Thread(r);    System.out.println("created " + t);    t.setUncaughtExceptionHandler(      new MyUncaughtExceptionHandler());    System.out.println(      "eh = " + t.getUncaughtExceptionHandler());    return t;  }}public class CaptureUncaughtException {  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool(      new HandlerThreadFactory());    exec.execute(new ExceptionThread2());  }} /* Output: (90% match)HandlerThreadFactory@de6ced creating new Threadcreated Thread[Thread-0,5,main]eh = MyUncaughtExceptionHandler@1fb8ee3run() by Thread[Thread-0,5,main]eh = MyUncaughtExceptionHandler@1fb8ee3caught java.lang.RuntimeException*///:~


   










0 0