java 線程理解

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

public class homework514 {
    //run()在完成時不會返回值,Callable接口的call()會產生返回值
    public static class Run_test implements Runnable{//使用Runnable接口的run()方法{多線程合做完成任務}
        private static int taskCount = 0;
        private int priority;
        private final int id = taskCount++;//線程id
        public Run_test(int priority){
            this.priority = priority;
        }
        public String dispaly(){//多線程就應該在run裏面輸出
            return "線程id:"+id+"優先級:"+priority;
        }
        public void run(){
            Thread.currentThread().setPriority(priority);//run()中是多個線程須要完成的任務
            System.out.println(dispaly());
            Thread.yield();//線程調度器
        }
    }
    public static Thread getThreadName(String threadName) {
        Thread  t = new Thread(threadName);
        while(true){
            //根據線程名取得線程
            if (t.getName().equals(threadName)){

                return t;
            }
            return null;
        }
    }

    static class Runner_extend_test extends Thread{//多線程各自完成任務
        private int countDown=5;
        private String name;
        public Runner_extend_test(String name){
            this.name = name;
        }//以倒計時爲例子
        public String dispaly(){//多線程就應該在run裏面輸出
            return name+":"+(countDown>0?countDown:"GO!");

        }
        public void run() {
            while (countDown-- > 0) {
                System.out.println(dispaly());
            }
        }

    }

    public static void main(String[] args)
    {

    /*
        for(int i=1;i<=5;i++)//未設置優先級
        {
            Thread t = new Thread(new Run_test());//提交給Thread構造器
            t.start();
        }
        */
    /*
        //建立一個線程集
        ExecutorService exec = Executors.newFixedThreadPool(5);//預先限制數量
        for(int i=1;i<=5;i++){
            exec.execute(new Run_test());
        }
        exec.shutdown();
        */
    /*
        //序列化線程,每個線程會在下一個線程運行前結束
        ExecutorService exec = Executors.newSingleThreadExecutor();//預先限制數量
        for(int i=1;i<=5;i++){
            exec.execute(new Run_test());
        }
        exec.shutdown();
        */
    /*
        //設置線程優先級
        ExecutorService exec = Executors.newCachedThreadPool();
        Run_test one = new Run_test(Thread.MAX_PRIORITY);
        Run_test two = new Run_test(Thread.MIN_PRIORITY);
        Run_test three = new Run_test(Thread.NORM_PRIORITY);
        exec.execute(one);//10
        exec.execute(two);//1
        exec.execute(three);//5
        System.out.println(getThreadName("one"));
        exec.shutdown();
        */
        //啓動繼承Thread的線程
        Runner_extend_test run_1=new Runner_extend_test("線程1");
        Runner_extend_test run_2=new Runner_extend_test("線程2");
        Runner_extend_test run_3=new Runner_extend_test("線程3");

        run_1.start();
        run_2.start();
        run_3.start();
    }
}

  一切都在代碼中java

相關文章
相關標籤/搜索