多線程

                                                                    JAVA多線程

    一.進程

      計算機正在執行的程序(.exe),負責內存的分配

        研究進程的時候,本質上就是研究線程
         * 一個Java應用程序(main函數)在執行的過程裏面通常有幾個線程在執行?
         * 1.main函數是一個線程
         * 2.gc線程

       1.1多進程

            計算機在以一個飛快的執行速度在來回的切換應用程序
        1.2單進程
       
    線程

        多線程

       

 //啓動線程      子線程
        MyThread th1=new MyThread();
        //th1.run();    //呈現出來的現象分線程沒關係?why?這只是純粹的對象調用方法,不是啓動線程
        th1.start();    //啓動線程
        
        //主線程執行內容
        for(int j=100;j>0;j--){
            System.out.println("main"+j);
        }
    }
}
/*自定義線程;
 * 步驟:
 * 1.創建一個類
 * 2.繼承Thread類
 * 3.重寫run方法
 */
class MyThread extends Thread{
    @Override
    public void run() {    
        for(int i=1;i<100;i++){
            System.out.println(i);
        }
    }
}
            一個應用程序裏面有多個執行路徑(執行的分配跟多進程一樣,競爭CPU使用權)
                 360軟件 進程
                體檢    線程
                殺毒   線程
        單線程
        線程裏面的一個執行路徑(分支)
    自定義線程

        第一種自定義線程例子

        

public class Demo03 {

	public static void main(String[] args) {
		/*研究第一種創建自動義線程的方法的特點:動車買票的情況:多個窗口賣票
		 * 
		 * 1.數據非靜態時不能共享
		 * 2.靜態數據可以線程之間共享
		 * 3.弊端:靜態變量的生命週期太長
		 * 4
		 * 
		 * 
		 */
		MyThread03 th1=new MyThread03();
		th1.setName("窗口一");
		MyThread03 th2=new MyThread03("窗口二");
		MyThread03 th3=new MyThread03("窗口三");
		MyThread03 th4=new MyThread03("窗口四");
			th1.start();
			th2.start();
			th3.start();
			th4.start();
	}

}
class MyThread03 extends Thread{
	public static int ticket=100;   //票數
	private String name;
	
	public MyThread03() {
		super();
	}
	public MyThread03(String name) {
		super(name);
	}

	@Override
	public void run() {	
		while(ticket>0){
			System.out.println(Thread.currentThread().getName()+"D360還剩下"+ticket+"張票");
			ticket--;
		}
		
	}
}


        第二種自定義線程例子

     

public class Demo04 {

	public static void main(String[] args) {
		/*第二種創建自定義線程的方式  實現接口的方式
		 * 
		 * 1.各線程之間實現共享
		 */
		Mythread04 th=new Mythread04();
		
		Thread th1=new Thread(th);
		th1.setName("窗口一");
		Thread th2=new Thread(th,"窗口二");
		Thread th3=new Thread(th,"窗口三");
		Thread th4=new Thread(th,"窗口四");
		th1.start();
		th2.start();
		th3.start();
		th4.start();
	}
}
class Mythread04 implements Runnable{
	private int ticket =100;
	@Override
	public void run() {
		try {
			Thread.sleep(100); //休眠
		} catch (Exception e) {
			e.printStackTrace();
		}
		while(ticket>0){
			System.out.println(Thread.currentThread().getName()+"中D10086還剩下"+ticket+"張票");
			ticket--;
		}
	}
	
}


    線程的加入和結束

        join()方法

        

public class JoinDemo {

	public static void main(String[] args) {
		/*線程加入:join()  對象的方法
		 *  
		 */
		Mythread06 th=new Mythread06();
		th.setName("線程一");
		th.start();
		
		for(int j=1;j<100;j++){
			if(j==50){
				try {
					th.join();   //線程加入  主線程進入阻塞狀態,讓出CPU使用權,。等待子線程執行完畢以後纔有可能繼續使用CPU
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName()+"=="+j);
		}
	}
}
class Mythread06 extends Thread{
	int i=1;
	@Override
	public void run() {
		while(i<100){
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"=="+i);
			i++;
		}
	}
}


        stop
   
           
public class StopDemo {
	public static void main(String[] args) {
		/*
		 *如何將線程結束掉?
		 *1.線程代買執行完畢的時候,進入死亡狀態(理想的情況) 
		 *2.使用標誌位,有條件的將線程結束掉
		 * 
		 */
		Mythread05 th=new Mythread05();
		th.setName("線程一");
		th.start();
		
		//主線程內容
		for(int i=1;i<100;i++){
			if(i==50){
				th.flag=false; //改變標誌位,結束子線程
			}
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"=="+i);
		}
	}
}
class Mythread05 extends Thread{
	public  boolean flag=true;
	int i=1;
	@Override
	public void run() {
		try {
			Thread.sleep(500);
		} catch (Exception e) {
			e.printStackTrace();
		}
		while(flag){
			System.out.println(Thread.currentThread().getName()+"=="+i);
			i++;
		}
		System.out.println(Thread.currentThread().getName()+"線程結束了!");
	}
}


發佈了43 篇原創文章 · 獲贊 24 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章