Sunday, April 22, 2012

A short and simple explanation about Threads in java

Multi-tasking :  it involves having multiple process (playing music and using text editor) -- Heavy weight

Multi-threading: it involves having mulitple threads(in text editor editing and printing) -- Light Weight

Creating a thread
- Runnable Interface
- Thread Class

- Using Runnable Interface

    Extend Runnable interface and use run method in it.

ex :


class B
{
       PSVM()
     {
          A a_ref= new A();
         a_ref.UseThread();
    }
}





  class A implements Runnable
{

    public void UseThread()
      {
              Thread new_thread = Thread(this, "new thread");
                 new_thread.start();
       }
       private void run()
      {
                // some code
      }
}



- Using Thread Class


  class A extends Thread
{

    public void UseThread()
      {
                 start();
       }
       private void run()
      {
                // some code
      }
}



// its better to implement runnable





Multiple Threads




class B
{
       PSVM()
     {
          A a_ref= new A();
         a_ref.UseThread("one");
             a_ref.UseThread("two");
         a_ref.UseThread("three");

    }
}





  class A implements Runnable
{

    public void UseThread(String threadname)
      {
              Thread new_thread = Thread(this, threadname);
                 new_thread.start();
       }
       private void run()
      {
                // some code
      }
}



using isalive and join methods


// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}






Do synchronize with  synchronized keyword or use synchrnoized(object) {  // some code }








Friday, April 20, 2012

Changing Size of SeekBar programatically in android


I had a scienario to change size of seekbar by changing the visibility of surrounding views. Took a while but below is small code snipped which may be helpful for sme1...


RelativeLayout.LayoutParams abc=new RelativeLayout.LayoutParams(526,30);
abc.addRule(RelativeLayout.CENTER_VERTICAL); abc.addRule(RelativeLayout.RIGHT_OF,R.id.pause);
mSeekBar.setLayoutParams(abc);