简体   繁体   English

Looper,工作线程和其他并发问题

[英]Looper, worker thread and other concurrency issues

Can someone please explain to me in short the following terms, and how they relate to each other? 有人可以简单地向我解释以下条款,以及它们如何相互关联?

  1. handler - each thread has a handler? handler - 每个线程都有一个处理程序? each view has a handler? 每个视图有一个处理程序? perform the post() . 执行post() there is a default handler for the OS main thread? OS主线程有一个默认处理程序?

  2. looper - looper -

  3. looper thread - looper thread -

  4. post - adding a runnable to the end of the thread's tasks queue post - 将runnable添加到线程任务队列的末尾

  5. task - a runnable or a message task - 可运行或消息

  6. executor - manages a threads' pool to execute some tasks. executor - 管理线程池以执行某些任务。 useful if preforming the same task few times. 如果几次执行相同的任务,则非常有用。

  7. timer - timer -

  8. runnable vs message - when to use each of them? runnable vs message - 什么时候使用它们?

  9. thread's queue - each thread has a tasks queue. thread's queue - 每个线程都有一个任务队列。

Any other terms I should be familiar with regarding concurrency issues in Android? 关于Android中的并发问题,我应该熟悉的其他任何术语?

I will try to explain shortly but to fully understand those terms I'll have to practice developing android applications. 我将尽快解释,但要完全理解这些术语,我将不得不练习开发Android应用程序。

handler : an object attached to a thread (any thread) that both processes messages and executes runnables in its thread. handler :附加到线程(任何线程)的对象,它既处理消息又在其线程中执行runnables。 That is important sometimes because ie only the UI thread can do operations on views, and sometimes a background thread must use an UI thread handler to do action in the views 这有点重要,因为即只有UI线程可以对视图进行操作,有时后台线程必须使用UI线程处理程序在视图中执行操作

looper is an object that iterates through a thread message queue, popping runnables and messages and giving them to the thread handlers. looper是一个对象,它遍历线程消息队列,弹出runnables和消息并将它们提供给线程处理程序。 The UI thread comes already with a Looper running UI线程已经运行了Looper

looper thread is the thread of a given looper, nothing special here looper thread是给定looper的线程,这里没什么特别的

post , task and executor ou answered yourself already 帖子任务执行者你已经回答了自己

timer and class to manage small amounts of time. 计时器和类来管理少量时间。 Like measuring some seconds or milliseconds, wainting some amount of time and so on. 就像测量几秒或几毫秒,节省一些时间等等。

runnable vs message the main difference is: when you use runnables, the poster thread mus specify the implementation of something, ehile when you use messages, the handlers specifies the implementation of some action. runnable vs message的主要区别在于:当你使用runnables时,海报线程指定了某些东西的实现,当你使用消息时,处理程序指定了某些动作的实现。 When your working with only one poster and one handles you can use both almost interchangeably. 当您只使用一张海报和一张手柄时,几乎可以互换使用。 When you have many different threads posting the same thing to a same handler, I would use messages and vice-versa. 当你有许多不同的线程将相同的东西发布到同一个处理程序时,我会使用消息,反之亦然。

Additionally to what @hsgubert said, there's some other concepts that's worth mentioning. 除了@hsgubert所说的,还有其他一些值得一提的概念。 Once you enter the "multithread world" you should know there are many synchronization and concurrency handling methods. 进入“多线程世界”后,您应该知道有许多同步和并发处理方法。 As you're running things in paralell, you'll be running your app and once in a time, at different points, you'll probably get some beautiful ConcurrentModificacionException . 当你在并行运行时,你将运行你的应用程序,并且在不同的时间点,你可能会得到一些漂亮的ConcurrentModificacionException

In my own experience, this is one of the biggest pains (memory leaks permitting) I've ever experienced, probably because there's not a pattern how to solve them. 根据我自己的经验,这是我经历过的最大的痛苦之一(内存泄漏允许),可能是因为没有一种模式如何解决它们。 So I think you should also know these concepts: 所以我认为你也应该知道这些概念:

  • Use synchronized methods/variables whenever needed. 必要时使用synchronized方法/变量。 In methods, it will prevent another Thread enter before the current Thread has ended its execution. 在方法中,它将阻止在当前Thread结束执行之前另一个Thread进入。 It will not prevent other Thread s modify insider data structures in another methods, though, so be clear about it. 但是,它不会阻止其他Thread在其他方法中修改内部数据结构,所以要明确它。 Used as a synchronized(variable_name) { ... } block, it will prevent variable_name being modified app-wide by any other Thread until you exit that block. 用作synchronized(variable_name) { ... }块,它将阻止在任何其他Thread 应用范围内修改variable_name ,直到退出该块。

  • There are many inter- Thread synchronization methods. 有许多Thread间同步方法。 The two I use most are Semaphore and CountDownLatch (probably I use most the latter). 我最常用的两个是SemaphoreCountDownLatch (可能我使用后者最多)。 Sometimes it might happen that you don't want to execute some code portion until some other portion (or Thread ) has finished its execution. 有时可能会发生这样的情况:在某个其他部分(或Thread )完成执行之前,您不希望执行某些代码部分。 This methods safe lifes. 这种方法安全生命。

  • There are several already-concurrency-prepared data structures. 有几个已经并发准备的数据结构。 These datastructures basically already implement the above mentioned, so if you use .add() on multiple places concurrently, Java itself handles that event and manages to not show any exception. 这些数据结构基本上已经实现了上面提到的,所以如果你同时在多个地方使用.add() ,Java本身就会处理那个事件并设法不显示任何异常。 More info here . 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM