java 多線程 start方法 run方法 簡單介紹。


一 start開啟一個多線程, run 只是一個內部的方法。

package com.aaa.threaddemo;
/*
 *   start方法的作用?
 *       在 Java中啟動多線程調用的是start方法。
 *      在start方法中,真正實現多線程的是一個本地的方法start0。
 *      調用start方法啟動一個線程,此時的狀態是  就緒。
 *      無需等待run方法體代碼執行完畢,可以繼續執行下面的代碼。
 *      被synchronized 修飾, 線程是安全的
 *      由jvm創建的main方法線程和system組線程,並不會通過start來啟動。
 *      等到run方法run 下結束,線程終止。start方法不可使用兩次。
 *    
    public synchronized void start() {
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
            }
        }
    }

    private native void start0();
    
    
    run方法?
        run()方法只是一個類中的普通方法,調用run方法跟調用普通方法一樣
        方法 run()稱為線程體,它包含了要執行的這個線程的內容,線程就進入了  【運行狀態】,開始運
        行 run 函數當中的代碼。 
        Run 方法運行結束, 此線程終止。再調用start方法報錯的。
        然后 CPU 再調度其它線
        
   
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }   
 * 
 */
public class StartDemo {
    public static void main(String[] args) {
        
        Runnable rdemo = new  Runnable() {
            public void run() {
                String name = Thread.currentThread().getName();
                System.out.println("當前運行的線程 : " + name);
            }
        };
        
        rdemo.run();
        
        // 開啟多線程,執行run方法
        new Thread(rdemo).start();    
    }
}

 

 

 

 

 

二  直觀比較run方法和start。

package com.aaa.threaddemo;
/*
 *   start方法的作用?
 *       在 Java中啟動多線程調用的是start方法。
 *      在start方法中,真正實現多線程的是一個本地的方法start0。
 *      調用start方法啟動一個線程,此時的狀態是  就緒。
 *      無需等待run方法體代碼執行完畢,可以繼續執行下面的代碼。
 *      被synchronized 修飾, 線程是安全的
 *      由jvm創建的main方法線程和system組線程,並不會通過start來啟動。
 *      等到run方法run 下結束,線程終止。start方法不可使用兩次。
 *    
    public synchronized void start() {
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
            }
        }
    }

    private native void start0();
    
    
    run方法?
        run()方法只是一個類中的普通方法,調用run方法跟調用普通方法一樣
        方法 run()稱為線程體,它包含了要執行的這個線程的內容,線程就進入了  【運行狀態】,開始運
        行 run 函數當中的代碼。 
        Run 方法運行結束, 此線程終止。再調用start方法報錯的。
        然后 CPU 再調度其它線
        
   
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }   
 * 
 */
public class StartDemo {
    public static void main(String[] args) {
                // 調用的是類中的一個普通方法run
                runDemo runDemo = new runDemo();
                runDemo.run();
                
                //開啟多線程
                runDemo.start();
                //線程已經結束,二次執行會報錯
                runDemo.start();    
    }
}

/*
 * 
     Runnable rdemo = new  Runnable() {
            public void run() {
                String name = Thread.currentThread().getName();
                System.out.println("當前運行的線程 : " + name);
            }
        };
        
        rdemo.run();
        
        // 開啟多線程,執行run方法
        new Thread(rdemo).start();    
 * 
        //  這里是new 一個Thread, 可以繼續調用start方法。
        new Thread(rdemo).start();
        
        
        
 */
class runDemo extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("run show" + Thread.currentThread().getName());
    }
}

 

 

 

 

 

 

三  新建線程,start 方法可再次使用

package com.aaa.threaddemo;
/*
 *   start方法的作用?
 *       在 Java中啟動多線程調用的是start方法。
 *      在start方法中,真正實現多線程的是一個本地的方法start0。
 *      調用start方法啟動一個線程,此時的狀態是  就緒。
 *      無需等待run方法體代碼執行完畢,可以繼續執行下面的代碼。
 *      被synchronized 修飾, 線程是安全的
 *      由jvm創建的main方法線程和system組線程,並不會通過start來啟動。
 *      等到run方法run 下結束,線程終止。start方法不可使用兩次。
 *    
    public synchronized void start() {
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
            }
        }
    }

    private native void start0();
    
    
    run方法?
        run()方法只是一個類中的普通方法,調用run方法跟調用普通方法一樣
        方法 run()稱為線程體,它包含了要執行的這個線程的內容,線程就進入了  【運行狀態】,開始運
        行 run 函數當中的代碼。 
        Run 方法運行結束, 此線程終止。再調用start方法報錯的。
        然后 CPU 再調度其它線
        
   
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }   
 * 
 */
public class StartDemo {
    public static void main(String[] args) {
        
         
         Runnable rdemo = new  Runnable() {
                public void run() {
                    String name = Thread.currentThread().getName();
                    System.out.println("當前運行的線程 : " + name);
                }
            };
            
            rdemo.run();
            
        // 開啟多線程,執行run方法
        new Thread(rdemo).start();    
     
        //  這里是new 一個Thread, 可以繼續調用start方法。
        new Thread(rdemo).start();
        
        
        // 調用的是類中的一個普通方法run
        runDemo runDemo = new runDemo();
        //開啟多線程
        runDemo.start();
        //線程已經結束,二次執行會報錯
        runDemo.start();    
    }
}
class runDemo extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("run show" + Thread.currentThread().getName());
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM