1. 程式人生 > >建立多執行緒的三種方式

建立多執行緒的三種方式

執行緒是指程序內部同時做的事情,比如在玩王者榮耀的時候,你可以同時攻擊英雄A和英雄B;

下面將此作為例子,引入三種建立多執行緒的方式;

例子思路:

                 ①先建立英雄類(Hero)設定三個屬性:name(英雄名)、hp(英雄的血量)、damage(攻擊力)

                   兩個方法:sttrackHero();  受到攻擊,減少hp、isDead();判斷英雄是否陣亡

                  ②建立繼承執行緒類(Thread),並重寫執行緒的run方法;

                  ③分別建立四個英雄(馬可波羅、李白、小喬、大喬)物件,建立第一個執行緒,馬可波羅攻擊李白;第二個執行緒,大喬攻擊小喬;

方式一:繼承執行緒類(Thread)

①首先建立英雄類(Hero)

package create_thread;

public class Hero {
	public String name; //英雄名
	public float hp;//血量
	public int damage;//攻擊傷害;
	
	//進行攻擊
	public void attrackHero(Hero h){
		
		h.hp-=damage;
		//格式化輸出
		System.out.format("%s正在攻擊%s,%s的hp變成了%.0f%n",name,h.name,h.name,h.hp);
		if(h.isDead()){
			System.out.println(h.name+"敗了");
		}
	}
	
	//判斷英雄是否低於0血
	public boolean isDead(){
		return 0>=hp?true:false;
	}

}

②繼承執行緒的KillThread類

package create_thread;

public class KillThread extends Thread{
	
	private Hero h1;
	private Hero h2;
	
	public KillThread(Hero h1,Hero h2){
		this.h1=h1;
		this.h2=h2;
	}
	
	public void run(){
		//判斷英雄是否血量值
		while(!h2.isDead()){
			h1.attrackHero(h2);
		}
	}
}

③建立英雄的物件,並啟動執行緒。

package create_thread;

public class TestThread {
	public static void main(String [] args){
		//建立物件
		Hero makeboluo = new Hero();
		makeboluo.name="馬可波羅";
		makeboluo.hp=666;
		makeboluo.damage=60;
		
		Hero libai = new Hero();
		libai.name="李白";
		libai.hp=666;
		libai.damage=50;
		
		Hero daqiao = new Hero();
		daqiao.name="大喬";
		daqiao.hp=666;
		daqiao.damage=50;
		
		Hero xiaoqiao = new Hero();
		xiaoqiao.name="小喬";
		xiaoqiao.hp=666;
		xiaoqiao.damage=50;
		
		KillThread killThread1=new KillThread(makeboluo,libai);
		//啟動執行緒;
		killThread1.start();
		
		KillThread killThread2=new KillThread(daqiao,xiaoqiao);
		//啟動執行緒;
		killThread2.start();	
	}
}

方式二:實現Runnable介面

①先實現介面,重寫run方法

package create_thread;

//實現Runnable介面
public class KillThread2 implements Runnable{
	Hero h1;
	Hero h2;
	public KillThread2(Hero h1,Hero h2){
		this.h1=h1;
		this.h2=h2;
	}	
	public void run() {
		while(!h2.isDead()){
			h1.attrackHero(h2);
		}		
	}
}

②啟動是先建立實現Runnable的killThread1物件,再利用該物件去啟動執行緒

package create_thread;

public class TestThread2 {
	public static void main(String [] args){
		//建立物件
		Hero makeboluo = new Hero();
		makeboluo.name="馬可波羅";
		makeboluo.hp=666;
		makeboluo.damage=60;
		
		Hero libai = new Hero();
		libai.name="李白";
		libai.hp=666;
		libai.damage=50;
		
		Hero daqiao = new Hero();
		daqiao.name="大喬";
		daqiao.hp=666;
		daqiao.damage=50;
		
		Hero xiaoqiao = new Hero();
		xiaoqiao.name="小喬";
		xiaoqiao.hp=666;
		xiaoqiao.damage=50;
		
		//實現Runnable介面,建立執行緒的方法
		KillThread killThread1=new KillThread(makeboluo,libai);
		new Thread(killThread1).start();
		
		KillThread killThread2=new KillThread(daqiao,xiaoqiao);
		new Thread(killThread2).start();
	}
}

方式三:匿名類

package create_thread;

public class TestThread3 { 
	    public static void main(String[] args) {
	          
			//建立物件
			Hero makeboluo = new Hero();
			makeboluo.name="馬可波羅";
			makeboluo.hp=666;
			makeboluo.damage=60;
			
			Hero libai = new Hero();
			libai.name="李白";
			libai.hp=666;
			libai.damage=50;
			
			Hero daqiao = new Hero();
			daqiao.name="大喬";
			daqiao.hp=666;
			daqiao.damage=50;
			
			Hero xiaoqiao = new Hero();
			xiaoqiao.name="小喬";
			xiaoqiao.hp=666;
			xiaoqiao.damage=50;
	          
	        //匿名類
	        Thread t1= new Thread(){
	            public void run(){
	                //匿名類中用到外部的區域性變數teemo,必須把teemo宣告為final
	                //但是在JDK7以後,就不是必須加final的了
	                while(!libai.isDead()){
	                    makeboluo.attrackHero(libai);
	                }              
	            }
	        };	         
	        t1.start();
	          
	        Thread t2= new Thread(){
	            public void run(){
	                while(!xiaoqiao.isDead()){
	                    daqiao.attrackHero(xiaoqiao);
	                }              
	            }
	        };
	        t2.start();         
	    }
}