0

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?

Anoir Nechi
  • 81
  • 1
  • 10
  • Try to avoid writing [arrow code](http://blog.codinghorror.com/flattening-arrow-code/). I'd also avoid trying to handle threads yourself unless absolutely necessary, the code tends to be ugly and hard to read/update/maintain. Instead, I suggest learning and using RxJava: https://github.com/ReactiveX/RxJava/wiki – Christopher Perry Apr 24 '15 at 03:34

3 Answers3

1

The code you have presented is really bad written for Android. All the synchronization is handled via Handlers and the results are posted to one or another thread. For example if you do new Handler() you are creatting a "Thread handler" for the current thread, which by default is the main thread.

If you call handler.post(myRunnable) you will be running something in the UI thread, but you can do similar things with other threads or looping threads.

Given that, if your problem is that the UI is not being refreshed in the moment you want, the reason could be that you are not "posting" the results to the UI thread in the correct moment. So before starting the thread, create a UI handler, and from your thread post the results. Remember that you are not allowed to perform any UI operation outside the UI thread.

Quick tip

From my experience, using Thread.sleep(n) is not a good idea for synchronization between threads, use traffic lights or messages between them.

droidpl
  • 5,872
  • 4
  • 35
  • 47
0

Are you calling .start() on your thread? Also why do you try a one ms Thread.sleep ?

Charles Durham
  • 2,445
  • 16
  • 17
  • i've put **thread.start();** at the end of the function there's changes in the UI but it's not working as it should be ... and for the 1 ms i was just testing becouse i don't know the exact choice and how shall i choose it – Anoir Nechi Apr 23 '15 at 22:43
  • Hello, anyone could help me please?? – Anoir Nechi Apr 23 '15 at 23:01
0

As far as I understand, you cannot update the UI thread from other thread. To do that you have to send message to UI main thread this may help Updating Android UI using threads

Community
  • 1
  • 1
Vinh
  • 21
  • 2