1. Download our Official Android App: Forums for Android!

Apps Android Process and thread

Discussion in 'Android Development' started by wsung, Jun 20, 2016.

  1. wsung

    wsung Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jun 20, 2016

    Jun 20, 2016
    3
    0
    5
    Male
    I am confused by the process and thread when i try to develop an android app ? for my understanding, a 'process' is the environment that allocated by the OS so that my program can use the limited resource within the environment (Process) for example, memory, CPU, hard disk space (Please correct me if i am wrong, Thanks a Lots), while the thread mean the execution of program by using the resource within the environment (Process)

    So now, if i create more than one thread within the process, my program (activity) still limited by the resource i am able to utilize, that mean my program (activity) will not become faster if i create more than one thread because by using the same amount of resources, i can only finish certain amount of task. So what is the purpose i create more than one thread ? i am thinking about this question and come with a theory as below :

    when a task of a program (activity) is running, it usually not utilize all the resources within the process, so it is waste the next task just wait there until the first task is completed, so a developer want to create another thread to run simultaneously to fully utilize the resource.

    Please correct me if i am wrong. Thanks !!
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,715
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,715
    9,446
    1,488
    Male
    Software developer
    South West of England
    You can think of a thread as a 'lightweight process'. Your code can create multiple threads, and the system will share execution between them (only one thread can be executed by the CPU at any one time). Swapping between threads is so fast that all the threads appear to execute in parallel. You are right in thinking that the threads are constrained by the resources allocated to your app (process). This includes available memory.
    All threads do have their own stack space though.

    Some reasons to consider using multiple threads are:-

    1. Network access. You may need to communicate with a remote server. This will involve sending a request, and waiting for a response. This is an ideal job for a thread, which may remain in a waiting state for a relatively long time.

    2. Your application can be partitioned into separate threads which can run in parallel, to achieve better performance than a single thread.

    3. There is some long running computational task that can be handled by a thread in the background.

    The system will generally warn you if your code contains logic which holds up the main thread of your application. The main thread primarily handles all UI activity (clicking buttons etc.) Obviously it's very important that this processing isn't held up, as your app will appear slow to the user.

    Creating a thread to offload processing from the main UI thread is a common requirement, and the framework provides a class to do just this. It's called AsyncTask.
     
    scary alien likes this.
  3. wsung

    wsung Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jun 20, 2016

    Jun 20, 2016
    3
    0
    5
    Male
    this is great explanation, thanks!
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #17
    Points:
    1,488
    Posts:
    6,715
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    6,715
    9,446
    1,488
    Male
    Software developer
    South West of England
    No problem man
     

Share This Page

Loading...