java多線程基礎學習

最近多java多線程的學習比較感興趣,所以在圖書館找到了一本好書能夠帶我進行多線程的基礎學習。之前在網(wǎng)上找的教程,學習了一點但是感覺還是沒有入門,一些比較重要的知識很多文章都沒有仔細講解。所以現(xiàn)在是重新開始學習java多線程。
進程和線程的概念就不在這里說了。
1.實現(xiàn)多線程方式有兩種,一種是繼承Thread,另一種是實現(xiàn)Runnable接口。
現(xiàn)在主要是推薦實現(xiàn)Runnable接口,因為java的機制是不支持多繼承。

package learnThread;

public class MyThread8 extends Thread
{
    public void run()
    {
        super.run();
        System.out.println("測試線程 ");
    }
    public static void main(String[] args) 
    {
        MyThread8 m=new MyThread8();
        m.start();
        System.out.println("運行結(jié)束");
    }

}
package learnThread;

public class myThread implements Runnable {

    public void run() 
    {
        System.out.println("測試線程");
    }

    public static void main(String[] args) 
    {
        Runnable runable=new myThread();
        Thread thread=new Thread(runable);
        thread.start();
        System.out.println("運行結(jié)束");
    }

}
搜狗截圖18年03月27日2027_1.png

實現(xiàn)的結(jié)果都沒什么差別。

使用多線程技術,代碼的運行結(jié)果與代碼的執(zhí)行順序或者調(diào)用順序是無關的。

Thread.java類中的start()方法通知“線程規(guī)劃器”此線程已經(jīng)準備就緒,等待調(diào)用線程對象的run()方法。這個過程就是讓系統(tǒng)安排一個時間來調(diào)用Thread中的run方法,也就是使線程得到運行。啟動線程,具有異步執(zhí)行的效果。

currentThread():可返回代碼段正在被哪個線程調(diào)用的信息
isAlive():判斷當前的線程是否處于活動狀態(tài)
sleep():指定的毫秒數(shù)內(nèi)讓當前線程休眠
getid():取得線程的唯一標識

停止線程

java中有三種方法可以終止正在運行的線程
1.使用中途退出標志,使線程正常退出,也就是當run方法完成后線程終止。
2.使用stop方法強制終止線程,但是不推薦這個方法,因為stop和suspend以及resume一樣,都是過期作廢的方法,它們可能會殘生不可預料的效果
3.使用interrupt方法中斷線程。

能停止的線程------異常法
package learnThread;

//停止線程
public class myThread5 extends Thread 
{
    public void run() 
    {
        super.run();
        try {
        for(int i=0;i<500000;i++)
        {
            if(this.isInterrupted())
                {
                    System.out.println("已經(jīng)停止了,能退出了");
                    throw new InterruptedException();

                }
                System.out.println("i="+(i+1));
                
            }
            System.out.println("我在for下面");
        }
         catch (InterruptedException e) {
            System.out.println("進行run()方法中的catch");
            e.printStackTrace();
        }
        

    }

    public static void main(String[] args) throws InterruptedException 
    {
        try
        {
            myThread5 thread=new myThread5();
            thread.start();
            Thread.sleep(2000);
            thread.interrupt();
        }
        catch(InterruptedException e)
        {
            System.out.println("catch main");
            e.printStackTrace();
            
        }

    }

}
睡眠法
package learnThread;

//停止線程
public class myThread6 extends Thread 
{
    public void run() 
    {
        super.run();
        try
        {
            System.out.println("run begin");
            Thread.sleep(20000);
            System.out.println("run end");
        }
        catch(InterruptedException e)
        {
            System.out.println("在沉睡中停止線程 "+this.isInterrupted());
            e.printStackTrace();
        }

    }

    public static void main(String[] args)
    {
        try
        {
            myThread6 thread=new myThread6();
            thread.start();
            Thread.sleep(200);
            thread.interrupt();
        }
        catch(InterruptedException e)
        {
            System.out.println("main catch");
            e.printStackTrace();
            
        }
        System.out.println("end");
        

    }

}
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容