JAVA 建立一個執行緒的三種方式
阿新 • • 發佈:2018-12-11
- package multiplethread;
- import charactor.Hero;
- public class Battle implements Runnable{
- private Hero h1;
- private Hero h2;
- public Battle(Hero h1, Hero h2){
- this.h1 = h1;
- this.h2 = h2;
- }
- public void run(){
- while(!h2.isDead()){
- h1.attackHero(h2);
- }
- }
- }
- package multiplethread;
- import charactor.Hero;
- public class TestThread {
- public static void main(String[] args) {
- Hero gareen = new Hero();
- gareen.hp = 616;
- gareen.damage = 50;
- Hero teemo = new Hero();
- teemo.hp = 300;
- teemo.damage = 30;
- Hero bh = new Hero();
- bh.hp = 500;
- bh.damage = 65;
- Hero leesin = new Hero();
- leesin.hp = 455;
- leesin.damage = 80;
- Battle battle1 = new Battle(gareen,teemo);
- new Thread(battle1).start();
- Battle battle2 = new Battle(bh,leesin);
- new Thread(battle2).start();
- }
- }