Wednesday, February 26, 2014

Share object between threads with synchronized methods

This example show how to synchronize share object between threads with synchronized methods.

Share object between threads WITH synchronized method


Share object between threads WITHOUT synchronized method


package com.example.androidthread;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 Button buttonStart;
 TextView textInfoA, textInfoB;

 String infoMsgA;
 String infoMsgB;

 ShareClass shareObj = new ShareClass(10);

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonStart = (Button) findViewById(R.id.buttonstart);
  textInfoA = (TextView) findViewById(R.id.infoa);
  textInfoB = (TextView) findViewById(R.id.infob);

  buttonStart.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {

    infoMsgA = "Thread A\n";
    infoMsgB = "Thread B\n";
    textInfoA.setText(infoMsgA);
    textInfoB.setText(infoMsgB);

    new Thread(new Runnable() {

     boolean stop = false;

     @Override
     public void run() {

      while (!stop) {
       if (shareObj.getCounter() > 0) {

        infoMsgA += "A: "
          + shareObj.delayDecCounter(2500) + "\n";
        
        try {
         Thread.sleep(3000);
        } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

        MainActivity.this.runOnUiThread(new Runnable() {

         @Override
         public void run() {
          textInfoA.setText(infoMsgA);
         }

        });

       } else {
        stop = true;
       }
      }
     }
    }).start();

    new Thread(new Runnable() {

     boolean stop = false;

     @Override
     public void run() {

      while (!stop) {
       if (shareObj.getCounter() > 0) {

        infoMsgB += "B: "
          + shareObj.delayDecCounter(500) + "\n";
        
        try {
         Thread.sleep(500);
        } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

        MainActivity.this.runOnUiThread(new Runnable() {

         @Override
         public void run() {
          textInfoB.setText(infoMsgB);
         }

        });

       } else {
        stop = true;
       }
      }
     }
    }).start();

   }
  });

 }

 public class ShareClass {

  int counter;

  ShareClass(int c) {
   counter = c;
  }

  public int getCounter() {
   return counter;
  }

  public synchronized int delayDecCounter(int delay) {

   int tmpCounter = counter;

   try {
    Thread.sleep(delay);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   tmpCounter--;
   counter = tmpCounter;
   return counter;
  }
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/buttonstart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start()" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="with synchronized" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/infoa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/infob"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Next: Share object between threads with Synchronized Statements


- More example about Thread

No comments: