Wednesday, February 26, 2014

Share object between threads with Synchronized Statements

Last post show how to create synchronized code with Synchronized Method. Alternatively, we can synchronize block of code with Synchronized Statements.


Example code:
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 int delayDecCounter(int delay) {
   
   //do something not access the share obj
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

   synchronized (this) {
    int tmpCounter = counter;

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

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

}


The layout XML, refer to last post.


- More example about Thread

No comments:

Post a Comment