0

I am trying to implement the multi threading in android. My intention is to build three different thread and from each thread I shall update three different textView in the UI.

All the three thread are running properly.I am using the following code snippet each thread.

public class Thread1 implements Runnable{

        @Override
        public void run() {

            while(true){
                System.out.println("Thread1 Thread details :"+Thread.currentThread());                

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

Inside from different thread log message are properly showing. But when I want to show any toast message or txtView message it is not working.

09-09 17:43:17.882    1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:17.883    1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:18.826    1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]
09-09 17:43:18.917    1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:18.917    1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:19.826    1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]
09-09 17:43:19.924    1383-1406/com.example.root.multithreading0004 I/System.out﹕ Thread2 Thread details :Thread[pool-1-thread-2,5,main]
09-09 17:43:19.924    1383-1407/com.example.root.multithreading0004 I/System.out﹕ Thread3 Thread details :Thread[pool-1-thread-3,5,main]
09-09 17:43:20.827    1383-1405/com.example.root.multithreading0004 I/System.out﹕ Thread1 Thread details :Thread[pool-1-thread-1,5,main]

Here is my complete code:

package com.example.root.multithreading0004;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MainActivity extends ActionBarActivity {

    public static int num1 = 0;
    public static int num2 = 0;
    public static int num3 = 0;
    public static int num4 = 0;   

    TextView tv2;
    TextView tv3;
    TextView tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         

        tv2 = (TextView)findViewById(R.id.textView2);
        tv3 = (TextView)findViewById(R.id.textView3);
        tv4 = (TextView)findViewById(R.id.textView4);

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        System.out.println("Main START");
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();
        Thread3 thread3 = new Thread3();

        executorService.submit(thread1);
        executorService.submit(thread2);
        executorService.submit(thread3);

        System.out.println("Main END");
    }    
    public class Thread1 implements Runnable{

        @Override
        public void run() {

            while(true){
                System.out.println("Thread1 Thread details :"+Thread.currentThread());
                //Toast.makeText(MainActivity.this,"I am Thread1", Toast.LENGTH_SHORT).show();             
                //tv2.append("I am Thread1:" + num1++ +"\n");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class Thread2 implements Runnable{

        @Override
        public void run() {

            while(true){
                System.out.println("Thread2 Thread details :"+Thread.currentThread());
                //Toast.makeText(MainActivity.this,"I am Thread2",Toast.LENGTH_SHORT).show();              
                //tv3.append("I am Thread2:" + num2++ +"\n");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public class Thread3 implements Runnable{

        @Override
        public void run() {

            while(true){
                System.out.println("Thread3 Thread details :"+Thread.currentThread());
                //Toast.makeText(MainActivity.this,"I am Thread3",Toast.LENGTH_SHORT).show();              
                //tv4.append("I am Thread3:" + num4++ +"\n");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {       
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {       
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Please guide me where I am missing?

RFK
  • 83
  • 11

1 Answers1

0

You are trying to modify the views outside Main(UI) Thread. That's impossible. You should update views like this:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
               //update textviews 
            }
        });

note: runOnUiThread() is activity's method

update

 @Override
        public void run() {

            while(true){
                System.out.println("Thread1 Thread details :"+Thread.currentThread());
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      //update textviews 
                        tv2.setText("I am Thread1:" + num1++ +"\n");
                    }
                });

                //
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
Yazazzello
  • 6,053
  • 1
  • 19
  • 21
  • Hi Yazazzello, Thanks for your valuable guidelines. I am newbie in android. Could you please tell me where should I write this code? Please, mention it in my code. I shall be very thankful to you. – RFK Sep 10 '15 at 05:54