1. 程式人生 > >(執行緒二)interrupt、setPriority、join方法示例

(執行緒二)interrupt、setPriority、join方法示例

例一:interrupt方法

                        public class ThreadDemo01 {
                            public static void main(String[] args) {
                                Thread t1=new Thread() {
                                    public void run() {
                                        System.out.println("t1開始");
                                        try {
                                            Thread.sleep(10000);
                                            System.out.println("t1在睡覺");
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                            System.out.println("t1睡覺被打斷");
                                        }
                                    }
                                };
                                Thread t2=new Thread() {
                                    public void run() {
                                        for(int i=0;i<5;i++) {
                                            System.out.println("t2:砸牆,"+80*i);
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                        t1.interrupt();
                                    }
                                };
                                t1.start();
                                t2.start();
                            }
                        }
                        執行結果:
                        t2:砸牆,0
                        t1開始
                        t2:砸牆,80
                        t2:砸牆,160
                        t2:砸牆,240
                        t2:砸牆,320
                        java.lang.InterruptedException: sleep interrupted
                        t1睡覺被打斷
                            at java.lang.Thread.sleep(Native Method)
                            at com.hyxy0915.ThreadDemo01$1.run(ThreadDemo01.java:8)

例二:執行緒setPriority方法測試說明

                        public class ThreadDemo03 {
                            public static void main(String[] args) {
                                Thread t1=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t1");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                Thread t2=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t2");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                Thread t3=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t3");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                t2.setPriority(Thread.MAX_PRIORITY);
                                t3.setPriority(Thread.MIN_PRIORITY);
                                t1.start();
                                t2.start();
                                t3.start();
                            }
                        }

                        public class ThreadDemo03 {
                            public static void main(String[] args) {
                                Thread t1=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t1");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                Thread t2=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t2");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                Thread t3=new Thread() {
                                    public void run() {
                                        for(int i=0;i<10;i++) {
                                            System.out.println("t3");
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            };
                                        }
                                    }
                                };
                                t2.setPriority(Thread.MAX_PRIORITY);
                                t3.setPriority(Thread.MIN_PRIORITY);
                                t1.start();
                                t2.start();
                                t3.start();
                            }
                        }

                        執行結果:從結果可以看出t1、t2、t3並非按照所設定優先順序進行輸出,可見,理論上來說執行緒可以設定優先順序,可是cpu是不可控的,所以優先順序僅僅只是在理論上,並非是一定按照我們所設定優先順序進行cpu排程

例三:join方法示例

                        public class ThreadDemo04 {
                            private static int sum=0;
                            public static void main(String[] args) {
                                Thread t1=new Thread() {
                                    public void run() {
                                        for(int i=0;i<=100;i++) {
                                            sum+=i;
                                        } 
                                    }
                                };
                                Thread t2=new Thread() {
                                    public void run() {
                                        try {
                                            t1.join();
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        System.out.println(sum);
                                    }
                                };
                                t1.setPriority(Thread.MAX_PRIORITY);
                                t1.start();
                                t2.start();
                            }
                        }

                        public class ThreadDemo04 {
                            private static int sum=0;
                            public static void main(String[] args) {
                                Thread t1=new Thread() {
                                    public void run() {
                                        for(int i=0;i<=100;i++) {
                                            sum+=i;
                                        } 
                                    }
                                };
                                Thread t2=new Thread() {
                                    public void run() {
                                        try {
                                            t1.join();
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        System.out.println(sum);
                                    }
                                };
                                t1.setPriority(Thread.MAX_PRIORITY);
                                t1.start();
                                t2.start();
                            }
                        }

                        輸出結果:5050
                        無論執行多少次結果都為5050,所以我們由此可知是t1執行緒執行結束後t2才開始執行