broken down into multiple instructions | combing1のブログ

combing1のブログ

ブログの説明を入力します。

Most mobile devices have become faster and faster, but they are in fact not considered very fast. If you want your APP both can withstand some of the Nike Air Max complex work without affecting the user experience, then the task must be executed in parallel. On Android, we use threads. A cup of coffee, then read this article carefully. I'll tell you about the concept of threads, as well as how to use threads in Java, how to use the thread Handler and so on. If you want to use asynchronous processing or parallel tasks, then you will use thread. What is a thread? Thread or a thread of execution is essentially a bunch of commands (also program code), and then we send it to the operating system. In general, our CPU core can only process one thread at any one time. Multicore processors (Most Android devices already are multicore) As the 579756 403 Purple White Nike Black Mamba 24 Kobe Sale name suggests, is that we can handle simultaneous multithreading (More simply more things that we can handle at the same time). Multicore processing single-core and essence of the above multi-tasking, I say that in general, not all descriptions are necessarily correct. Because monocytes can also be used to simulate multi-tasking multi-threaded. Each running task in the thread can be broken down into multiple instructions, and these instructions are not executed simultaneously. Therefore, the single-core devices can switch to the first thread to execute 1 instruction 1A, 2 and then switch to the thread to execute instruction 2A, then go back to the thread 1 execution 1B, 1C, 1D, and then continue to switch to the thread 2, the implementation 2B, 2C, etc., and so on. This switching between threads very quickly that will happen in single-core devices. Almost all of the task processing threads are in the same time. In fact, this is because the illusion caused by too fast, just like the movie 'The Matrix' in Brown's agent, as can changing a lot of head and hands. Next we look at some code. Java core in the thread in Java, if you want to do parallel processing tasks, then your code will execute Runnable inside. Can inherit the Thread class, or implement Runnable interface: // Version 1 public class IAmAThread extends Thread {public IAmAThread () {super (\u0026 quot; IAmAThread Air Max 2012 Black Navy Blue White \u0026 quot;);}Override public void run () {// your code (sequence of instructions )}} // to execute this sequence of instructions in a separate thread new IAmAThread () start ();.. // Version 2 public class IAmARunnable implements Runnable {Override public void run () {// your code (sequence of instructions)}} // to execute this sequence of instructions in a separate thread IAmARunnable myRunnable = new IAmARunnable ();. new Thread (myRunnable) .start (); These two methods are basically the same. The first version is to create a Thread class, the second version is the need to create a Runnable object, and then also need a Thread class to call it. The second edition is generally recommended method. It is also a great theme, and is beyond the scope of this article, discussed the future will do. Whenever a thread started on Android APP, all components are run in a separate thread (the default) - called the main thread. This thread is mainly used for the processing operations and to view UI components and small parts distribution events, so the main thread is also called UI thread. If you run a time-consuming operation on the UI thread, then the UI will be locked until the end of this time-consuming operation. For the user experience, this is very bad! That is why we want to understand the threading mechanism on Android. Understanding these mechanisms could put some complex work to move to other threads to execute. If you run a time-consuming task in the UI thread, it is likely to ANR (application no response) occurs, so that users will quickly end off your APP. Android and Java, as it supports the use of Java inside the Thread class to step tasking. Therefore, like the above can easily be used as an example of Java threads on Android, but it seems still a little difficult. Why use a standard Java in the Android thread difficulties? In fact, parallel tasking did not imagine so simple, you must ensure that multiple threads concurrently, as great as Tim Bray said: ordinary humans can not do concurrency at scale (or really at all) ... especially come for Android He said following function is slightly bloated: asynchronous for UI thread is a major PITA (if you need to update the main thread interface, then you New Nike Free Run 3 Shoes Silver 3 will be used in a background thread). If the screen orientation or screen configuration change, there will be some more strange phenomenon. Because changing the screen orientation will cause Activity reconstruction (so you need to change the background thread state of the destroyed Activity, and if the background thread not above the UI thread, then the situation will be more complicated, New Nike Free 5.0 V4 Grey Blue Running Shoes because such condition 1). For the thread pool, there is no default approach. Cancel threads operation requires custom code. So how on Android task concurrent processing it? You may have heard some of the Android some common Air Jordan Outlet nouns: 1, Handler which is detailed topics we want to discuss today. 2, AsyncTask use AsyncTask is the easiest way to manipulate threads on Android, is also the most error-prone ways. 3, IntentService this approach need to write more code, but this is the time-consuming task to move to the back of a good way, it is my favorite way. EventBus mechanism coupled with the use of a framework such as Otto, so it is very easy to achieve IntentService. 4, Loader for handling asynchronous tasks, there are many things to be done, such as from a database or a content provider, where some of the data processing. 5, Service Service if you've ever used it, you should know that there will be a little misunderstanding, one common misconception is that the service is running in the background thread. In fact, not! It seems to run in the background because they are not associated with UI components, but they (the default) is run on the UI thread ...... so by default to run on the UI thread, not even in the top UI components. If you want the service to run in a background thread, you must customize a thread, and then the operation code running in the thread (with the above-mentioned method is very similar). In fact, you should use IntentService achieve, but this is not the topic of this paper. Handler Android on the following is from the Android developer documentation for Handlers: excerpt of passage: New Nike Free 5.0 V4 Grey Lime Trainers \u0026 gt; A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue Each Handler instance is associated with a single thread and. . that thread's message queue When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it - from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue. To better understand this concept, maybe you need to look at what is Message Queues. The basic message queue thread has called a 'message queue' thing, which is responsible for inter-thread communication. This is a design mode, all control commands or transfer content between threads. Message Queue as its name implies, the thread, it is an instruction queue. Here we can also Air Max 2011 Womens Purple Grey do some cool Nike Jordan Melo B Mo things: timing of messages and threads was executed at some point in time. You need to go in another thread added to the team action, but not in this thread. Note: That's 'message' and Runnable objects, concepts instruction queue is the same. Back Handler Android on ...... If you read carefully, you can see that the document had this to say:. \u0026 Gt; A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue so Handler allows you to thread Queue Message: \u0026 gt; Each Handler instance is associated with a single thread and that thread's message queue a Handler object can only be a thread associated with:. \u0026 gt; When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it so in the end and which thread a Handler associate it? It is to create a thread. \u0026 Gt; - from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue, in our understanding of this knowledge, please read ...... Tips: Here are a few point you may not know. Each thread with a Handler class instance bindings, and can run with other threads, communicate with each other. There is also a small suggestion Nike Lebron 10(X) (if used AsyncTask words), AsyncTask Handler is the use of internal processing, Air Max 2011 Womens Red but it does not run in the UI thread, it will provide a channel for communication and the UI Air Jordan Heel thread, use postExecute method can be realized. This is kinda cool, then how to create Handler it? There are two ways: using the default constructor: new Handler (). Use parameterized constructor, the parameter is a Runnable object or callback object. What practical Handler API inside it? Remember: Nike Free Run 3 Handler simply sending a message to the message queue it (or use the post way) they have a more convenient way to help with the UI thread communication. If you now look at Handler's API, you can clearly see this several ways: postpostDelayedpostAtTime code sample code here is very basic, but you can take a look at the comment. Example 1: Using Handler's 'post' method public class TestActivity extends Activity {// ... // all standard stuffOverride public void onCreate (Bundle savedInstanceState) {// ... // all standard stuff // we ' re creating a new handler here // and we're in the UI Thread (default) // so this Handler is associated with the UI thread Handler mHandler = new Handler (); // I want to start doing something really long // which means I should run the fella in another thread // I do that by sending a message -. in the form of another runnable object // But first, I'm going to create a Runnable object or a message for this Runnable mRunnableOnSeparateThread = new 2015 Nike Free 5.0 Runnable () {Override public void run () {// do some long operation longOperation (); // After mRunnableOnSeparateThread is done with it's job, // I need to tell the user that i'm done // which means I need to send a message back to the UI thread // who Nike Free Run 3 Women do we know that's associated with the UI thread? mHandler.post (new Runnable () {Override public void run () {// do some UI related thing // like update a progress bar or TextView // ....}});}}; // Cool but I've not executed the mRunnableOnSeparateThread yet // I've only defined the message to be sent // When I execute it though, I want it to be in a different thread // that was the whole point new Thread (mRunnableOnSeparateThread) .start ();.}} If you simply do not Handler object, the callback post method would be more difficult. Example 2: Method using postDelayed site recently introduced a new feature, I have every time simulation EditText the AutoComplete feature, after each text changes will trigger a call to an API to retrieve data from the server. I want to reduce the number of APP API calls, so I decided to use the postDelayed Handler method to achieve this function. This example is not for parallel processing, only about Handler sends a message to the message queue messages also arrange some point in the future execution. // The below code is inside a TextWatcher // which implements the onTextChanged method // I've simplified it to only highlight the parts we're // interested in private long lastChange = 0;Override public void onTextChanged (final CharSequence chars , int start, int before, int count) {// The handler is spawned from the UI thread new Handler (). postDelayed (// argument 1 for postDelated = message to be sent new Runnable () {Override public void run ( ) {if (noChangeInText_InTheLastFewSeconds ()) {searchAndPopulateListView (chars.toString ()); // logic}}}, // argument 2 for postDelated = delay before execution 300); lastChange = System.currentTimeMillis ();} private boolean noChangeInText_InTheLastFewSeconds () {return System.currentTimeMillis () - lastChange \u0026 gt; = 300} Finally, I put 'postAtTime' This method is left to the reader as an associate, and master Handler it? If so, then you can enjoy the use of threads.Android threads and Handler Basics