Mehmet Ethem SULAN :: Test Güdümlü Yazılım(Test Driven Development) :: Java Thread Example Code
//www.ethemsulan.com
package Yonetim;
import Robotlar.*;
public class Engine extends EngineSuperSinif{
	public Engine(){
//super() ile ust sinif(extends edilen sinif) yapicisi cagriliyor
		super();
		r1Obj=new Robot1();
		r1Obj.setLocation1(0);
		r2Obj=new Robot2();
		r2Obj.setLocation2(19);
	}
public static void main(String[] args) {
//threadi baslatiyor.yani run() metodunu cagirir
		new Engine().start();
	}
//Baska uygulamanin erisilmesini engelledik(synchronized)
	public void run() {
		synchronized (r1Obj) {
//Robot1'in enerjisi ve paylasimi yapiliyor.
//Enerji<=0 oldugunda thread durduluyor
	r1Obj.unit1Dagit();
	firstLocation=r1Obj.getLocation1();
System.out.println("Robot1'in yeni konumu : "+firstLocation);
    secondLocation=r1Obj.degerDagit1(10, 0);
    r1Obj.setLocation1(secondLocation);    
harEnerHarc1(firstLocation, secondLocation);
if(saldir1()==0){
System.out.println("Robot2 etki alani disinda");
}else{
System.out.println("Robot1'in fuze etkisi : "+saldir1());
      }
if(r1Obj.getEnergy1()<=0){
System.out.println("Birinci thread yani ROBOT1 oldu");
            	this.stop();
            }
   System.out.println();
 try {
	this.sleep((int)(Math.random()*1000));
	} catch (InterruptedException e) {
				e.printStackTrace();
		}
}
//Robot2'nin enerji dagilmi ve hareketi kontrol ediliyor
synchronized (r2Obj) {
	r2Obj.unit2Dagit();
firstLocation=r2Obj.degerDagit2(19, 10);
System.out.println("Robot2'in yeni konumu : "+firstLocation);
secondLocation=rast.nextInt(10);
r2Obj.setLocation2(secondLocation);    
harEnerHarc2(firstLocation, secondLocation);
if(saldir2()==0){
System.out.println("Robot1 etki alani disinda");
}else{
System.out.println("Robot2'nin fuze " +
		"etkisi : "+saldir2());
     }
if(r2Obj.getEnergy2()<=0){
System.out.println("Ikinci thread yani ROBOT2 oldu");
            	this.stop();
            }
System.out.println();
try {
this.sleep((int)(Math.random()*100));
} catch (InterruptedException e) {
	e.printStackTrace();
			}	
		}
//surekli kendi icinde cagiriyoruz threadi
            run();	
	}
}
//-------------------------------------------------------------------
//www.ethemsulan.com
package Yonetim;
import java.util.Random;
import Robotlar.Robot1;
import Robotlar.Robot2;
public class EngineSuperSinif extends Thread {
	protected Robot1 r1Obj;
	protected Robot2 r2Obj;
	protected Random rast = new Random();
	protected int firstLocation;
	protected int secondLocation;
	private int mermiHasar;
public EngineSuperSinif() {
		super();
	}
public EngineSuperSinif(Runnable arg0) {
		super(arg0);
	}
public EngineSuperSinif(String arg0) {
		super(arg0);
	}
public EngineSuperSinif(ThreadGroup arg0, Runnable arg1) {
		super(arg0, arg1);
	}
public EngineSuperSinif(ThreadGroup arg0, String arg1) {
		super(arg0, arg1);
	}
public EngineSuperSinif(Runnable arg0, String arg1) {
		super(arg0, arg1);
	}
public EngineSuperSinif(ThreadGroup arg0, Runnable arg1, String arg2) {
		super(arg0, arg1, arg2);
	}
public int harEnerHarc1(int ilkKonum, int sonKonum) {
r1Obj.setEnergy1((Math.abs(ilkKonum-sonKonum)/
		r1Obj.getSpeed1())*5);
		return r1Obj.getEnergy1();
	}
public int harEnerHarc2(int ilkKonum, int sonKonum) {
r2Obj.setEnergy2((Math.abs(ilkKonum-sonKonum)/r2Obj.getSpeed2())*5);
		return r2Obj.getEnergy2();
	}
public EngineSuperSinif(ThreadGroup arg0, Runnable arg1, String arg2,
			long arg3) {
		super(arg0, arg1, arg2, arg3);
	}
public int saldir1() {
mermiHasar=((rast.nextInt(10)+1)*r1Obj.getAttact1()/
r1Obj.getSpeed1()-(r2Obj.getDefense2()*r2Obj.getLocation2()/10));
	if(mermiHasar>0){
		r1Obj.setEnergy1(mermiHasar);
		return mermiHasar;
		}
		return 0;
	}
public int saldir2() {
mermiHasar=((rast.nextInt(10)+1)*r2Obj.getAttact2()/
r2Obj.getSpeed2()-(r1Obj.getDefense1()*r1Obj.getLocation1()/10));
	if(mermiHasar>0){
		r2Obj.setEnergy2(mermiHasar);
		return mermiHasar;
		}
		return 0;
	}
}

Projenin rapidshare linki : http://rapidshare.com/files/369705681/Engine.zip.html

Ana Sayfa: http://www.ethemsulan.com/2010/03/cevik-surec-agile-process-ile-javada.html

Write a Comment

Let me know what you think?