What Thread, Main Thread? Part I

By Martín Zangl – Android Developer at Santex

mobile-learning

Public Static Void Main

As we know, every single Java app starts with call to method public static void main(). This is true for Java desktop, JEE servlets, and Android applications.

When Android boots, it uses a simple process called ZygoteInit. Zygote (Wikipedia) is a cell that can divide asexually by mitosis to produce identical offspring. This is a VM Dalvik process that loads the most common classes of the Android SDK on a thread, and then waits. When starting a new Android application, the Android system forks the ZygoteInit process. The thread in the child fork stops waiting and calls ActivityThread.main()

Here is what it’s essentially doing:

public class ActivityThread {

public static void main(String... args) {

Looper.prepare();

Looper.setMainLooper(Looper.myLooper());

Looper.loop();
}
}

Looper? What is a Looper?

Here we can find a particular concurrency pattern called Thread-Specific Storage. This pattern allows multiple threads to use one “logical global” access point to retrieve an object that is local to a thread, without incurring locking overhead on each object access.

articulo

When a Android application starts, it creates a Looper object. A Looper object contains a static ThreadLocal object, a Message Queue, and the Thread reference that created it. In other words, a Looper implements a Thread-specific event loop. Using a Looper is a good way to process messages serially on one Thread.

Looper.prepare(); This operation associates the looper with the current thread and storage it in a static ThreadLocal.
Looper.loops(); will process each message in the queue, and block when the queue is empty.

As previously said, each looper is associated with one thread. To retrieve the Looper associated to the current thread you can use the method Looper.myLooper().

An example of using a Looper with a Thread:

class HandlerThread extends Thread {
Looper looper;
public void run() {
Looper.prepare();
looper = Looper.myLooper();
Looper.loop();
}
}

Handlers is the natural companion to a looper. Looper has a Message queue and Handlers put messages or runnable objects in place and process each of them, i.e. it allows them to send messages to a looper message queue from any thread and handle messages dequeued on the thread associated to that looper.

You can associate many handlers to one looper. The looper delivers the message to one specific handler.

A default Handler constructor associates it with current the thread and its looper. We could define the other Looper if we want in order to post messages to the Message queue of whatever Looper we want.

The looper instanced in the void main method is called Main Looper, part of the Main Thread. This particular thread is in charge of dispatching events to the UI widgets that include the drawing events. All components (other Activities, Services, Broadcast Receivers and Content Providers) that run in the same process are instanced in the Main Thread.

When an application performs intensive work in response to user interactions, this single thread model can yield poor performance. If everything is happening in the Main Thread it will block the whole UI, so no events can be dispatched, including drawing events. That is when a ANR (Application Not Responding) dialog appears. In that case, we just put all the long background operations in another thread, but other problems arise in paradise – only the main thread can process UI widgets. So, we need to somehow handle communication between threads.

Next post we’ll talk about:
Using custom handlers and other threads
Asynctasks
Loaders
IntentServices

About the Author  – Martín Zangl is an experienced Android Developer at Santex,  passionate about his work.  Martín is continuously learning and training to investigate new technologies.

Source:
https://corner.squareup.com/2013/10/android-main-thread-1.html
http://developer.android.com/reference/android/os/Looper.html
http://developer.android.com/guide/components/processes-and-threads.html

7 tips for new Project Managers

IMG_2912

By Johan Tabori – Project Manager at Santex

The project management role has evolved very rapidly over the last two decades. The traditional view of “boss” has morphed into a combination of “facilitator” and “coordinator” which fits better in agile organizations. The following tips may help you to improve your project management skills in today’s increasingly changing business environments:

1. Ensure healthy and friendly work environments.
Fear is the worst incentive to work, therefore, make sure the team work environment is as friendly as it can be, your teammates will trust you and you will be able to delegate responsibilities more adequately. Same works for the client, team dynamics can be affected by how good a relationship with the client is, so don’t be afraid to demand collaboration and mutual respect.

2. Know your team and client.
As a project manager, you need to be a little bit of a psychologist. As you move forward in the project, it’s very important to understand both your team’s and client’s mindsets. This will become very helpful when bringing up sensitive issues or when you need to resolve conflicts.

3. Quickly respond to communications.
Unless you are in a meeting or have an emergency, don’t wait too long to respond an email or call. If you’re not sure about the topic in discussion, don’t be afraid to ask for clarifications. If it requires further investigation, let the sender know and promise you will looking into it and get back to him/her as soon as possible.

4. Review project status with your client periodically.
Weekly follow up meetings is the best way to keep your client and/or stakeholders informed. Make sure you cover key aspects of the project such as current status, scope, milestones, communications and team updates.

5. Involve your team as much as possible in key decisions regarding the project.
In most cases, your team have more technical skills than you do, so instead of imposing any particular technology, architecture or framework, make sure these are agreed within the team.

6. Take the opportunity to learn.
Self centered project managers fail miserably in accomplishing long term goals . Acknowledge your weaknesses and information gaps and learn from every team member and client.

7.  Demonstrate your value as a service provider before asking for more business.
Clients hate when they are presented with prospective new businesses at the beginning of the contract. They want to see you “in action” before they can start thinking about it. Once you have proved you deliver value to their organization, they will come to you and discuss future projects.

 About the Author
Johan Tabori – Johan’s education as an informatics engineer prepared him well to become the project manager that he is today. A natural multi-tasker, he has been leading IT project teams in a variety of vertical markets and applications for more than 10 years.