Android中线程又是一个什么样的存在?

本文介绍了计算机科学中的基本概念——线程,并详细探讨了多线程编程的概念及其在Android开发中的应用。文章对比了进程与线程的区别,讨论了多线程如何实现并发执行以及不同多线程模型的特点。

http://www.wideskills.com/android/intrprocess-communication/threads-in-android

12.2 Threads in Android


  • Hello readers!! Hope you are doing well. Today we shall learn about threads. Just like a cotton thread which you use to stitch clothes threads in computer science is one of the indispensible phenomenon about which you should be aware of. Let us not waste time and learn threads.

    12.2.1 Introduction

    Thread can be understood as a process is a program which executes a single thread of instruction. We can say single thread of control allows the process to perform only one task at one time. For example when we run a word processor we can say a process is running word processor which is a program and it is under control o a single thread of execution. We cannot type numbers and characters at same time and run the spell checker concurrently or simultaneously. Modern operating systems have modified the process concept to allow a process to have multiple threads of execution so that more than one task can be accomplished at the same time.

    A thread comprises a thread ID, a program counter, a register set, and a stack. Thread has a sharing nature hence it can share its code section, data section, and other operating-system resources, such as open files and signals with other threads belonging to same process. Thread is considered to be a basic unit of CPU utilization.

    Processes are known as traditional or heavy-weight by this we mean that process has a single thread of execution. When a process can support multiple threads it can perform multiple tasks.

    Figure single threaded process and multithreaded process

    12.2.2 Multithreading

    There is a popular concept in programmers’ world called multithreaded programming. A multithreaded program is one which contains more than one part which can run concurrently. Each part of this type of program is called a thread. A thread defines a separate path of execution. So we can say multithreaded program is a synonym of multitasking. By multitasking we mean accomplishment of more than one task for example in your windows operating system you can listen to audio songs and program in android at the same time. Now multitasking can be broadly categorized into two types:

    1. Process-based multitasking
    2. Thread-based multitasking

    We already know about processes, isn’t it!! Process-based multitasking allows two or more programs to execute concurrently as we all know process is a program in execution. A program is the smallest unit of code which can be dispatched by the scheduler in process-based multitasking.

    Thread based multi-tasking implies to an environment where thread is the smallest unit of code which can be dispatched. This implies that a single program can perform more than one tasks at a time. Thread-based multitasking handles small details in a code.

    Multitasking threads requires less overhead with respect to multitasking processes. Processes require their own addresses spaces. When we talk about interprocess communication it becomes limited and expensive. Context switching in between processes becomes unaffordable at times. Threads are considered as light weight as they share same address space and switching from one thread to next is less expensive.

    With multithreading we can write impressive and efficient programs. They make the best use of CPU as idle time can be kept minimum. When we talk about networking this is important as idle time is very common in such environment for example rate of transmission of data packets over network might be fast slow as compared to the rate at which computer can process it.

    In single threaded environment program has to wait for each of the task to complete before it can proceed to next one even though CPU is idle. With multithreading we can gain access to idle time and make use of it.

    12.2.3 Models of Multithreading

    Support for threads can be provided at two levels, namely-

    • User-level
    • Kernel level

    User threads are the one which are supported above the kernel and are managed without kernel support, Kernel threads are supported and managed directly by the operating system. Operating systems like Windows XP, Linux, Mac OS X, Solaris, and Tru64UNIX support kernel threads. Relationship status between user threads and kernel threads gives rise to three models and they are-

    • Many-to-one model
    • One-to-one model
    • Many-to-many model

    Let us have a brief idea about these models

    12.2.3.1 Many-to-one model

    In this model many user threads are supported by one kernel thread. Thread is managed by thread library in user space. It is efficient. But if one thread makes a blocking system call then entire process is blocked. Only one thread can access kernel hence so multiple threads cannot run on a multiprocessor parallel as it is supposed to be. 

    Figure many to one model

    12.2.3.2 One to one model

    In this model each user thread is mapped by one kernel thread. This model allows multiple threads to run in parallel on multiprocessors. This model provides more concurrency as if any one of the makes a system blocking call the entire process is not hampered from continuing its task. Creating one user thread corresponding to each kernel thread is an overhead for system. As creating an kernel thread can create overhead for application and in turn it will hamper the performance of application as a whole. The one-to-one model allows for greater concurrency. But the developer has to be careful not to create too many threads within an application.

    Figure One to one model

    12.2.3..3 Many to many model

    In this model there are many user threads with respect to equal or smaller number of kernel threads. Number of kernel threads may be specific to either a particular application or a particular machine. This model allows you to create as many as user threads but true concurrency is not achieved as kernel can schedule only on kernel thread at a time. Developers are allowed to create as many user threads as necessary The corresponding kernel threads can run in parallel on a multiprocessor. If a thread performs a blocking system call then operating system can issue another kernel thread for execution.
      

    Figure many to many model

    Congratulations guysJ!! We are done with this section. Hope you learned something interesting and useful. See you in the next section. Till then keep practicing. Happy App developing!!!

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值