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!!!

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值