★18.并發

定義線程類

方式一:Runnable

1. 實現Runnable接口

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

    private String status() {
        return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "), ";
    }

    public void run() {
        while (countDown-- > 0) {
            System.out.print(status());
            Thread.yield();
        }
    }
}

2. 使用Thread類

public class BasicThreads {
    public static void main(String[] args) {
        Thread t1 = new Thread(new LiftOff());
        t1.start();
        Thread t2 = new Thread(new LiftOff());
        t2.start();
        System.out.println("Waiting for LiftOff");
    }
}

方式二:Thread

  • 缺點:繼承Thread導致無法繼承其他類,而實現Runnable接口的方式還能繼承其他類

    代碼

public class SimpleThread extends Thread {
    private int countDown = 5;
    private static int threadCount = 0;

    private SimpleThread() {
        super(Integer.toString(++threadCount));
        start();
    }

    public String toString() { return "#" + getName() + "(" + countDown + "), "; }

    public void run() {
        while (true) {
            System.out.print(this);
            if (--countDown == 9) return;
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new SimpleThread();
        }
    }
}

方式三:Executors

創建無限線程的線程池

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();
    }
}

創建有限線程的線程池

public class FixedThreadPool {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++)
            exec.execute(new LiftOff());
        exec.shutdown();
    }
}

創建單一線程

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();
    }
}

方式三:Callable

  • 相比Runnable接口,Callable接口可以從線程中返回一個值,這個值通過Future對象包裝
class TaskWithResult implements Callable<String> {
    private int id;
    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<>();
        for (int i = 0; i < 10; i++) {
            results.add(exec.submit(new TaskWithResult(i)));
        }
        for (Future<String> fs : results) {
            try {
                // get會阻塞程序
                System.out.println(fs.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            } catch (ExecutionException e) {
                e.printStackTrace();
            } finally {
                exec.shutdown();
            }
        }
    }
}

休眠

方式一

Thread.sleep(100);

方式二

TimeUnit.MILLISECONDS.sleep(100);

線程優先級

public class SimplePriorities implements Runnable {
    private int countDown = 5;
    private int priority;

    private SimplePriorities(int priority) {
        this.priority = priority;
    }

    public void run() {
        // 設置優先級
        Thread.currentThread().setPriority(priority);
        while (true) {
            for (int i = 1; i < 100000; i++) {
                if (i % 1000 == 0) {
                    Thread.yield();
                }
            }
            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();
    }
}

后臺線程

描述

  • 當程序所有非后臺線程中止時,后臺線程會被殺死。
  • 通常后臺線程程序中不是不可或缺的部分。
  • 后臺線程創建的線程也是后臺線程。

簡單示例

public class SimpleDaemons implements Runnable {
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] arms) throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread daemon = new Thread(new SimpleDaemons());
            // 設置此線程為后臺線程
            daemon.setDaemon(true);
            daemon.start();
        }
        TimeUnit.MILLISECONDS.sleep(175);
    }
}

ThreadFactory

// 通過實現ThreadFactory接口來實現創建線程時的預處理
class DaemonThreadFactory implements ThreadFactory {
    public Thread newThread(@NotNull Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(true);
        return t;
    }
}

public class DaemonThread implements Runnable {
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
            }
        } catch (InterruptedException e) {
            System.out.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 DaemonThread());
        TimeUnit.MILLISECONDS.sleep(500);
    }
}

線程交互

  • 有線程A和線程B,若在線程A中調用B.join(),則線程A會在此處掛起直至線程B結束。
  • 有線程A和線程B,若在線程A中調用B.interrupt(),則線程B會中斷并拋出異常。

異常處理器

class ExceptionThread implements Runnable {
    public void run() {
        Thread t = Thread.currentThread();
        throw new RuntimeException();
    }
}

// 實現接口Thread.UncaughtExceptionHandler作為異常處理器
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("ViAritcPtiln'e");

    }
}

class HandlerThreadFactory implements ThreadFactory {
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        // 每個線程都綁定一個異常處理器
        t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
        return t;
    }
}

public class CaptureUncaughtException {
    public static void main(String[] args) {
        // 設置默認線程處理器
        // Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
        ExecutorService exec = Executors.newCachedThreadPool(new HandlerThreadFactory());
        exec.execute(new ExceptionThread());
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容