i have the code bellow to execute a simulation for an android application of a car but it seems that threads are not well synchronized how can i fix this
public void Simulation()
{
ambientTemp = 20;
engTemp = 20;
mileage = 123456;
fuel = 100;
thread = new Thread()
{
menu1_Fragment f1 = new menu1_Fragment();
menu2_Fragment f2 = new menu2_Fragment();
menu3_Fragment f3 = new menu3_Fragment();
public void run()
{
for (int i=0; i<l; i++)
{
try {
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
speed = SPEED[i];
revs = ENGSPEED[i];
System.out.println(speed);
System.out.println(revs);
fuel -= 1;
engTemp += 0.5;
mileage += 1;
gear = AMP[i];
time += 1;
if (tachoFrag != null && tachoFrag.isVisible())
{
View item1 = findViewById(R.id.progressBar4);
f1.setRevs(item1,revs);
f1.setSpeed(speed);
f1.setFuelGauge(fuel);
final View item2 = findViewById(R.id.milage);
final View item3 = findViewById(R.id.ambienttemp);
final View item4 = findViewById(R.id.gear);
try {
Thread.sleep(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
f1.setMileage(item2,mileage);
f1.setAmbientTemp(item3,ambientTemp);
f1.setGear(item4,gear);
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, f1);
transaction.commit();
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
i've added some println to make suure that the loop is working and it seems fine but the UI is not refreshing as it should to be... how can i fix that?