多線程的介紹day05

程序:一段靜態的代碼。

進程:正在運行的程序。

線程:進程中的一部分。

1.通過匿名類來開啓線程

public class ThreadTest extends Thread {
     public static void main(String[] args) {
         //通過匿名類來開啓線程
        new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for(int i=0;i<=100;i++){
                    System.out.println(Thread.currentThread().getName()+"i="+i);
                }
            }
        }.start();
    
   }
   

}

2.線程常用的方法

package com.xujy.thread;
/**
 * Thread的常用方法
 * 1.start方法,啓動線程並執行相應的run方法。
 * 2.run方法,子線程要執行的代碼放進run方法裏。
 * 3.Thread.currentThread().getName(),獲得線程名字。
 * 4.Thread.currentThread().setName(),設置線程名字。
 * 5.yield 調用此方法的線程,釋放當前cpu的執行權,然後各個線程再重新搶奪cpu的執行權。相當於先讓一下。
 * 6.join 在a線程執行過程中,調用b.join()方法,a線程停止執行,b線程開始執行直到執行完畢,再執行a線程
 * 7.isAlive()判斷當前線程是否還存活
 * 8.sleep(1000)當前線程每休眠一秒執行一次。
 * 9.線程通信:wait notify notifyAll
 * 10.設置線程的優先級setPriority()默認是5,最低是1,最高是10,設置線程的優先級可以讓線程搶到cpu的概率加大
 * @author Administrator
 *
 */
public class TestThread {
    public static void main(String[] args) throws InterruptedException {
        //3.創建類的對象,調用start方法開啓線程
        Test1 t1=new Test1();
        t1.setName("線程1");//設置子線程的名字
        /*Test1 t2=new Test1();
        t2.setName("線程2");*/
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        //t2.start();
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        Thread.currentThread().setName("主線程");//設置主線程的名字
        //4.主線程同時也執行這個方法,此時開始了三個線程,兩個子線程一個主線程。
        for(int i=1;i<=100;i++){
            System.out.println("線程的優先級是"+Thread.currentThread().getPriority()+"線程的名字是"+Thread.currentThread().getName()+",i="+i);
            /*if(i%20==0){
                Thread.currentThread().yield();
            }*/
            /*if(i==1){
                t1.join();
            }
            System.out.println(t1.isAlive()+",==="+Thread.currentThread().isAlive());*/
        }
    
    }

}
//1.創建一個類繼承Thread
class Test1 extends Thread{
    //2.重寫父類的run方法
    @Override
    public void run() {
        // Thread.currentThread().getName()獲取線程名字
        for(int i=0;i<=100;i++){
            /*try {
                Thread.currentThread().sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/
            System.out.println("線程的優先級是"+Thread.currentThread().getPriority()+"線程的名字是"+Thread.currentThread().getName()+",i="+i);
        }
    }
}
2.同時開啓兩個窗口實現100張票的購買

/**
 * 使用實現優點
 * 1.避免了java中單繼承的侷限性
 * 2.可以多個線程同時執行一個任務,而繼承的方式則是多個線程執行重複的同一個任務。
 * @author Administrator
 *
 */
public class ThreadRun implements Runnable {
    int ticket=100;
    public static void main(String[] args) {
        ThreadRun tr=new ThreadRun();
        Thread th=new Thread(tr);
        Thread th1=new Thread(tr);
        th.setName("窗口1");
        th.start();
        th1.setName("窗口2");
        th1.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            if(ticket>0){
             System.out.println(Thread.currentThread().getName()+"ticket="+ticket--);
            }else{
                break;
            }
        }
     
    }

}

使用繼承方式的時候需要把變量定義爲靜態變量,隨着類的加載而加載,不會因爲創建了多個對象而每個對象有一套屬性值。

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