Android Thread ModeliMurat Can ALPAYv1.0
Murat Can ALPAYlinkedin.com/in/mcalpaymcatr.blogspot.com
İçerik● Tekil Thread Modeli● Uzun süreli görevler● Thread API
Kime Yönelik● Yeni Başlayan● Thread ve örüntüleri
Tekil Thread ModeliUI ThreadDrawHandleEventsResumeActivities
Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();}});
Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();syn...
Tekil Thread ModeliUI ThreadDrawHandleEventsResumeActivities
Tekil Thread Modeli● Tüm GUI çatıları tekil thread modelikullanıyor.
Çoklu Thread Modeli Mümkün mü?From Brian Goetzs"Java Concurrency in Practice" Book9.1 Why are GUIs single-threaded?Single-...
Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();syn...
Solution 1btnSave.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {new Thread(new Runnable...
İlerlemeyi Görünür Kıl
Problem 2public void onClick(View v) {new Thread(new Runnable() {public void run() {...btnSave.setImageResource(R.drawable...
Problem 2android.view.ViewRootImpl$CalledFromWrongThreadException:Only the original thread that created a view hierarchy c...
Thread SafeBrian Goetz "Java Concurrency in Practice""A class is thread-safe if it behaves correctly when accessedfrom mul...
Thread API● android.app.Activity.runOnUiThread(Runnable)● android.view.View.post(Runnable)● android.os.AsyncTask
runOnUiThread vs. post● küçük farklılık?● Şu anki thread UI threadi ise Runnable çalıştırılırdeğilse mesaj kuyruğuna eklen...
Solution 2.1public void onClick(View v) {new Thread(new Runnable() {public void run() {NoteListActivity.this.runOnUiThread...
Solution 2.2public void onClick(View v) {new Thread(new Runnable() {public void run() {btnTest.post(new Runnable() {@Overr...
İlerlemeyi Görünür Kıl
Solution 3final ProgressControllerAnimationThread progressT =new ProgressControllerAnimationThread(btnSave,R.drawable.save...
AsyncTask● android.os.AsyncTask<Params, Progress, Result>● execute()
AsyncTask● onPreExecute()● doInBackground(Params...)○ publishProgress(Progress...)● onProgressUpdate(Progress...)● onPostE...
AsyncTaskpublic void onClick(View v) {AsyncTask<Void, Integer, Void> testTask = new AsyncTask...{protected Void doInBackgr...
android.os.(Handler & Looper)● Thread için mesaj kuyruğu● Handler○ handleMessage○ sendMessage○ post
Handler & Looper// Looper is already defined for UI Threadhandler = new Handler() {public void handleMessage(Message msg) ...
android.os.(Handler & Looper)● Looper○ prepare○ loop● Thread.run metodu içinde çalışmalı
Thread Öncelikleri● Arkaplan threadleri çok daha az öncelikle çalışıyor
Özet● Kullanıcı deneyimi● Tekil Thread Modeli● 3 metot○ post○ runOnUIThread○ AsyncTask
Teşekkürler?
Upcoming SlideShare
Loading in...5
×

Android Thread Modeli

540

Published on

UI Thread'ine yaptırmak için nasıl iş planlanır?
runOnUIThread, post, AsyncTask ve Handler&Looper kullanımı

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
540
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Android Thread Modeli"

  1. 1. Android Thread ModeliMurat Can ALPAYv1.0
  2. 2. Murat Can ALPAYlinkedin.com/in/mcalpaymcatr.blogspot.com
  3. 3. İçerik● Tekil Thread Modeli● Uzun süreli görevler● Thread API
  4. 4. Kime Yönelik● Yeni Başlayan● Thread ve örüntüleri
  5. 5. Tekil Thread ModeliUI ThreadDrawHandleEventsResumeActivities
  6. 6. Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();}});
  7. 7. Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();synchronizeOnWeb(); // long running operation,// or Thread.sleep(...}});
  8. 8. Tekil Thread ModeliUI ThreadDrawHandleEventsResumeActivities
  9. 9. Tekil Thread Modeli● Tüm GUI çatıları tekil thread modelikullanıyor.
  10. 10. Çoklu Thread Modeli Mümkün mü?From Brian Goetzs"Java Concurrency in Practice" Book9.1 Why are GUIs single-threaded?Single-threaded GUI frameworks are not unique to Java; Qt, NextStep,MacOS Cocoa, X Windows, and many others are also single-threaded. Thisis not for lack of trying; there have been many attempts to writemultithreaded GUI frameworks, but because of persistent problems withrace conditions and deadlock...UI Thread UI ThreadUI ThreadUI ThreadUI Thread
  11. 11. Problem 1btnSave.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {saveInternalStorage();synchronizeOnWeb(); // long running operation,// or Thread.sleep(2000)}});
  12. 12. Solution 1btnSave.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {new Thread(new Runnable() {@Overridepublic void run() {longRunningOperation();}}).start();}});
  13. 13. İlerlemeyi Görünür Kıl
  14. 14. Problem 2public void onClick(View v) {new Thread(new Runnable() {public void run() {...btnSave.setImageResource(R.drawable.progress1);...}}).start();}
  15. 15. Problem 2android.view.ViewRootImpl$CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views.at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)...at android.widget.ImageView.setImageResource(ImageView.java:352)at mca.activities.NoteListActivity$3$1.run(NoteListActivity.java:69)at java.lang.Thread.run(Thread.java:856)
  16. 16. Thread SafeBrian Goetz "Java Concurrency in Practice""A class is thread-safe if it behaves correctly when accessedfrom multiple threads, regardless of the scheduling orinterleaving of the execution of those threads by theruntime environment, and with no additionalsynchronization or other coordination on the part of thecalling code."
  17. 17. Thread API● android.app.Activity.runOnUiThread(Runnable)● android.view.View.post(Runnable)● android.os.AsyncTask
  18. 18. runOnUiThread vs. post● küçük farklılık?● Şu anki thread UI threadi ise Runnable çalıştırılırdeğilse mesaj kuyruğuna eklenir.● Runnableın UI Threadinin mesaj kuyruğunaeklenmesini sağlar.
  19. 19. Solution 2.1public void onClick(View v) {new Thread(new Runnable() {public void run() {NoteListActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {btnTest.setImageResource(R.drawable.progress1);}});}}).start();}
  20. 20. Solution 2.2public void onClick(View v) {new Thread(new Runnable() {public void run() {btnTest.post(new Runnable() {@Overridepublic void run() {btnTest.setImageResource(R.drawable.progress1);}});}}).start();}
  21. 21. İlerlemeyi Görünür Kıl
  22. 22. Solution 3final ProgressControllerAnimationThread progressT =new ProgressControllerAnimationThread(btnSave,R.drawable.save);progressT.start();new Thread(new Runnable() {@Overridepublic void run() {...boolean result = longRunningOperation();progressT.end(result);}}).start();
  23. 23. AsyncTask● android.os.AsyncTask<Params, Progress, Result>● execute()
  24. 24. AsyncTask● onPreExecute()● doInBackground(Params...)○ publishProgress(Progress...)● onProgressUpdate(Progress...)● onPostExecute(Result)
  25. 25. AsyncTaskpublic void onClick(View v) {AsyncTask<Void, Integer, Void> testTask = new AsyncTask...{protected Void doInBackground(Void... params) {...while (!opComplete()) {doApartOfLongRunningOp();if (System.currentTimeMillis() - changeTime > 100) {changeTime = System.currentTimeMillis();publishProgress(progress++);}}return null;}protected void onProgressUpdate(Integer... values) {...}};testTask.execute();}
  26. 26. android.os.(Handler & Looper)● Thread için mesaj kuyruğu● Handler○ handleMessage○ sendMessage○ post
  27. 27. Handler & Looper// Looper is already defined for UI Threadhandler = new Handler() {public void handleMessage(Message msg) {btnSave.setImageResource(R.drawable.add);}};btnSave.setOnClickListener(new OnClickListener() {public void onClick(View v) {new Thread(new Runnable() {public void run() {...handler.sendMessage(new Message());}}).start();}});
  28. 28. android.os.(Handler & Looper)● Looper○ prepare○ loop● Thread.run metodu içinde çalışmalı
  29. 29. Thread Öncelikleri● Arkaplan threadleri çok daha az öncelikle çalışıyor
  30. 30. Özet● Kullanıcı deneyimi● Tekil Thread Modeli● 3 metot○ post○ runOnUIThread○ AsyncTask
  31. 31. Teşekkürler?

×