Java tutorial
/* * Copyright 2013 wada811<at.wada811@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package at.wada811.android.library.demos.concurrent; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import at.wada811.android.library.demos.R; import at.wada811.utils.LogUtils; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * ExecutorService?????? | DevAchieve * http://wada811.blogspot.com/2013/11/executor-service-that-multi-thread-task-queue-in-java.html */ public class ExecutorActivity extends FragmentActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_executor); ((Button) findViewById(R.id.button1)).setText("newSingleThreadExecutor"); ((Button) findViewById(R.id.button1)).setOnClickListener(this); ((Button) findViewById(R.id.button2)).setText("newFixedThreadPool"); ((Button) findViewById(R.id.button2)).setOnClickListener(this); ((Button) findViewById(R.id.button3)).setText("newCachedThreadPool"); ((Button) findViewById(R.id.button3)).setOnClickListener(this); ((Button) findViewById(R.id.button4)).setText("newScheduledThreadPool"); ((Button) findViewById(R.id.button4)).setOnClickListener(this); ((Button) findViewById(R.id.button5)).setText("newSingleThreadScheduledExecutor(1)"); ((Button) findViewById(R.id.button5)).setOnClickListener(this); ((Button) findViewById(R.id.button6)).setText("newSingleThreadScheduledExecutor(2)"); ((Button) findViewById(R.id.button6)).setOnClickListener(this); ((Button) findViewById(R.id.button7)).setText("AtFixedRate(1)"); ((Button) findViewById(R.id.button7)).setOnClickListener(this); ((Button) findViewById(R.id.button8)).setText("AtFixedRate(2)"); ((Button) findViewById(R.id.button8)).setOnClickListener(this); ((Button) findViewById(R.id.button9)).setText("WithFixedDelay(1)"); ((Button) findViewById(R.id.button9)).setOnClickListener(this); ((Button) findViewById(R.id.button10)).setText("WithFixedDelay(2)"); ((Button) findViewById(R.id.button10)).setOnClickListener(this); } @Override public void onClick(View v) { LogUtils.d(); switch (v.getId()) { case R.id.button1: newSingleThreadExecutorTest(); break; case R.id.button2: newFixedThreadPoolTest(); break; case R.id.button3: newCachedThreadPoolTest(); break; case R.id.button4: newScheduledThreadPoolTest(); break; case R.id.button5: newSingleThreadScheduledExecutorTest(); break; case R.id.button6: newSingleThreadScheduledExecutorDuringExecutionTest(); break; case R.id.button7: newSingleThreadScheduledExecutorAtFixedRateTest(); break; case R.id.button8: newSingleThreadScheduledExecutorAtFixedRateDuringExecutionTest(); break; case R.id.button9: newSingleThreadScheduledExecutorWithFixedDelayTest(); break; case R.id.button10: newSingleThreadScheduledExecutorWithFixedDelayDuringExecutionTest(); break; default: break; } LogUtils.d(); } /** * {@link Executors#newSingleThreadExecutor} ? * * <p> * SingleThread ??????????????? * </p> */ public void newSingleThreadExecutorTest() { LogUtils.d(); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(new ExecutorRunnable("A", 1)); executorService.submit(new ExecutorRunnable("B", 1)); executorService.submit(new ExecutorRunnable("C", 1)); executorService.submit(new ExecutorRunnable("D", 1)); } /** * {@link Executors#newFixedThreadPool(int)} ? * * <p> * ????? * </p> */ public void newFixedThreadPoolTest() { LogUtils.d(); ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(new ExecutorRunnable("A", 1)); executorService.submit(new ExecutorRunnable("B", 1)); executorService.submit(new ExecutorRunnable("C", 1)); executorService.submit(new ExecutorRunnable("D", 1)); } /** * {@link Executors#newCachedThreadPool} ? * * <p> * ?????????? * </p> */ public void newCachedThreadPoolTest() { LogUtils.d(); ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(new ExecutorRunnable("A", 1)); executorService.submit(new ExecutorRunnable("B", 1)); executorService.submit(new ExecutorRunnable("C", 1)); executorService.submit(new ExecutorRunnable("D", 1)); } /** * {@link Executors#newScheduledThreadPool(int)} ? * * <p> * ??????????????????? * </p> */ public void newScheduledThreadPoolTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); executorService.schedule(new ExecutorRunnable("A", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("B", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("C", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("D", 1), 1, TimeUnit.SECONDS); } /** * {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)} ????? * * <p> * ??????????? * </p> */ public void newSingleThreadScheduledExecutorTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.schedule(new ExecutorRunnable("A", 1), 8, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("B", 1), 4, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("C", 1), 0, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("D", 1), 12, TimeUnit.SECONDS); } /** * {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)} ??? * * <p> * ??????????? * </p> */ public void newSingleThreadScheduledExecutorDuringExecutionTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.schedule(new ExecutorRunnable("A", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("B", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("C", 1), 1, TimeUnit.SECONDS); executorService.schedule(new ExecutorRunnable("D", 1), 1, TimeUnit.SECONDS); } /** * {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)} * ?????? * * <p> * ????????????????????? * </p> */ public void newSingleThreadScheduledExecutorAtFixedRateTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new ExecutorRunnable("A", 1), 1, 2, TimeUnit.SECONDS); shutdown(executorService); } /** * {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)} * ???? * * <p> * ????????????????????? <br> * ?????????????????? * </p> */ public void newSingleThreadScheduledExecutorAtFixedRateDuringExecutionTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new ExecutorRunnable("A", 3), 1, 2, TimeUnit.SECONDS); shutdown(executorService); } /** * {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)} * ????? * * <p> * ?????????????????????? * </p> */ public void newSingleThreadScheduledExecutorWithFixedDelayTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleWithFixedDelay(new ExecutorRunnable("A", 1), 1, 2, TimeUnit.SECONDS); shutdown(executorService); } /** * {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)} * ??? * * <p> * ?????????????????????? <br> * ???????????????????? * </p> */ public void newSingleThreadScheduledExecutorWithFixedDelayDuringExecutionTest() { LogUtils.d(); ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleWithFixedDelay(new ExecutorRunnable("A", 3), 1, 2, TimeUnit.SECONDS); shutdown(executorService); } /** * ExecutorService ??? * * <p> * ?????????????????????1???????<br> * UI?8???????? Handler ???????????? * </p> */ private void shutdown(final ExecutorService executorService) { new Handler().postDelayed(new Runnable() { @Override public void run() { try { LogUtils.d("shutdown"); executorService.shutdown(); if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) { LogUtils.d("shutdownNow"); executorService.shutdownNow(); } } catch (InterruptedException e) { e.printStackTrace(); LogUtils.d("shutdownNow: " + e.getMessage()); executorService.shutdownNow(); } } }, 8000); } public class ExecutorRunnable implements Runnable { private String mName; private int mSeconds; public ExecutorRunnable(String name, int seconds) { mName = name; mSeconds = seconds; } @Override public void run() { LogUtils.d(mName + ": ThreadId: " + Thread.currentThread().getId()); LogUtils.d(mName + ": start"); try { TimeUnit.SECONDS.sleep(mSeconds); } catch (InterruptedException e) { e.printStackTrace(); LogUtils.w(mName, e); } LogUtils.d(mName + ": end"); } } }