Java 如何讓多線程按順序執行

範例1:使用Join的方式指定線程執行次序

package com.contoso;

public class MultiThreadMemo {

    public static void main(String args[]) throws InterruptedException {
        Thread1Memo t1 = new Thread1Memo();
        Thread2Memo t2 = new Thread2Memo();
        Thread3Memo t3 = new Thread3Memo();
        Thread4Memo t4 = new Thread4Memo();
        Thread5Memo t5 = new Thread5Memo();
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
        t4.start();
        t4.join();
        t5.start();
        t5.join();
        System.out.println("主線程:" + Thread.currentThread());
    }

    /*
    static Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < 101; i++) {
                System.out.println("loop" + i + " this is t1 threading.");
            }
        }
    });

    static Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < 101; i++) {
                System.out.println("loop" + i + " this is t2 threading.");
            }
        }
    });

    static Thread t3 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < 101; i++) {
                System.out.println("loop" + i + " this is t3 threading.");
            }
        }
    });

    static Thread t4 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < 101; i++) {
                System.out.println("loop" + i + " this is t4 threading.");
            }
        }
    });

    static Thread t5 = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < 101; i++) {
                System.out.println("loop" + i + " this is t5 threading.");
            }
        }
    });
     */
}
package com.contoso;

public class Thread1Memo extends Thread {

    @Override
    public void run() {
        for (int i = 1; i < 101; i++) {
            System.out.println("loop" + i + " this is t1 threading.");
        }
    }

}
package com.contoso;

public class Thread2Memo extends Thread {

    @Override
    public void run() {
        for (int i = 1; i < 101; i++) {
            System.out.println("loop" + i + " this is t2 threading.");
        }
    }

}
package com.contoso;

public class Thread3Memo extends Thread {

    @Override
    public void run() {
        for (int i = 1; i < 101; i++) {
            System.out.println("loop" + i + " this is t3 threading.");
        }
    }

}
package com.contoso;

public class Thread4Memo extends Thread {

    @Override
    public void run() {
        for (int i = 1; i < 101; i++) {
            System.out.println("loop" + i + " this is t4 threading.");
        }
    }

}
package com.contoso;

public class Thread5Memo extends Thread {

    @Override
    public void run() {
        for (int i = 1; i < 101; i++) {
            System.out.println("loop" + i + " this is t5 threading.");
        }
    }

}

控制檯打印輸出效果如下:

run:
loop1 this is t1 threading.
loop2 this is t1 threading.
loop3 this is t1 threading.
loop4 this is t1 threading.
loop5 this is t1 threading.
loop6 this is t1 threading.
loop7 this is t1 threading.
loop8 this is t1 threading.
loop9 this is t1 threading.
loop10 this is t1 threading.
loop11 this is t1 threading.
loop12 this is t1 threading.
loop13 this is t1 threading.
loop14 this is t1 threading.
loop15 this is t1 threading.
loop16 this is t1 threading.
loop17 this is t1 threading.
loop18 this is t1 threading.
loop19 this is t1 threading.
loop20 this is t1 threading.
loop21 this is t1 threading.
loop22 this is t1 threading.
loop23 this is t1 threading.
loop24 this is t1 threading.
loop25 this is t1 threading.
loop26 this is t1 threading.
loop27 this is t1 threading.
loop28 this is t1 threading.
loop29 this is t1 threading.
loop30 this is t1 threading.
loop31 this is t1 threading.
loop32 this is t1 threading.
loop33 this is t1 threading.
loop34 this is t1 threading.
loop35 this is t1 threading.
loop36 this is t1 threading.
loop37 this is t1 threading.
loop38 this is t1 threading.
loop39 this is t1 threading.
loop40 this is t1 threading.
loop41 this is t1 threading.
loop42 this is t1 threading.
loop43 this is t1 threading.
loop44 this is t1 threading.
loop45 this is t1 threading.
loop46 this is t1 threading.
loop47 this is t1 threading.
loop48 this is t1 threading.
loop49 this is t1 threading.
loop50 this is t1 threading.
loop51 this is t1 threading.
loop52 this is t1 threading.
loop53 this is t1 threading.
loop54 this is t1 threading.
loop55 this is t1 threading.
loop56 this is t1 threading.
loop57 this is t1 threading.
loop58 this is t1 threading.
loop59 this is t1 threading.
loop60 this is t1 threading.
loop61 this is t1 threading.
loop62 this is t1 threading.
loop63 this is t1 threading.
loop64 this is t1 threading.
loop65 this is t1 threading.
loop66 this is t1 threading.
loop67 this is t1 threading.
loop68 this is t1 threading.
loop69 this is t1 threading.
loop70 this is t1 threading.
loop71 this is t1 threading.
loop72 this is t1 threading.
loop73 this is t1 threading.
loop74 this is t1 threading.
loop75 this is t1 threading.
loop76 this is t1 threading.
loop77 this is t1 threading.
loop78 this is t1 threading.
loop79 this is t1 threading.
loop80 this is t1 threading.
loop81 this is t1 threading.
loop82 this is t1 threading.
loop83 this is t1 threading.
loop84 this is t1 threading.
loop85 this is t1 threading.
loop86 this is t1 threading.
loop87 this is t1 threading.
loop88 this is t1 threading.
loop89 this is t1 threading.
loop90 this is t1 threading.
loop91 this is t1 threading.
loop92 this is t1 threading.
loop93 this is t1 threading.
loop94 this is t1 threading.
loop95 this is t1 threading.
loop96 this is t1 threading.
loop97 this is t1 threading.
loop98 this is t1 threading.
loop99 this is t1 threading.
loop100 this is t1 threading.
loop1 this is t2 threading.
loop2 this is t2 threading.
loop3 this is t2 threading.
loop4 this is t2 threading.
loop5 this is t2 threading.
loop6 this is t2 threading.
loop7 this is t2 threading.
loop8 this is t2 threading.
loop9 this is t2 threading.
loop10 this is t2 threading.
loop11 this is t2 threading.
loop12 this is t2 threading.
loop13 this is t2 threading.
loop14 this is t2 threading.
loop15 this is t2 threading.
loop16 this is t2 threading.
loop17 this is t2 threading.
loop18 this is t2 threading.
loop19 this is t2 threading.
loop20 this is t2 threading.
loop21 this is t2 threading.
loop22 this is t2 threading.
loop23 this is t2 threading.
loop24 this is t2 threading.
loop25 this is t2 threading.
loop26 this is t2 threading.
loop27 this is t2 threading.
loop28 this is t2 threading.
loop29 this is t2 threading.
loop30 this is t2 threading.
loop31 this is t2 threading.
loop32 this is t2 threading.
loop33 this is t2 threading.
loop34 this is t2 threading.
loop35 this is t2 threading.
loop36 this is t2 threading.
loop37 this is t2 threading.
loop38 this is t2 threading.
loop39 this is t2 threading.
loop40 this is t2 threading.
loop41 this is t2 threading.
loop42 this is t2 threading.
loop43 this is t2 threading.
loop44 this is t2 threading.
loop45 this is t2 threading.
loop46 this is t2 threading.
loop47 this is t2 threading.
loop48 this is t2 threading.
loop49 this is t2 threading.
loop50 this is t2 threading.
loop51 this is t2 threading.
loop52 this is t2 threading.
loop53 this is t2 threading.
loop54 this is t2 threading.
loop55 this is t2 threading.
loop56 this is t2 threading.
loop57 this is t2 threading.
loop58 this is t2 threading.
loop59 this is t2 threading.
loop60 this is t2 threading.
loop61 this is t2 threading.
loop62 this is t2 threading.
loop63 this is t2 threading.
loop64 this is t2 threading.
loop65 this is t2 threading.
loop66 this is t2 threading.
loop67 this is t2 threading.
loop68 this is t2 threading.
loop69 this is t2 threading.
loop70 this is t2 threading.
loop71 this is t2 threading.
loop72 this is t2 threading.
loop73 this is t2 threading.
loop74 this is t2 threading.
loop75 this is t2 threading.
loop76 this is t2 threading.
loop77 this is t2 threading.
loop78 this is t2 threading.
loop79 this is t2 threading.
loop80 this is t2 threading.
loop81 this is t2 threading.
loop82 this is t2 threading.
loop83 this is t2 threading.
loop84 this is t2 threading.
loop85 this is t2 threading.
loop86 this is t2 threading.
loop87 this is t2 threading.
loop88 this is t2 threading.
loop89 this is t2 threading.
loop90 this is t2 threading.
loop91 this is t2 threading.
loop92 this is t2 threading.
loop93 this is t2 threading.
loop94 this is t2 threading.
loop95 this is t2 threading.
loop96 this is t2 threading.
loop97 this is t2 threading.
loop98 this is t2 threading.
loop99 this is t2 threading.
loop100 this is t2 threading.
loop1 this is t3 threading.
loop2 this is t3 threading.
loop3 this is t3 threading.
loop4 this is t3 threading.
loop5 this is t3 threading.
loop6 this is t3 threading.
loop7 this is t3 threading.
loop8 this is t3 threading.
loop9 this is t3 threading.
loop10 this is t3 threading.
loop11 this is t3 threading.
loop12 this is t3 threading.
loop13 this is t3 threading.
loop14 this is t3 threading.
loop15 this is t3 threading.
loop16 this is t3 threading.
loop17 this is t3 threading.
loop18 this is t3 threading.
loop19 this is t3 threading.
loop20 this is t3 threading.
loop21 this is t3 threading.
loop22 this is t3 threading.
loop23 this is t3 threading.
loop24 this is t3 threading.
loop25 this is t3 threading.
loop26 this is t3 threading.
loop27 this is t3 threading.
loop28 this is t3 threading.
loop29 this is t3 threading.
loop30 this is t3 threading.
loop31 this is t3 threading.
loop32 this is t3 threading.
loop33 this is t3 threading.
loop34 this is t3 threading.
loop35 this is t3 threading.
loop36 this is t3 threading.
loop37 this is t3 threading.
loop38 this is t3 threading.
loop39 this is t3 threading.
loop40 this is t3 threading.
loop41 this is t3 threading.
loop42 this is t3 threading.
loop43 this is t3 threading.
loop44 this is t3 threading.
loop45 this is t3 threading.
loop46 this is t3 threading.
loop47 this is t3 threading.
loop48 this is t3 threading.
loop49 this is t3 threading.
loop50 this is t3 threading.
loop51 this is t3 threading.
loop52 this is t3 threading.
loop53 this is t3 threading.
loop54 this is t3 threading.
loop55 this is t3 threading.
loop56 this is t3 threading.
loop57 this is t3 threading.
loop58 this is t3 threading.
loop59 this is t3 threading.
loop60 this is t3 threading.
loop61 this is t3 threading.
loop62 this is t3 threading.
loop63 this is t3 threading.
loop64 this is t3 threading.
loop65 this is t3 threading.
loop66 this is t3 threading.
loop67 this is t3 threading.
loop68 this is t3 threading.
loop69 this is t3 threading.
loop70 this is t3 threading.
loop71 this is t3 threading.
loop72 this is t3 threading.
loop73 this is t3 threading.
loop74 this is t3 threading.
loop75 this is t3 threading.
loop76 this is t3 threading.
loop77 this is t3 threading.
loop78 this is t3 threading.
loop79 this is t3 threading.
loop80 this is t3 threading.
loop81 this is t3 threading.
loop82 this is t3 threading.
loop83 this is t3 threading.
loop84 this is t3 threading.
loop85 this is t3 threading.
loop86 this is t3 threading.
loop87 this is t3 threading.
loop88 this is t3 threading.
loop89 this is t3 threading.
loop90 this is t3 threading.
loop91 this is t3 threading.
loop92 this is t3 threading.
loop93 this is t3 threading.
loop94 this is t3 threading.
loop95 this is t3 threading.
loop96 this is t3 threading.
loop97 this is t3 threading.
loop98 this is t3 threading.
loop99 this is t3 threading.
loop100 this is t3 threading.
loop1 this is t4 threading.
loop2 this is t4 threading.
loop3 this is t4 threading.
loop4 this is t4 threading.
loop5 this is t4 threading.
loop6 this is t4 threading.
loop7 this is t4 threading.
loop8 this is t4 threading.
loop9 this is t4 threading.
loop10 this is t4 threading.
loop11 this is t4 threading.
loop12 this is t4 threading.
loop13 this is t4 threading.
loop14 this is t4 threading.
loop15 this is t4 threading.
loop16 this is t4 threading.
loop17 this is t4 threading.
loop18 this is t4 threading.
loop19 this is t4 threading.
loop20 this is t4 threading.
loop21 this is t4 threading.
loop22 this is t4 threading.
loop23 this is t4 threading.
loop24 this is t4 threading.
loop25 this is t4 threading.
loop26 this is t4 threading.
loop27 this is t4 threading.
loop28 this is t4 threading.
loop29 this is t4 threading.
loop30 this is t4 threading.
loop31 this is t4 threading.
loop32 this is t4 threading.
loop33 this is t4 threading.
loop34 this is t4 threading.
loop35 this is t4 threading.
loop36 this is t4 threading.
loop37 this is t4 threading.
loop38 this is t4 threading.
loop39 this is t4 threading.
loop40 this is t4 threading.
loop41 this is t4 threading.
loop42 this is t4 threading.
loop43 this is t4 threading.
loop44 this is t4 threading.
loop45 this is t4 threading.
loop46 this is t4 threading.
loop47 this is t4 threading.
loop48 this is t4 threading.
loop49 this is t4 threading.
loop50 this is t4 threading.
loop51 this is t4 threading.
loop52 this is t4 threading.
loop53 this is t4 threading.
loop54 this is t4 threading.
loop55 this is t4 threading.
loop56 this is t4 threading.
loop57 this is t4 threading.
loop58 this is t4 threading.
loop59 this is t4 threading.
loop60 this is t4 threading.
loop61 this is t4 threading.
loop62 this is t4 threading.
loop63 this is t4 threading.
loop64 this is t4 threading.
loop65 this is t4 threading.
loop66 this is t4 threading.
loop67 this is t4 threading.
loop68 this is t4 threading.
loop69 this is t4 threading.
loop70 this is t4 threading.
loop71 this is t4 threading.
loop72 this is t4 threading.
loop73 this is t4 threading.
loop74 this is t4 threading.
loop75 this is t4 threading.
loop76 this is t4 threading.
loop77 this is t4 threading.
loop78 this is t4 threading.
loop79 this is t4 threading.
loop80 this is t4 threading.
loop81 this is t4 threading.
loop82 this is t4 threading.
loop83 this is t4 threading.
loop84 this is t4 threading.
loop85 this is t4 threading.
loop86 this is t4 threading.
loop87 this is t4 threading.
loop88 this is t4 threading.
loop89 this is t4 threading.
loop90 this is t4 threading.
loop91 this is t4 threading.
loop92 this is t4 threading.
loop93 this is t4 threading.
loop94 this is t4 threading.
loop95 this is t4 threading.
loop96 this is t4 threading.
loop97 this is t4 threading.
loop98 this is t4 threading.
loop99 this is t4 threading.
loop100 this is t4 threading.
loop1 this is t5 threading.
loop2 this is t5 threading.
loop3 this is t5 threading.
loop4 this is t5 threading.
loop5 this is t5 threading.
loop6 this is t5 threading.
loop7 this is t5 threading.
loop8 this is t5 threading.
loop9 this is t5 threading.
loop10 this is t5 threading.
loop11 this is t5 threading.
loop12 this is t5 threading.
loop13 this is t5 threading.
loop14 this is t5 threading.
loop15 this is t5 threading.
loop16 this is t5 threading.
loop17 this is t5 threading.
loop18 this is t5 threading.
loop19 this is t5 threading.
loop20 this is t5 threading.
loop21 this is t5 threading.
loop22 this is t5 threading.
loop23 this is t5 threading.
loop24 this is t5 threading.
loop25 this is t5 threading.
loop26 this is t5 threading.
loop27 this is t5 threading.
loop28 this is t5 threading.
loop29 this is t5 threading.
loop30 this is t5 threading.
loop31 this is t5 threading.
loop32 this is t5 threading.
loop33 this is t5 threading.
loop34 this is t5 threading.
loop35 this is t5 threading.
loop36 this is t5 threading.
loop37 this is t5 threading.
loop38 this is t5 threading.
loop39 this is t5 threading.
loop40 this is t5 threading.
loop41 this is t5 threading.
loop42 this is t5 threading.
loop43 this is t5 threading.
loop44 this is t5 threading.
loop45 this is t5 threading.
loop46 this is t5 threading.
loop47 this is t5 threading.
loop48 this is t5 threading.
loop49 this is t5 threading.
loop50 this is t5 threading.
loop51 this is t5 threading.
loop52 this is t5 threading.
loop53 this is t5 threading.
loop54 this is t5 threading.
loop55 this is t5 threading.
loop56 this is t5 threading.
loop57 this is t5 threading.
loop58 this is t5 threading.
loop59 this is t5 threading.
loop60 this is t5 threading.
loop61 this is t5 threading.
loop62 this is t5 threading.
loop63 this is t5 threading.
loop64 this is t5 threading.
loop65 this is t5 threading.
loop66 this is t5 threading.
loop67 this is t5 threading.
loop68 this is t5 threading.
loop69 this is t5 threading.
loop70 this is t5 threading.
loop71 this is t5 threading.
loop72 this is t5 threading.
loop73 this is t5 threading.
loop74 this is t5 threading.
loop75 this is t5 threading.
loop76 this is t5 threading.
loop77 this is t5 threading.
loop78 this is t5 threading.
loop79 this is t5 threading.
loop80 this is t5 threading.
loop81 this is t5 threading.
loop82 this is t5 threading.
loop83 this is t5 threading.
loop84 this is t5 threading.
loop85 this is t5 threading.
loop86 this is t5 threading.
loop87 this is t5 threading.
loop88 this is t5 threading.
loop89 this is t5 threading.
loop90 this is t5 threading.
loop91 this is t5 threading.
loop92 this is t5 threading.
loop93 this is t5 threading.
loop94 this is t5 threading.
loop95 this is t5 threading.
loop96 this is t5 threading.
loop97 this is t5 threading.
loop98 this is t5 threading.
loop99 this is t5 threading.
loop100 this is t5 threading.
主線程:Thread[main,5,main]
BUILD SUCCESSFUL (total time: 1 second)

範例2:使用即將執行完任務的子線程啓動新的子線程,以實現多個子線程的按指定的順序執行

package com.contoso;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MultiThreadMemo2 extends JFrame {

    private JPanel contentPane;
    private JProgressBar progressBar1;
    private JProgressBar progressBar2;
    private JButton btnStart;

    private static final int MIN_PROGRESS = 0;
    private static final int MAX_PROGRESS = 100;

    private static int currentProgress1 = MIN_PROGRESS;
    private static int currentProgress2 = MIN_PROGRESS;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MultiThreadMemo2 frame = new MultiThreadMemo2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MultiThreadMemo2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setBounds(0, 0, 400, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        setTitle("測試窗口");
        contentPane.setLayout(null);

        progressBar1 = new JProgressBar();
        progressBar1.setBounds(30, 50, 320, 23);
        progressBar1.setMinimum(MIN_PROGRESS);
        progressBar1.setMaximum(MAX_PROGRESS);
        progressBar1.setValue(currentProgress1);
        progressBar1.setStringPainted(true);
        contentPane.add(progressBar1);

        progressBar2 = new JProgressBar();
        progressBar2.setBounds(30, 80, 320, 23);
        progressBar2.setMinimum(MIN_PROGRESS);
        progressBar2.setMaximum(MAX_PROGRESS);
        progressBar2.setValue(currentProgress2);
        progressBar2.setStringPainted(true);
        contentPane.add(progressBar2);

        btnStart = new JButton("開始測試");
        btnStart.setBounds(160, 150, 120, 23);
        contentPane.add(btnStart);

        // 添加進度改變通知
        progressBar1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("progressBar1 當前進度值: " + progressBar1.getValue() + "; "
                        + "進度百分比: " + progressBar1.getPercentComplete());
            }
        });

        // 添加進度改變通知
        progressBar2.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("progressBar2 當前進度值: " + progressBar2.getValue() + "; "
                        + "進度百分比: " + progressBar2.getPercentComplete());
            }
        });

        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               

                Thread t2 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (currentProgress2 < 100) {
                            try {
                                currentProgress2++;
                                Thread.sleep(1000);
                                if (currentProgress2 > MAX_PROGRESS) {
                                    currentProgress2 = MIN_PROGRESS;

                                }
                                progressBar2.setValue(currentProgress2);
                            } catch (Exception e) {
                            }

                        }
                        currentProgress2 = 0;
                        btnStart.setEnabled(true);
                    }
                });

                Thread t1 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                         btnStart.setEnabled(false);
                         progressBar1.setValue(0);
                         progressBar2.setValue(0);
                        while (currentProgress1 < 100) {
                            try {
                                currentProgress1++;
                                Thread.sleep(1000);
                                if (currentProgress1 > MAX_PROGRESS) {
                                    currentProgress1 = MIN_PROGRESS;

                                }
                                progressBar1.setValue(currentProgress1);
                            } catch (Exception e) {
                            }
                            if (progressBar1.getValue() == 100) {
                                t2.start();
                            }
                        }
                        currentProgress1 = 0;
                    }
                });

                try {
                    t1.start();
                } catch (Exception e) {
                }
                
            }
        });

    }
}

Windows界面百分比進度條效果圖如下:

控制檯打印輸出效果如下:

run:
progressBar1 當前進度值: 1; 進度百分比: 0.01
progressBar1 當前進度值: 2; 進度百分比: 0.02
progressBar1 當前進度值: 3; 進度百分比: 0.03
progressBar1 當前進度值: 4; 進度百分比: 0.04
progressBar1 當前進度值: 5; 進度百分比: 0.05
progressBar1 當前進度值: 6; 進度百分比: 0.06
progressBar1 當前進度值: 7; 進度百分比: 0.07
progressBar1 當前進度值: 8; 進度百分比: 0.08
progressBar1 當前進度值: 9; 進度百分比: 0.09
progressBar1 當前進度值: 10; 進度百分比: 0.1
progressBar1 當前進度值: 11; 進度百分比: 0.11
progressBar1 當前進度值: 12; 進度百分比: 0.12
progressBar1 當前進度值: 13; 進度百分比: 0.13
progressBar1 當前進度值: 14; 進度百分比: 0.14
progressBar1 當前進度值: 15; 進度百分比: 0.15
progressBar1 當前進度值: 16; 進度百分比: 0.16
progressBar1 當前進度值: 17; 進度百分比: 0.17
progressBar1 當前進度值: 18; 進度百分比: 0.18
progressBar1 當前進度值: 19; 進度百分比: 0.19
progressBar1 當前進度值: 20; 進度百分比: 0.2
progressBar1 當前進度值: 21; 進度百分比: 0.21
progressBar1 當前進度值: 22; 進度百分比: 0.22
progressBar1 當前進度值: 23; 進度百分比: 0.23
progressBar1 當前進度值: 24; 進度百分比: 0.24
progressBar1 當前進度值: 25; 進度百分比: 0.25
progressBar1 當前進度值: 26; 進度百分比: 0.26
progressBar1 當前進度值: 27; 進度百分比: 0.27
progressBar1 當前進度值: 28; 進度百分比: 0.28
progressBar1 當前進度值: 29; 進度百分比: 0.29
progressBar1 當前進度值: 30; 進度百分比: 0.3
progressBar1 當前進度值: 31; 進度百分比: 0.31
progressBar1 當前進度值: 32; 進度百分比: 0.32
progressBar1 當前進度值: 33; 進度百分比: 0.33
progressBar1 當前進度值: 34; 進度百分比: 0.34
progressBar1 當前進度值: 35; 進度百分比: 0.35
progressBar1 當前進度值: 36; 進度百分比: 0.36
progressBar1 當前進度值: 37; 進度百分比: 0.37
progressBar1 當前進度值: 38; 進度百分比: 0.38
progressBar1 當前進度值: 39; 進度百分比: 0.39
progressBar1 當前進度值: 40; 進度百分比: 0.4
progressBar1 當前進度值: 41; 進度百分比: 0.41
progressBar1 當前進度值: 42; 進度百分比: 0.42
progressBar1 當前進度值: 43; 進度百分比: 0.43
progressBar1 當前進度值: 44; 進度百分比: 0.44
progressBar1 當前進度值: 45; 進度百分比: 0.45
progressBar1 當前進度值: 46; 進度百分比: 0.46
progressBar1 當前進度值: 47; 進度百分比: 0.47
progressBar1 當前進度值: 48; 進度百分比: 0.48
progressBar1 當前進度值: 49; 進度百分比: 0.49
progressBar1 當前進度值: 50; 進度百分比: 0.5
progressBar1 當前進度值: 51; 進度百分比: 0.51
progressBar1 當前進度值: 52; 進度百分比: 0.52
progressBar1 當前進度值: 53; 進度百分比: 0.53
progressBar1 當前進度值: 54; 進度百分比: 0.54
progressBar1 當前進度值: 55; 進度百分比: 0.55
progressBar1 當前進度值: 56; 進度百分比: 0.56
progressBar1 當前進度值: 57; 進度百分比: 0.57
progressBar1 當前進度值: 58; 進度百分比: 0.58
progressBar1 當前進度值: 59; 進度百分比: 0.59
progressBar1 當前進度值: 60; 進度百分比: 0.6
progressBar1 當前進度值: 61; 進度百分比: 0.61
progressBar1 當前進度值: 62; 進度百分比: 0.62
progressBar1 當前進度值: 63; 進度百分比: 0.63
progressBar1 當前進度值: 64; 進度百分比: 0.64
progressBar1 當前進度值: 65; 進度百分比: 0.65
progressBar1 當前進度值: 66; 進度百分比: 0.66
progressBar1 當前進度值: 67; 進度百分比: 0.67
progressBar1 當前進度值: 68; 進度百分比: 0.68
progressBar1 當前進度值: 69; 進度百分比: 0.69
progressBar1 當前進度值: 70; 進度百分比: 0.7
progressBar1 當前進度值: 71; 進度百分比: 0.71
progressBar1 當前進度值: 72; 進度百分比: 0.72
progressBar1 當前進度值: 73; 進度百分比: 0.73
progressBar1 當前進度值: 74; 進度百分比: 0.74
progressBar1 當前進度值: 75; 進度百分比: 0.75
progressBar1 當前進度值: 76; 進度百分比: 0.76
progressBar1 當前進度值: 77; 進度百分比: 0.77
progressBar1 當前進度值: 78; 進度百分比: 0.78
progressBar1 當前進度值: 79; 進度百分比: 0.79
progressBar1 當前進度值: 80; 進度百分比: 0.8
progressBar1 當前進度值: 81; 進度百分比: 0.81
progressBar1 當前進度值: 82; 進度百分比: 0.82
progressBar1 當前進度值: 83; 進度百分比: 0.83
progressBar1 當前進度值: 84; 進度百分比: 0.84
progressBar1 當前進度值: 85; 進度百分比: 0.85
progressBar1 當前進度值: 86; 進度百分比: 0.86
progressBar1 當前進度值: 87; 進度百分比: 0.87
progressBar1 當前進度值: 88; 進度百分比: 0.88
progressBar1 當前進度值: 89; 進度百分比: 0.89
progressBar1 當前進度值: 90; 進度百分比: 0.9
progressBar1 當前進度值: 91; 進度百分比: 0.91
progressBar1 當前進度值: 92; 進度百分比: 0.92
progressBar1 當前進度值: 93; 進度百分比: 0.93
progressBar1 當前進度值: 94; 進度百分比: 0.94
progressBar1 當前進度值: 95; 進度百分比: 0.95
progressBar1 當前進度值: 96; 進度百分比: 0.96
progressBar1 當前進度值: 97; 進度百分比: 0.97
progressBar1 當前進度值: 98; 進度百分比: 0.98
progressBar1 當前進度值: 99; 進度百分比: 0.99
progressBar1 當前進度值: 100; 進度百分比: 1.0
progressBar2 當前進度值: 1; 進度百分比: 0.01
progressBar2 當前進度值: 2; 進度百分比: 0.02
progressBar2 當前進度值: 3; 進度百分比: 0.03
progressBar2 當前進度值: 4; 進度百分比: 0.04
progressBar2 當前進度值: 5; 進度百分比: 0.05
progressBar2 當前進度值: 6; 進度百分比: 0.06
progressBar2 當前進度值: 7; 進度百分比: 0.07
progressBar2 當前進度值: 8; 進度百分比: 0.08
progressBar2 當前進度值: 9; 進度百分比: 0.09
progressBar2 當前進度值: 10; 進度百分比: 0.1
progressBar2 當前進度值: 11; 進度百分比: 0.11
progressBar2 當前進度值: 12; 進度百分比: 0.12
progressBar2 當前進度值: 13; 進度百分比: 0.13
progressBar2 當前進度值: 14; 進度百分比: 0.14
progressBar2 當前進度值: 15; 進度百分比: 0.15
progressBar2 當前進度值: 16; 進度百分比: 0.16
progressBar2 當前進度值: 17; 進度百分比: 0.17
progressBar2 當前進度值: 18; 進度百分比: 0.18
progressBar2 當前進度值: 19; 進度百分比: 0.19
progressBar2 當前進度值: 20; 進度百分比: 0.2
progressBar2 當前進度值: 21; 進度百分比: 0.21
progressBar2 當前進度值: 22; 進度百分比: 0.22
progressBar2 當前進度值: 23; 進度百分比: 0.23
progressBar2 當前進度值: 24; 進度百分比: 0.24
progressBar2 當前進度值: 25; 進度百分比: 0.25
progressBar2 當前進度值: 26; 進度百分比: 0.26
progressBar2 當前進度值: 27; 進度百分比: 0.27
progressBar2 當前進度值: 28; 進度百分比: 0.28
progressBar2 當前進度值: 29; 進度百分比: 0.29
progressBar2 當前進度值: 30; 進度百分比: 0.3
progressBar2 當前進度值: 31; 進度百分比: 0.31
progressBar2 當前進度值: 32; 進度百分比: 0.32
progressBar2 當前進度值: 33; 進度百分比: 0.33
progressBar2 當前進度值: 34; 進度百分比: 0.34
progressBar2 當前進度值: 35; 進度百分比: 0.35
progressBar2 當前進度值: 36; 進度百分比: 0.36
progressBar2 當前進度值: 37; 進度百分比: 0.37
progressBar2 當前進度值: 38; 進度百分比: 0.38
progressBar2 當前進度值: 39; 進度百分比: 0.39
progressBar2 當前進度值: 40; 進度百分比: 0.4
progressBar2 當前進度值: 41; 進度百分比: 0.41
progressBar2 當前進度值: 42; 進度百分比: 0.42
progressBar2 當前進度值: 43; 進度百分比: 0.43
progressBar2 當前進度值: 44; 進度百分比: 0.44
progressBar2 當前進度值: 45; 進度百分比: 0.45
progressBar2 當前進度值: 46; 進度百分比: 0.46
progressBar2 當前進度值: 47; 進度百分比: 0.47
progressBar2 當前進度值: 48; 進度百分比: 0.48
progressBar2 當前進度值: 49; 進度百分比: 0.49
progressBar2 當前進度值: 50; 進度百分比: 0.5
progressBar2 當前進度值: 51; 進度百分比: 0.51
progressBar2 當前進度值: 52; 進度百分比: 0.52
progressBar2 當前進度值: 53; 進度百分比: 0.53
progressBar2 當前進度值: 54; 進度百分比: 0.54
progressBar2 當前進度值: 55; 進度百分比: 0.55
progressBar2 當前進度值: 56; 進度百分比: 0.56
progressBar2 當前進度值: 57; 進度百分比: 0.57
progressBar2 當前進度值: 58; 進度百分比: 0.58
progressBar2 當前進度值: 59; 進度百分比: 0.59
progressBar2 當前進度值: 60; 進度百分比: 0.6
progressBar2 當前進度值: 61; 進度百分比: 0.61
progressBar2 當前進度值: 62; 進度百分比: 0.62
progressBar2 當前進度值: 63; 進度百分比: 0.63
progressBar2 當前進度值: 64; 進度百分比: 0.64
progressBar2 當前進度值: 65; 進度百分比: 0.65
progressBar2 當前進度值: 66; 進度百分比: 0.66
progressBar2 當前進度值: 67; 進度百分比: 0.67
progressBar2 當前進度值: 68; 進度百分比: 0.68
progressBar2 當前進度值: 69; 進度百分比: 0.69
progressBar2 當前進度值: 70; 進度百分比: 0.7
progressBar2 當前進度值: 71; 進度百分比: 0.71
progressBar2 當前進度值: 72; 進度百分比: 0.72
progressBar2 當前進度值: 73; 進度百分比: 0.73
progressBar2 當前進度值: 74; 進度百分比: 0.74
progressBar2 當前進度值: 75; 進度百分比: 0.75
progressBar2 當前進度值: 76; 進度百分比: 0.76
progressBar2 當前進度值: 77; 進度百分比: 0.77
progressBar2 當前進度值: 78; 進度百分比: 0.78
progressBar2 當前進度值: 79; 進度百分比: 0.79
progressBar2 當前進度值: 80; 進度百分比: 0.8
progressBar2 當前進度值: 81; 進度百分比: 0.81
progressBar2 當前進度值: 82; 進度百分比: 0.82
progressBar2 當前進度值: 83; 進度百分比: 0.83
progressBar2 當前進度值: 84; 進度百分比: 0.84
progressBar2 當前進度值: 85; 進度百分比: 0.85
progressBar2 當前進度值: 86; 進度百分比: 0.86
progressBar2 當前進度值: 87; 進度百分比: 0.87
progressBar2 當前進度值: 88; 進度百分比: 0.88
progressBar2 當前進度值: 89; 進度百分比: 0.89
progressBar2 當前進度值: 90; 進度百分比: 0.9
progressBar2 當前進度值: 91; 進度百分比: 0.91
progressBar2 當前進度值: 92; 進度百分比: 0.92
progressBar2 當前進度值: 93; 進度百分比: 0.93
progressBar2 當前進度值: 94; 進度百分比: 0.94
progressBar2 當前進度值: 95; 進度百分比: 0.95
progressBar2 當前進度值: 96; 進度百分比: 0.96
progressBar2 當前進度值: 97; 進度百分比: 0.97
progressBar2 當前進度值: 98; 進度百分比: 0.98
progressBar2 當前進度值: 99; 進度百分比: 0.99
progressBar2 當前進度值: 100; 進度百分比: 1.0
progressBar1 當前進度值: 0; 進度百分比: 0.0
progressBar2 當前進度值: 0; 進度百分比: 0.0
progressBar1 當前進度值: 1; 進度百分比: 0.01
progressBar1 當前進度值: 2; 進度百分比: 0.02
progressBar1 當前進度值: 3; 進度百分比: 0.03
progressBar1 當前進度值: 4; 進度百分比: 0.04
progressBar1 當前進度值: 5; 進度百分比: 0.05
progressBar1 當前進度值: 6; 進度百分比: 0.06
progressBar1 當前進度值: 7; 進度百分比: 0.07
progressBar1 當前進度值: 8; 進度百分比: 0.08
progressBar1 當前進度值: 9; 進度百分比: 0.09
progressBar1 當前進度值: 10; 進度百分比: 0.1
progressBar1 當前進度值: 11; 進度百分比: 0.11
progressBar1 當前進度值: 12; 進度百分比: 0.12
progressBar1 當前進度值: 13; 進度百分比: 0.13
progressBar1 當前進度值: 14; 進度百分比: 0.14
progressBar1 當前進度值: 15; 進度百分比: 0.15
progressBar1 當前進度值: 16; 進度百分比: 0.16
progressBar1 當前進度值: 17; 進度百分比: 0.17
progressBar1 當前進度值: 18; 進度百分比: 0.18
progressBar1 當前進度值: 19; 進度百分比: 0.19
progressBar1 當前進度值: 20; 進度百分比: 0.2
progressBar1 當前進度值: 21; 進度百分比: 0.21
progressBar1 當前進度值: 22; 進度百分比: 0.22
progressBar1 當前進度值: 23; 進度百分比: 0.23
progressBar1 當前進度值: 24; 進度百分比: 0.24
progressBar1 當前進度值: 25; 進度百分比: 0.25
progressBar1 當前進度值: 26; 進度百分比: 0.26
progressBar1 當前進度值: 27; 進度百分比: 0.27
progressBar1 當前進度值: 28; 進度百分比: 0.28
progressBar1 當前進度值: 29; 進度百分比: 0.29
progressBar1 當前進度值: 30; 進度百分比: 0.3
progressBar1 當前進度值: 31; 進度百分比: 0.31
progressBar1 當前進度值: 32; 進度百分比: 0.32
progressBar1 當前進度值: 33; 進度百分比: 0.33
progressBar1 當前進度值: 34; 進度百分比: 0.34
progressBar1 當前進度值: 35; 進度百分比: 0.35
progressBar1 當前進度值: 36; 進度百分比: 0.36
progressBar1 當前進度值: 37; 進度百分比: 0.37
progressBar1 當前進度值: 38; 進度百分比: 0.38
progressBar1 當前進度值: 39; 進度百分比: 0.39
progressBar1 當前進度值: 40; 進度百分比: 0.4
progressBar1 當前進度值: 41; 進度百分比: 0.41
progressBar1 當前進度值: 42; 進度百分比: 0.42
progressBar1 當前進度值: 43; 進度百分比: 0.43
progressBar1 當前進度值: 44; 進度百分比: 0.44
progressBar1 當前進度值: 45; 進度百分比: 0.45
progressBar1 當前進度值: 46; 進度百分比: 0.46
progressBar1 當前進度值: 47; 進度百分比: 0.47
progressBar1 當前進度值: 48; 進度百分比: 0.48
progressBar1 當前進度值: 49; 進度百分比: 0.49
progressBar1 當前進度值: 50; 進度百分比: 0.5
progressBar1 當前進度值: 51; 進度百分比: 0.51
progressBar1 當前進度值: 52; 進度百分比: 0.52
progressBar1 當前進度值: 53; 進度百分比: 0.53
progressBar1 當前進度值: 54; 進度百分比: 0.54
progressBar1 當前進度值: 55; 進度百分比: 0.55
progressBar1 當前進度值: 56; 進度百分比: 0.56
progressBar1 當前進度值: 57; 進度百分比: 0.57
progressBar1 當前進度值: 58; 進度百分比: 0.58
progressBar1 當前進度值: 59; 進度百分比: 0.59
progressBar1 當前進度值: 60; 進度百分比: 0.6
progressBar1 當前進度值: 61; 進度百分比: 0.61
progressBar1 當前進度值: 62; 進度百分比: 0.62
progressBar1 當前進度值: 63; 進度百分比: 0.63
progressBar1 當前進度值: 64; 進度百分比: 0.64
progressBar1 當前進度值: 65; 進度百分比: 0.65
progressBar1 當前進度值: 66; 進度百分比: 0.66
progressBar1 當前進度值: 67; 進度百分比: 0.67
progressBar1 當前進度值: 68; 進度百分比: 0.68
progressBar1 當前進度值: 69; 進度百分比: 0.69
progressBar1 當前進度值: 70; 進度百分比: 0.7
progressBar1 當前進度值: 71; 進度百分比: 0.71
progressBar1 當前進度值: 72; 進度百分比: 0.72
progressBar1 當前進度值: 73; 進度百分比: 0.73
progressBar1 當前進度值: 74; 進度百分比: 0.74
progressBar1 當前進度值: 75; 進度百分比: 0.75
progressBar1 當前進度值: 76; 進度百分比: 0.76
progressBar1 當前進度值: 77; 進度百分比: 0.77
progressBar1 當前進度值: 78; 進度百分比: 0.78
progressBar1 當前進度值: 79; 進度百分比: 0.79
progressBar1 當前進度值: 80; 進度百分比: 0.8
progressBar1 當前進度值: 81; 進度百分比: 0.81
progressBar1 當前進度值: 82; 進度百分比: 0.82
progressBar1 當前進度值: 83; 進度百分比: 0.83
progressBar1 當前進度值: 84; 進度百分比: 0.84
progressBar1 當前進度值: 85; 進度百分比: 0.85
progressBar1 當前進度值: 86; 進度百分比: 0.86
progressBar1 當前進度值: 87; 進度百分比: 0.87
progressBar1 當前進度值: 88; 進度百分比: 0.88
progressBar1 當前進度值: 89; 進度百分比: 0.89
progressBar1 當前進度值: 90; 進度百分比: 0.9
progressBar1 當前進度值: 91; 進度百分比: 0.91
progressBar1 當前進度值: 92; 進度百分比: 0.92
progressBar1 當前進度值: 93; 進度百分比: 0.93
progressBar1 當前進度值: 94; 進度百分比: 0.94
progressBar1 當前進度值: 95; 進度百分比: 0.95
progressBar1 當前進度值: 96; 進度百分比: 0.96
progressBar1 當前進度值: 97; 進度百分比: 0.97
progressBar1 當前進度值: 98; 進度百分比: 0.98
progressBar1 當前進度值: 99; 進度百分比: 0.99
progressBar1 當前進度值: 100; 進度百分比: 1.0
progressBar2 當前進度值: 1; 進度百分比: 0.01
progressBar2 當前進度值: 2; 進度百分比: 0.02
progressBar2 當前進度值: 3; 進度百分比: 0.03
progressBar2 當前進度值: 4; 進度百分比: 0.04
progressBar2 當前進度值: 5; 進度百分比: 0.05
progressBar2 當前進度值: 6; 進度百分比: 0.06
progressBar2 當前進度值: 7; 進度百分比: 0.07
progressBar2 當前進度值: 8; 進度百分比: 0.08
progressBar2 當前進度值: 9; 進度百分比: 0.09
progressBar2 當前進度值: 10; 進度百分比: 0.1
progressBar2 當前進度值: 11; 進度百分比: 0.11
progressBar2 當前進度值: 12; 進度百分比: 0.12
progressBar2 當前進度值: 13; 進度百分比: 0.13
progressBar2 當前進度值: 14; 進度百分比: 0.14
progressBar2 當前進度值: 15; 進度百分比: 0.15
progressBar2 當前進度值: 16; 進度百分比: 0.16
progressBar2 當前進度值: 17; 進度百分比: 0.17
progressBar2 當前進度值: 18; 進度百分比: 0.18
progressBar2 當前進度值: 19; 進度百分比: 0.19
progressBar2 當前進度值: 20; 進度百分比: 0.2
progressBar2 當前進度值: 21; 進度百分比: 0.21
progressBar2 當前進度值: 22; 進度百分比: 0.22
progressBar2 當前進度值: 23; 進度百分比: 0.23
progressBar2 當前進度值: 24; 進度百分比: 0.24
progressBar2 當前進度值: 25; 進度百分比: 0.25
progressBar2 當前進度值: 26; 進度百分比: 0.26
progressBar2 當前進度值: 27; 進度百分比: 0.27
progressBar2 當前進度值: 28; 進度百分比: 0.28
progressBar2 當前進度值: 29; 進度百分比: 0.29
progressBar2 當前進度值: 30; 進度百分比: 0.3
progressBar2 當前進度值: 31; 進度百分比: 0.31
progressBar2 當前進度值: 32; 進度百分比: 0.32
progressBar2 當前進度值: 33; 進度百分比: 0.33
progressBar2 當前進度值: 34; 進度百分比: 0.34
progressBar2 當前進度值: 35; 進度百分比: 0.35
progressBar2 當前進度值: 36; 進度百分比: 0.36
progressBar2 當前進度值: 37; 進度百分比: 0.37
progressBar2 當前進度值: 38; 進度百分比: 0.38
progressBar2 當前進度值: 39; 進度百分比: 0.39
progressBar2 當前進度值: 40; 進度百分比: 0.4
progressBar2 當前進度值: 41; 進度百分比: 0.41
progressBar2 當前進度值: 42; 進度百分比: 0.42
progressBar2 當前進度值: 43; 進度百分比: 0.43
progressBar2 當前進度值: 44; 進度百分比: 0.44
progressBar2 當前進度值: 45; 進度百分比: 0.45
progressBar2 當前進度值: 46; 進度百分比: 0.46
progressBar2 當前進度值: 47; 進度百分比: 0.47
progressBar2 當前進度值: 48; 進度百分比: 0.48
progressBar2 當前進度值: 49; 進度百分比: 0.49
progressBar2 當前進度值: 50; 進度百分比: 0.5
progressBar2 當前進度值: 51; 進度百分比: 0.51
progressBar2 當前進度值: 52; 進度百分比: 0.52
progressBar2 當前進度值: 53; 進度百分比: 0.53
progressBar2 當前進度值: 54; 進度百分比: 0.54
progressBar2 當前進度值: 55; 進度百分比: 0.55
progressBar2 當前進度值: 56; 進度百分比: 0.56
progressBar2 當前進度值: 57; 進度百分比: 0.57
progressBar2 當前進度值: 58; 進度百分比: 0.58
progressBar2 當前進度值: 59; 進度百分比: 0.59
progressBar2 當前進度值: 60; 進度百分比: 0.6
progressBar2 當前進度值: 61; 進度百分比: 0.61
progressBar2 當前進度值: 62; 進度百分比: 0.62
progressBar2 當前進度值: 63; 進度百分比: 0.63
progressBar2 當前進度值: 64; 進度百分比: 0.64
progressBar2 當前進度值: 65; 進度百分比: 0.65
progressBar2 當前進度值: 66; 進度百分比: 0.66
progressBar2 當前進度值: 67; 進度百分比: 0.67
progressBar2 當前進度值: 68; 進度百分比: 0.68
progressBar2 當前進度值: 69; 進度百分比: 0.69
progressBar2 當前進度值: 70; 進度百分比: 0.7
progressBar2 當前進度值: 71; 進度百分比: 0.71
progressBar2 當前進度值: 72; 進度百分比: 0.72
progressBar2 當前進度值: 73; 進度百分比: 0.73
progressBar2 當前進度值: 74; 進度百分比: 0.74
progressBar2 當前進度值: 75; 進度百分比: 0.75
progressBar2 當前進度值: 76; 進度百分比: 0.76
progressBar2 當前進度值: 77; 進度百分比: 0.77
progressBar2 當前進度值: 78; 進度百分比: 0.78
progressBar2 當前進度值: 79; 進度百分比: 0.79
progressBar2 當前進度值: 80; 進度百分比: 0.8
progressBar2 當前進度值: 81; 進度百分比: 0.81
progressBar2 當前進度值: 82; 進度百分比: 0.82
progressBar2 當前進度值: 83; 進度百分比: 0.83
progressBar2 當前進度值: 84; 進度百分比: 0.84
progressBar2 當前進度值: 85; 進度百分比: 0.85
progressBar2 當前進度值: 86; 進度百分比: 0.86
progressBar2 當前進度值: 87; 進度百分比: 0.87
progressBar2 當前進度值: 88; 進度百分比: 0.88
progressBar2 當前進度值: 89; 進度百分比: 0.89
progressBar2 當前進度值: 90; 進度百分比: 0.9
progressBar2 當前進度值: 91; 進度百分比: 0.91
progressBar2 當前進度值: 92; 進度百分比: 0.92
progressBar2 當前進度值: 93; 進度百分比: 0.93
progressBar2 當前進度值: 94; 進度百分比: 0.94
progressBar2 當前進度值: 95; 進度百分比: 0.95
progressBar2 當前進度值: 96; 進度百分比: 0.96
progressBar2 當前進度值: 97; 進度百分比: 0.97
progressBar2 當前進度值: 98; 進度百分比: 0.98
progressBar2 當前進度值: 99; 進度百分比: 0.99
progressBar2 當前進度值: 100; 進度百分比: 1.0

範例3 使用線程池依次執行子線程A 、B、C

package com.contoso;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author [email protected]
 */
public class App {

    public static void main(String[] args) {
        //創建一個線程池存儲這個3個線程
        //線程池採用的是先進先出的隊列數據結構,按順序每次只能彈出一個工作線程
        ExecutorService executor = Executors.newSingleThreadExecutor();
        // 測試多次看看是否真的按先打印A,然後打印B,最後打印C的順序來執行多線程
        for (int i = 0; i < 10; i++) { 
            ThreadA a = new ThreadA();
            ThreadB b = new ThreadB();
            ThreadC c = new ThreadC();
            executor.submit(a);
            executor.submit(b);
            executor.submit(c);
        }
        //結束線程池的生命週期
        executor.shutdown();
    }

}
package com.contoso;

public class ThreadA extends Thread {

    @Override
    public void run() {
        try {
            //即使讓當前子線程等待其它子線程也不會被執行
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "A");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package com.contoso;

public class ThreadB extends Thread {

    @Override
    public void run() {
        try {
            //即使讓當前子線程等待其它子線程也不會被執行
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "B");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package com.contoso;

public class ThreadC extends Thread {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "C");
    }

}
run:
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
pool-1-thread-1A
pool-1-thread-1B
pool-1-thread-1C
BUILD SUCCESSFUL (total time: 30 seconds)

範例4 使用Executors.newFixedThreadPool,synchronized,wait,notifyAll,還有嵌套子線程類

package com.contoso;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author [email protected]
 */
public class App {

    public static Object obj = new Object();
    public static String next = "a";

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            ThreadA a = new ThreadA();
            ThreadB b = new ThreadB();
            ThreadC c = new ThreadC();
            executor.submit(a);
            executor.submit(b);
            executor.submit(c);
        }
        //結束線程池的生命週期
        executor.shutdown();

    }

    private static class ThreadA extends Thread {

        @Override
        public void run() {
            while (true) {
                synchronized (obj) {
                    while (!"a".equals(next)) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "A");
                    next = "b";
                    obj.notifyAll();
                }

            }

        }

    }

    private static class ThreadB extends Thread {

        @Override
        public void run() {
            while (true) {
                synchronized (obj) {
                    while (!"b".equals(next)) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "B");
                    next = "c";
                    obj.notifyAll();
                }

            }

        }

    }

    private static class ThreadC extends Thread {

        @Override
        public void run() {
            while (true) {
                synchronized (obj) {
                    while (!"c".equals(next)) {
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "C");
                    next = "a";
                    obj.notifyAll();
                }

            }

        }

    }

}
run:
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
pool-1-thread-1A
pool-1-thread-2B
pool-1-thread-3C
。。。  。。。
。。。  。。。

範例5 使用synchronized,wait,notifyAll實現按順序打印 A, B, C

package com.contoso;
/**
 *
 * @author [email protected]
 */
public class App {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(new ThreadA()).start();
            new Thread(new ThreadB()).start();
            new Thread(new ThreadC()).start();
        }
    }
}
package com.contoso;

class Printer {

    private static int status = 1;
    private static Object obj = new Object();

    public void printA() throws InterruptedException {
        synchronized (obj) {
            while (status != 1) {
                obj.wait();
            }
            System.out.println('A');
            status = 2;
            obj.notifyAll();
        }
    }

    public void printB() throws InterruptedException {
        synchronized (obj) {
            while (status != 2) {
                obj.wait();
            }
            System.out.println('B');
            status = 3;
            obj.notifyAll();
        }
    }

    public void printC() throws InterruptedException {
        synchronized (obj) {
            while (status != 3) {
                obj.wait();
            }
            System.out.println('C');
            status = 1;
            obj.notifyAll();
        }
    }
}
package com.contoso;

class ThreadA implements Runnable {
    @Override
    public void run() {
        try {
            new Printer().printA();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.contoso;

class ThreadB implements Runnable {
    @Override
    public void run() {
        try {
            new Printer().printB();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.contoso;

class ThreadC implements Runnable {
    @Override
    public void run() {
        try {
            new Printer().printC();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
run:
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
BUILD SUCCESSFUL (total time: 0 seconds)

範例6 使用volatile,synchronized,wait,notifyAll實現按順序打印 A, B, C

package com.contoso;

public class App {

    public static void main(String[] args) {
        Printer printer = new Printer();
        for (int i = 0; i < 10; i++) {
            ThreadB b = new ThreadB(printer);
            ThreadA a = new ThreadA(printer);
            ThreadC c = new ThreadC(printer);
            c.start();
            a.start();
            b.start();
        }
    }
}
package com.contoso;

public class Printer {

    //默認狀態值爲1,它決定了首先執行線程A,輸出順序與線程啓動順序無關
    private volatile int status = 1;

    public synchronized void printA() {
        try {
            while (status != 1) {
                wait();
            }
            System.out.println("A");
            status = 2;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void printB() {
        try {
            while (status != 2) {
                wait();
            }
            System.out.println("B");
            status = 3;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void printC() {
        try {
            while (status != 3) {
                wait();
            }
            System.out.println("C");
            status = 1;
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.contoso;

public class ThreadA extends Thread {

    private Printer printer;

    public ThreadA(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printA();
    }

}
package com.contoso;

public class ThreadB extends Thread {

    private Printer printer;

    public ThreadB(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printB();
    }

}
package com.contoso;

public class ThreadC extends Thread {

    private Printer printer;

    public ThreadC(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printC();
    }

}
run:
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
A
B
C
BUILD SUCCESSFUL (total time: 0 seconds)

範例7:使用ReentrantLock和Condition實現按順序打印A, B, C

package com.contoso;

public class App {

    public static void main(String argv[]) {

        Printer printer = new Printer();
        for (int i = 0; i < 10; i++) {
            Thread A = new Thread(new ThreadA(printer));
            Thread B = new Thread(new ThreadB(printer));
            Thread C = new Thread(new ThreadC(printer));
            A.start();
            B.start();
            C.start();
        }

    }
}
package com.contoso;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Printer {

    private int flag = 1;
    private Lock lock = new ReentrantLock();
    private Condition conditionA = lock.newCondition();
    private Condition conditionB = lock.newCondition();
    private Condition conditionC = lock.newCondition();

    /*
    判斷條件使用while而不使用if,它們之間有什麼區別呢? 
    線程從wait等待狀態被喚醒,從而獲得鎖以後會繼續往下執行,
    比如A調用nofityAll()可能喚醒的是B或者是C,這時B或者C誰會先獲得鎖是不確定的;
    如果是C先獲得了鎖,那麼C就繼續往下執行打印,這樣打印的順序就不對啦。
    所以我們需要使用while,當C獲得鎖以後再去判斷一下flag,
    如果這時收到的信號還不是它執行的時候,C再一次進入wait狀態。
    此時A與C都處於wait等待狀態,最後只有B獲得鎖和執行信號匹配,這樣就能實現順序打印啦
    */
    public void printA() {
        try {
            lock.lock();
            //注意啦,此處不能使用if條件,原因是無法按順序打印,容易讓線程無限等待
            while (flag != 1) {
                try {
                    conditionA.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " A");
            flag = 2;
            conditionB.signal();
        } finally {
            lock.unlock();
        }

    }

    public void printB() {
        try {
            lock.lock();
           //注意啦,此處不能使用if條件,原因是無法按順序打印,容易讓線程無限等待
            while (flag != 2) {
                try {
                    conditionB.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " B");
            flag = 3;
            conditionC.signal();
        } finally {
            lock.unlock();
        }

    }

    public void printC() {
        try {
            lock.lock();
            //注意啦,此處不能使用if條件,原因是無法按順序打印,容易讓線程無限等待
            while (flag != 3) {
                try {
                    conditionC.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " C");
            flag = 1;
            conditionA.signal();
        } finally {
            lock.unlock();
        }
    }
}
package com.contoso;

public class ThreadA implements Runnable {

    private Printer printer;

    public ThreadA(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printA();
    }

}
package com.contoso;

public class ThreadB implements Runnable {

    private Printer printer;

    public ThreadB(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printB();
    }
}
package com.contoso;

public class ThreadC implements Runnable {

    private Printer printer;

    public ThreadC(Printer printer) {
        this.printer = printer;
    }

    @Override
    public void run() {
        printer.printC();
    }

}
run:
Thread-0 A
Thread-1 B
Thread-5 C
Thread-3 A
Thread-10 B
Thread-11 C
Thread-12 A
Thread-13 B
Thread-2 C
Thread-6 A
Thread-4 B
Thread-8 C
Thread-15 A
Thread-7 B
Thread-17 C
Thread-18 A
Thread-19 B
Thread-20 C
Thread-9 A
Thread-22 B
Thread-14 C
Thread-24 A
Thread-25 B
Thread-26 C
Thread-21 A
Thread-16 B
Thread-29 C
Thread-27 A
Thread-28 B
Thread-23 C
BUILD SUCCESSFUL (total time: 0 seconds)

範例8: 讓主線程一直等所有的子線程處理完畢再繼續執行主線程

package com.contoso;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class App {

    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService executor = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            executor.submit(new Processor(latch));
        }
        executor.shutdown();

        try {
            // 讓主線程一直等所有的子線程處理完畢再繼續執行主線程
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long finishedTime = System.nanoTime();
        System.out.println(Thread.currentThread().getName() + " Thread Completed." + "  finishedTime = " + finishedTime);
    }
}
package com.contoso;

import java.util.concurrent.CountDownLatch;

public class Processor implements Runnable {

    private CountDownLatch latch;

    public Processor(CountDownLatch latch) {
        this.latch = latch;
    }

    public void run() {
        long startTime = System.nanoTime();
        System.out.println(Thread.currentThread().getName() + " Thread Processor Started." + "  startTime = " + startTime);
        try {
            System.out.println(Thread.currentThread().getName() + " Thread State is " + Thread.currentThread().getState()+" simulation task running for 10 seconds");
            Thread.sleep(10000); // 10s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
        long endTime = System.nanoTime();
        System.out.println(Thread.currentThread().getName() + " Thread Processor Finished." + "  endTime = " + endTime);
    }
}
run:
pool-1-thread-2 Thread Processor Started.  startTime = 55322237121188
pool-1-thread-3 Thread Processor Started.  startTime = 55322237127592
pool-1-thread-1 Thread Processor Started.  startTime = 55322237119267
pool-1-thread-2 Thread State is RUNNABLE simulation task running for 10 seconds
pool-1-thread-1 Thread State is RUNNABLE simulation task running for 10 seconds
pool-1-thread-3 Thread State is RUNNABLE simulation task running for 10 seconds
pool-1-thread-1 Thread Processor Finished.  endTime = 55332238384639
pool-1-thread-3 Thread Processor Finished.  endTime = 55332238444514
main Thread Completed.  finishedTime = 55332238552737
pool-1-thread-2 Thread Processor Finished.  endTime = 55332238384960
BUILD SUCCESSFUL (total time: 10 seconds)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章