Elysia Tools

在一个清晰的工作台里浏览工具、案例、可视化和主题指南。

热门分类

专题

语言
ZHESPTRUFRDEEN

Elysia Tools

导航

Java

Android Java 并发:任务、计数器与许可归属

追踪固定线程池提交、同步计数和获取许可时中断的分支。

案例

这个案例集合中的条目

代码java简单

创建线程

使用Thread类、Runnable接口和ExecutorService创建和启动线程

难度

5/10

预计时间

25 min

标签

java, android, multithreading, threads

前置条件

Basic Java knowledge, Understanding of concurrency

java
// Android Java Thread Creation Examples
// Using Thread, Runnable, and ExecutorService

import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.*;

// 1. Basic Thread Creation
class BasicThreadCreation {

    // Create thread using Thread class
    public void createBasicThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread running: " + Thread.currentThread().getName());
                // Perform background work
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread completed");
            }
        });

        thread.start();
    }

    // Create thread with custom name
    public void createNamedThread(String name) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Named thread: " + Thread.currentThread().getName());
                // Perform work
            }
        }, name);

        thread.start();
    }

    // Create thread using Runnable
    public void createThreadWithRunnable() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable thread: " + Thread.currentThread().getName());
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }

    // Create thread using lambda (Java 8+)
    public void createThreadWithLambda() {
        Thread thread = new Thread(() -> {
            System.out.println("Lambda thread: " + Thread.currentThread().getName());
            performWork();
        });

        thread.start();
    }

    private void performWork() {
        // Simulate work
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Work completed");
    }
}

// 2. Thread with Exception Handling
class ThreadExceptionHandling {

    public void createThreadWithExceptionHandling() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("Thread working...");
                    // Simulate error
                    throw new RuntimeException("Thread error!");
                } catch (Exception e) {
                    System.out.println("Caught exception in thread: " + e.getMessage());
                }
            }
        });

        thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("Uncaught exception in " + t.getName() + ": " + e.getMessage());
            }
        });

        thread.start();
    }

    // Thread with priority
    public void createThreadWithPriority() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("High priority thread running");
            }
        });

        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
    }

    // Daemon thread
    public void createDaemonThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                while (true) {
                    System.out.println("Daemon thread: " + count);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        break;
                    }
                    count++;
                }
            }
        });

        thread.setDaemon(true);
        thread.start();

        // Main thread exits, daemon thread terminates
    }

    // Thread sleep and join
    public void createThreadWithJoin() {
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread 1 starting");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 1 completed");
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread 2 starting");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2 completed");
            }
        });

        thread1.start();
        thread2.start();

        // Wait for both threads to complete
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All threads completed");
    }
}

// 3. Handler and Looper (Android Specific)
class HandlerLooperExample {

    private final Handler handler = new Handler(Looper.getMainLooper());

    // Post runnable to main thread
    public void postToMainThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Background thread: " + Thread.currentThread().getName());

                // Post result to main thread
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Main thread: " + Thread.currentThread().getName());
                        // Update UI
                    }
                });
            }
        }).start();
    }

    // Post delayed
    public void postDelayed() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.out.println("Delayed execution on main thread");
            }
        }, 2000); // 2 seconds delay
    }

    // Post at front of queue
    public void postAtFrontOfQueue() {
        handler.postAtFrontOfQueue(new Runnable() {
            @Override
            public void run() {
                System.out.println("Execute immediately on main thread");
            }
        });
    }

    // Remove callbacks
    public void removeCallbacks() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("This won't execute");
            }
        };

        handler.post(runnable);
        handler.removeCallbacks(runnable);
    }
}

// 4. Thread Communication (Wait and Notify)
class ThreadCommunication {

    private String sharedData = "";

    public void waitNotifyExample() {
        final Object lock = new Object();

        Thread producerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    System.out.println("Producer: Producing data...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sharedData = "Hello from producer";
                    System.out.println("Producer: Data ready, notifying...");
                    lock.notifyAll();
                }
            }
        });

        Thread consumerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    System.out.println("Consumer: Waiting for data...");
                    while (sharedData.isEmpty()) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("Consumer: Received: " + sharedData);
                }
            }
        });

        consumerThread.start();
        producerThread.start();
    }
}

// 5. Thread Local Storage
class ThreadLocalStorageExample {

    private ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public void threadLocalDemo() {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("Value from Thread 1");
                System.out.println("Thread 1: " + threadLocal.get());
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("Value from Thread 2");
                System.out.println("Thread 2: " + threadLocal.get());
            }
        });

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Main thread: " + threadLocal.get());
    }
}

// 6. AsyncTask (Deprecated but still useful for understanding)
// Note: AsyncTask is deprecated in API 30, use Coroutines or ExecutorService instead
class ModernAsyncExample {

    private final ExecutorService executor = Executors.newFixedThreadPool(4);
    private final Handler handler = new Handler(Looper.getMainLooper());

    // Modern approach using ExecutorService and Handler
    public void executeAsyncTask() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                // Background work
                System.out.println("Working in background: " + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Post result to main thread
                final String result = "Task completed";
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Result on main thread: " + result);
                        // Update UI here
                    }
                });
            }
        });
    }

    // Cancelable task
    public Future<String> submitCancelableTask() {
        Future<String> future = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("Task started");
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(200);
                    System.out.println("Progress: " + (i + 1) * 10 + "%");
                }
                return "Task completed successfully";
            }
        });

        return future;
    }

    public void shutdown() {
        executor.shutdown();
    }
}

// Main demonstration
class ThreadCreationDemo {
    public static void demonstrateThreadCreation() {
        System.out.println("=== Android Java Thread Creation Examples ===\n");

        // 1. Basic thread creation
        System.out.println("--- 1. Basic Thread Creation ---");
        BasicThreadCreation basicThread = new BasicThreadCreation();
        basicThread.createBasicThread();
        basicThread.createNamedThread("MyCustomThread");

        // 2. Lambda thread
        System.out.println("\n--- 2. Lambda Thread ---");
        basicThread.createThreadWithLambda();

        // 3. Exception handling
        System.out.println("\n--- 3. Exception Handling ---");
        ThreadExceptionHandling exceptionHandling = new ThreadExceptionHandling();
        exceptionHandling.createThreadWithJoin();

        // 4. Handler and Looper
        System.out.println("\n--- 4. Handler and Looper ---");
        HandlerLooperExample handlerExample = new HandlerLooperExample();
        handlerExample.postToMainThread();

        // 5. Thread communication
        System.out.println("\n--- 5. Thread Communication ---");
        ThreadCommunication communication = new ThreadCommunication();
        communication.waitNotifyExample();

        // Wait for threads to complete
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("\n=== All Thread Creation Examples Completed ===");
    }
}
代码java中等

线程同步

使用锁、信号量和其他同步机制同步线程以保护共享资源

难度

7/10

预计时间

35 min

标签

java, android, multithreading, synchronization

前置条件

Intermediate Java, Understanding of concurrency

java
// Android Java Thread Synchronization Examples
// Using locks, semaphores, and synchronization mechanisms

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;

// 1. Synchronized Block and Method
class SynchronizedBlockExample {
    private int counter = 0;

    // Using synchronized block
    public void incrementCounter() {
        synchronized (this) {
            counter++;
            System.out.println("Counter: " + counter + " (Thread: " + Thread.currentThread().getName() + ")");
        }
    }

    // Synchronized method
    public synchronized void decrementCounter() {
        counter--;
        System.out.println("Counter: " + counter + " (Thread: " + Thread.currentThread().getName() + ")");
    }

    public synchronized int getCounter() {
        return counter;
    }
}

// 2. ReentrantLock
class ReentrantLockExample {
    private final Lock lock = new ReentrantLock();
    private int sharedResource = 0;

    public void performTask() {
        lock.lock();
        try {
            System.out.println("Lock acquired by " + Thread.currentThread().getName());
            // Critical section
            sharedResource++;
            Thread.sleep(100);
            System.out.println("Shared resource: " + sharedResource);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("Lock released by " + Thread.currentThread().getName());
        }
    }

    public void tryLockExample() {
        try {
            if (lock.tryLock(1, TimeUnit.SECONDS)) {
                try {
                    System.out.println("TryLock succeeded");
                    // Perform work
                } finally {
                    lock.unlock();
                }
            } else {
                System.out.println("Could not acquire lock");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // ReadWriteLock for multiple readers
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private String data = "Initial Data";

    public String readData() {
        readWriteLock.readLock().lock();
        try {
            System.out.println("Reading: " + data);
            return data;
        } finally {
            readWriteLock.readLock().unlock();
        }
    }

    public void writeData(String newValue) {
        readWriteLock.writeLock().lock();
        try {
            System.out.println("Writing: " + newValue);
            data = newValue;
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
}

// 3. Semaphore
class SemaphoreExample {
    private final Semaphore semaphore = new Semaphore(3); // Allow 3 concurrent accesses

    public void accessResource(int id) {
        boolean acquired = false;
        try {
            semaphore.acquire();
            acquired = true;
            System.out.println("Resource " + id + " acquired by " + Thread.currentThread().getName());

            // Simulate work
            Thread.sleep(1000);
            System.out.println("Resource " + id + " released");
        } catch (InterruptedException e) {
            System.out.println("Interrupted: " + e.getMessage());
            Thread.currentThread().interrupt();
        } finally {
            if (acquired) semaphore.release();
        }
    }

    public void tryAcquireExample(int id) {
        if (semaphore.tryAcquire()) {
            try {
                System.out.println("Resource " + id + " accessed immediately");
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                semaphore.release();
            }
        } else {
            System.out.println("Resource " + id + " not available");
        }
    }
}

// 4. Atomic Variables
class AtomicVariableExample {
    private final AtomicInteger atomicCounter = new AtomicInteger(0);
    private final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    private final AtomicReference<String> atomicReference = new AtomicReference<>("Initial");

    public void atomicIncrement() {
        int newValue = atomicCounter.incrementAndGet();
        System.out.println("Atomic counter: " + newValue);
    }

    public void compareAndSet() {
        boolean success = atomicBoolean.compareAndSet(false, true);
        System.out.println("CAS result: " + success + ", Value: " + atomicBoolean.get());
    }

    public void atomicUpdate() {
        atomicReference.updateAndGet(current -> {
            System.out.println("Current: " + current);
            return "Updated";
        });
        System.out.println("Final: " + atomicReference.get());
    }

    public void getAndAdd() {
        int oldValue = atomicCounter.getAndAdd(5);
        System.out.println("Old: " + oldValue + ", New: " + atomicCounter.get());
    }
}

// 5. CountDownLatch
class CountDownLatchExample {
    public void demonstrateLatch() {
        final CountDownLatch latch = new CountDownLatch(3);

        // Create worker threads
        for (int i = 0; i < 3; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("Worker " + index + " starting");
                        Thread.sleep(1000 + (int)(Math.random() * 1000));
                        System.out.println("Worker " + index + " completed");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            }).start();
        }

        // Main thread waits
        System.out.println("Main thread waiting for workers...");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("All workers completed!");
    }

    public void awaitWithTimeout() {
        final CountDownLatch latch = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            }).start();
        }

        // Wait with timeout
        try {
            if (latch.await(2, TimeUnit.SECONDS)) {
                System.out.println("Completed within timeout");
            } else {
                System.out.println("Timeout - not all completed");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

// 6. CyclicBarrier
class CyclicBarrierExample {
    public void demonstrateBarrier() {
        final CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                System.out.println("\n*** All threads reached barrier ***\n");
            }
        });

        for (int i = 0; i < 3; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("Thread " + index + ": Phase 1");
                        Thread.sleep(500);
                        System.out.println("Thread " + index + ": Waiting at barrier");

                        barrier.await();

                        System.out.println("Thread " + index + ": Phase 2");
                    } catch (Exception e) {
                        System.out.println("Barrier error: " + e.getMessage());
                    }
                }
            }).start();
        }
    }
}

// 7. BlockingQueue (Producer-Consumer)
class BlockingQueueExample {
    private final BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(5);
    private volatile boolean running = true;

    public void startProducer() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int item = 0;
                while (running) {
                    try {
                        queue.put(item);
                        System.out.println("Produced: " + item + " (Queue size: " + queue.size() + ")");
                        item++;
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }).start();
    }

    public void startConsumer() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (running) {
                    try {
                        int item = queue.take();
                        System.out.println("Consumed: " + item);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }).start();
    }

    public void stop() {
        running = false;
    }
}

// 8. Volatile Variables
class VolatileExample {
    private volatile boolean flag = false;

    public void startFlagWatcher() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Watcher: Waiting for flag...");
                while (!flag) {
                    // Busy wait (not recommended, use proper synchronization)
                }
                System.out.println("Watcher: Flag is now true!");
            }
        }).start();
    }

    public void setFlag() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("Main: Flag set to true");
    }
}

// 9. Thread-safe Collections
class ThreadSafeCollectionsExample {
    // ConcurrentHashMap
    private final ConcurrentHashMap<String, Integer> concurrentMap = new ConcurrentHashMap<>();

    public void addToConcurrentMap() {
        for (int i = 0; i < 10; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    concurrentMap.put("key" + index, index);
                    System.out.println("Added key" + index + ": " + index);
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Map size: " + concurrentMap.size());
    }

    // CopyOnWriteArrayList
    private final CopyOnWriteArrayList<String> concurrentList = new CopyOnWriteArrayList<>();

    public void addToList() {
        for (int i = 0; i < 5; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    concurrentList.add("Item " + index);
                    System.out.println("List size: " + concurrentList.size());
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("List: " + concurrentList);
    }

    // Concurrent Linked Queue
    private final ConcurrentLinkedQueue<Integer> concurrentQueue = new ConcurrentLinkedQueue<>();

    public void addToQueue() {
        for (int i = 0; i < 5; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    concurrentQueue.offer(index);
                    System.out.println("Queue size: " + concurrentQueue.size());
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Queue: " + concurrentQueue);
    }
}

// Main demonstration
class ThreadSynchronizationDemo {
    public static void demonstrateThreadSynchronization() {
        System.out.println("=== Android Java Thread Synchronization Examples ===\n");

        // 1. Synchronized block
        System.out.println("--- 1. Synchronized Block ---");
        final SynchronizedBlockExample syncExample = new SynchronizedBlockExample();

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    syncExample.incrementCounter();
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 2. ReentrantLock
        System.out.println("\n--- 2. ReentrantLock ---");
        final ReentrantLockExample lockExample = new ReentrantLockExample();

        for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    lockExample.performTask();
                }
            }).start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 3. Semaphore
        System.out.println("\n--- 3. Semaphore ---");
        final SemaphoreExample semaphoreExample = new SemaphoreExample();

        for (int i = 0; i < 5; i++) {
            final int index = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    semaphoreExample.accessResource(index);
                }
            }).start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 4. Atomic variables
        System.out.println("\n--- 4. Atomic Variables ---");
        final AtomicVariableExample atomicExample = new AtomicVariableExample();

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    atomicExample.atomicIncrement();
                }
            }).start();
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 5. CountDownLatch
        System.out.println("\n--- 5. CountDownLatch ---");
        CountDownLatchExample latchExample = new CountDownLatchExample();
        latchExample.demonstrateLatch();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 6. Thread-safe collections
        System.out.println("\n--- 6. Thread-safe Collections ---");
        ThreadSafeCollectionsExample collectionsExample = new ThreadSafeCollectionsExample();
        collectionsExample.addToConcurrentMap();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("\n=== All Thread Synchronization Examples Completed ===");
    }
}
代码java中等

线程池使用

使用ExecutorService、自定义线程池配置和Future对象高效使用线程池

难度

8/10

预计时间

40 min

标签

java, android, multithreading, thread pool

前置条件

Intermediate Java, Understanding of thread pools

java
// Android Java Thread Pool Examples
// Using ExecutorService, Custom Thread Pools, and Future

import java.util.concurrent.*;
import java.util.List;
import java.util.ArrayList;

// 1. ExecutorService Thread Pools
class ExecutorServicePools {

    // Fixed thread pool
    public void fixedThreadPoolExample() {
        ExecutorService executor = Executors.newFixedThreadPool(4);

        System.out.println("--- Fixed Thread Pool (4 threads) ---");

        long startTime = System.currentTimeMillis();

        List<Future<Void>> futures = new ArrayList<>();
        for (int taskId = 1; taskId <= 8; taskId++) {
            final int finalTaskId = taskId;
            Future<Void> future = executor.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    System.out.println("Task " + finalTaskId + " starting on " + Thread.currentThread().getName());
                    Thread.sleep(1000);
                    System.out.println("Task " + finalTaskId + " completed");
                    return null;
                }
            });
            futures.add(future);
        }

        // Wait for all tasks to complete
        for (Future<Void> future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        long endTime = System.currentTimeMillis();
        System.out.println("Total time: " + (endTime - startTime) + "ms");

        executor.shutdown();
    }

    // Cached thread pool
    public void cachedThreadPoolExample() {
        ExecutorService executor = Executors.newCachedThreadPool();

        System.out.println("\n--- Cached Thread Pool ---");

        List<Future<Integer>> futures = new ArrayList<>();
        for (int taskId = 1; taskId <= 10; taskId++) {
            final int finalTaskId = taskId;
            Future<Integer> future = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println("Task " + finalTaskId + " on " + Thread.currentThread().getName());
                    Thread.sleep(100 + (int)(Math.random() * 400));
                    return finalTaskId * 2;
                }
            });
            futures.add(future);
        }

        for (Future<Integer> future : futures) {
            try {
                Integer result = future.get();
                System.out.println("Result: " + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        executor.shutdown();
    }

    // Single thread executor
    public void singleThreadExecutorExample() {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        System.out.println("\n--- Single Thread Executor ---");

        for (int taskId = 1; taskId <= 5; taskId++) {
            final int finalTaskId = taskId;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Task " + finalTaskId + " on " + Thread.currentThread().getName());
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        executor.shutdown();
        try {
            executor.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // Scheduled executor
    public void scheduledExecutorExample() {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

        System.out.println("\n--- Scheduled Executor ---");

        // Schedule one-time task
        executor.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("Delayed task executed");
            }
        }, 2, TimeUnit.SECONDS);

        // Schedule at fixed rate
        final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("Periodic task at " + System.currentTimeMillis());
            }
        }, 0, 1, TimeUnit.SECONDS);

        // Run for 5 seconds then shutdown
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        future.cancel(true);
        executor.shutdown();
    }
}

// 2. Custom Thread Pool
class CustomThreadPoolExample {

    public void customThreadPoolExample() {
        // Create custom thread pool
        ThreadFactory threadFactory = new ThreadFactory() {
            private int count = 0;

            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("CustomPool-" + System.currentTimeMillis() + "-" + count++);
                thread.setDaemon(false);
                thread.setPriority(Thread.NORM_PRIORITY);
                return thread;
            }
        };

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            2, // Core pool size
            5, // Maximum pool size
            60L, // Keep-alive time
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(10), // Work queue
            threadFactory,
            new ThreadPoolExecutor.CallerRunsPolicy() // Rejection policy
        );

        System.out.println("\n--- Custom Thread Pool ---");

        // Submit tasks
        for (int i = 1; i <= 10; i++) {
            final int taskId = i;
            try {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Task " + taskId + " on " + Thread.currentThread().getName());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (RejectedExecutionException e) {
                System.out.println("Task " + i + " rejected");
            }
        }

        executor.shutdown();
        try {
            executor.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

// 3. ExecutorService with Future
class FutureExample {

    public void submitWithFuture() {
        ExecutorService executor = Executors.newFixedThreadPool(3);

        System.out.println("\n--- ExecutorService with Future ---");

        // Submit callable tasks
        List<Future<Integer>> futures = new ArrayList<>();
        for (int taskId = 1; taskId <= 5; taskId++) {
            final int finalTaskId = taskId;
            Future<Integer> future = executor.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println("Task " + finalTaskId + " starting");
                    Thread.sleep(500 + (int)(Math.random() * 1000));
                    System.out.println("Task " + finalTaskId + " completed");
                    return finalTaskId * 10;
                }
            });
            futures.add(future);
        }

        // Process results as they complete
        for (Future<Integer> future : futures) {
            try {
                Integer result = future.get();
                System.out.println("Future result: " + result);
            } catch (Exception e) {
                System.out.println("Future error: " + e.getMessage());
            }
        }

        executor.shutdown();
    }

    public void invokeAllExample() {
        ExecutorService executor = Executors.newFixedThreadPool(4);

        List<Callable<Integer>> tasks = new ArrayList<>();
        for (int taskId = 1; taskId <= 10; taskId++) {
            final int finalTaskId = taskId;
            tasks.add(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(1000);
                    return finalTaskId;
                }
            });
        }

        long startTime = System.currentTimeMillis();

        try {
            List<Future<Integer>> futures = executor.invokeAll(tasks);

            for (Future<Integer> future : futures) {
                System.out.println("Completed: " + future.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("InvokeAll time: " + (endTime - startTime) + "ms");

        executor.shutdown();
    }

    public void invokeAnyExample() {
        ExecutorService executor = Executors.newFixedThreadPool(4);

        List<Callable<Integer>> tasks = new ArrayList<>();
        for (int taskId = 1; taskId <= 5; taskId++) {
            final int finalTaskId = taskId;
            tasks.add(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sleepTime = (int)(Math.random() * 3000) + 1000;
                    System.out.println("Task " + finalTaskId + " will take " + sleepTime + "ms");
                    Thread.sleep(sleepTime);
                    return finalTaskId;
                }
            });
        }

        try {
            Integer result = executor.invokeAny(tasks);
            System.out.println("First completed task: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        executor.shutdown();
    }
}

// 4. Completion Service
class CompletionServiceExample {

    public void completionServiceDemo() {
        ExecutorService executor = Executors.newFixedThreadPool(4);
        CompletionService<Integer> completionService = new ExecutorCompletionService<>(executor);

        System.out.println("\n--- Completion Service ---");

        // Submit tasks
        for (int taskId = 1; taskId <= 5; taskId++) {
            final int finalTaskId = taskId;
            completionService.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sleepTime = 1000 + (int)(Math.random() * 2000);
                    System.out.println("Task " + finalTaskId + " will take " + sleepTime + "ms");
                    Thread.sleep(sleepTime);
                    return finalTaskId;
                }
            });
        }

        // Process as they complete
        for (int i = 0; i < 5; i++) {
            try {
                Future<Integer> future = completionService.take();
                Integer result = future.get();
                System.out.println("Completed task: " + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        executor.shutdown();
    }
}

// 5. ForkJoin Pool (Parallel processing)
class ForkJoinPoolExample {

    public void parallelSum() {
        ForkJoinPool pool = new ForkJoinPool();

        System.out.println("\n--- ForkJoin Pool ---");

        RecursiveSumTask task = new RecursiveSumTask(1, 1000000);
        Integer result = pool.invoke(task);

        System.out.println("Parallel sum result: " + result);
        pool.shutdown();
    }

    public static class RecursiveSumTask extends RecursiveTask<Integer> {
        private final int start;
        private final int end;

        public RecursiveSumTask(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        protected Integer compute() {
            if (end - start <= 10000) {
                // Compute directly
                int sum = 0;
                for (int i = start; i <= end; i++) {
                    sum += i;
                }
                return sum;
            } else {
                // Split task
                int mid = (start + end) / 2;
                RecursiveSumTask leftTask = new RecursiveSumTask(start, mid);
                RecursiveSumTask rightTask = new RecursiveSumTask(mid + 1, end);

                leftTask.fork();
                Integer rightResult = rightTask.compute();
                Integer leftResult = leftTask.join();

                return leftResult + rightResult;
            }
        }
    }
}

// 6. Async Tasks with Callback
class AsyncWithCallback {

    public interface AsyncCallback<T> {
        void onSuccess(T result);
        void onFailure(Exception e);
    }

    public void executeAsyncTask(final AsyncCallback<String> callback) {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                // Simulate work
                Thread.sleep(1000);
                return "Task completed successfully";
            }
        }).addCallback(new AsyncCallback<String>() {
            @Override
            public void onSuccess(String result) {
                callback.onSuccess(result);
            }

            @Override
            public void onFailure(Exception e) {
                callback.onFailure(e);
            }
        });

        executor.shutdown();
    }

    // Helper method to add callback to Future
    private <T> Future<T> addCallback(final Future<T> future, final AsyncCallback<T> callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    T result = future.get();
                    callback.onSuccess(result);
                } catch (Exception e) {
                    callback.onFailure(e);
                }
            }
        }).start();

        return future;
    }
}

// 7. Thread Pool Monitoring
class ThreadPoolMonitoring {

    public void monitorThreadPool() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            2, 4, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(10)
        );

        System.out.println("\n--- Thread Pool Monitoring ---");

        // Start monitoring thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!executor.isTerminated()) {
                    System.out.println("Active: " + executor.getActiveCount() +
                                     ", Pool: " + executor.getPoolSize() +
                                     ", Queue: " + executor.getQueue().size() +
                                     ", Completed: " + executor.getCompletedTaskCount());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }).start();

        // Submit some tasks
        for (int i = 0; i < 10; i++) {
            final int taskId = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.println("Task " + taskId + " completed");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        executor.shutdown();
        try {
            executor.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

// Main demonstration
class ThreadPoolDemo {
    public static void demonstrateThreadPoolUsage() {
        System.out.println("=== Android Java Thread Pool Examples ===\n");

        // 1. ExecutorService pools
        ExecutorServicePools executorPools = new ExecutorServicePools();
        executorPools.fixedThreadPoolExample();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        executorPools.cachedThreadPoolExample();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        executorPools.singleThreadExecutorExample();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        executorPools.scheduledExecutorExample();

        // 2. Custom thread pool
        CustomThreadPoolExample customPool = new CustomThreadPoolExample();
        customPool.customThreadPoolExample();

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 3. Future example
        System.out.println("\n--- Future Examples ---");
        FutureExample futureExample = new FutureExample();
        futureExample.submitWithFuture();

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        futureExample.invokeAnyExample();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 4. Completion service
        CompletionServiceExample completionExample = new CompletionServiceExample();
        completionExample.completionServiceDemo();

        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 5. ForkJoin pool
        ForkJoinPoolExample forkJoinExample = new ForkJoinPoolExample();
        forkJoinExample.parallelSum();

        // 6. Thread pool monitoring
        ThreadPoolMonitoring monitoring = new ThreadPoolMonitoring();
        monitoring.monitorThreadPool();

        try {
            Thread.sleep(12000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("\n=== All Thread Pool Examples Completed ===");
    }
}

工具

经常与该案例搭配使用的工具

相关内容

相关案例集合

相关内容

相关专题

工具使用指南

帮助搜索与理解工作流的案例说明

概览

线程池卡片适合核对任务数量,同步卡片适合检查共享状态。sleep 参数或线程优先级不等于实测完成期限。

选择有明确目标的代码练习

每次追踪一个受控分支,阅读源码不能确定并发输出顺序。

使用方式

  1. 1在页面阅读指定卡片;如需运行,只把选定方法复制到一次性测试项目。这里展示代码,不是运行中的 Android 应用。

常见失败与 edge cases

  • 运行边界

    这些练习追踪展示的代码,不代表所有类能一起编译,也不代表已实测 Android 设备、文件系统、日志或权限流程。

示例

1. 分别计算任务与工作线程

如何使用

在「线程池使用」检查 fixedThreadPoolExample,核对任务 ID、线程池构造参数和后续 get 调用。

结果

ID 为 1 至 8 的八个任务提交到配置为 4 个工作线程的池,等待 future 后再 shutdown。这不证明精确耗时两秒或固定输出顺序。

2. 追踪同一个计数器实例

如何使用

在「线程同步」从声明的 0 开始,依次推导两次 incrementCounter、一次 decrementCounter,最后 getCounter,假设没有其他调用。

结果

算术结果为 1。加、减、读取使用同一个实例监视器。结果以这些调用完成为前提,不是任意调度下的性能实测。

3. 不要释放尚未取得的许可

如何使用

在「线程同步」检查 accessResource,比较 acquire 返回前中断和成功取得许可后清理。

结果

acquired 初始为 false,只有 acquire 返回后才变为 true;清理仅在 true 时释放,获取过程中被中断不会增加许可。初始有 3 个许可,不代表必然有三个任务同时运行。

专题

种群生态、多样性与食物网计算器

用教学型生态模型探索种群增长、环境承载力、物种相互作用、样本多样性和营养级能量传递。

专题

多轨音频混音与叠加

平衡多路音源,按时间放置叠加层,用交叉淡化平滑衔接,并通过试听与峰值检查确认输出不削波。

专题

XLSX 多来源 ETL 导入与数仓交付

把多个工作簿和 CSV 来源整理成可追溯、带 schema 依据的 SQL、Parquet 与对象存储批处理输入。

专题

多语言网页字体配对与交付验证

检查字体字形覆盖,用真实文案比较标题与正文字体,配置可变轴和响应式字号,并验收上线后的字体加载与回退表现。

首页
工具3215
案例593
可视化472
文件查看器103
专题239
标签333
图像、音频与视频657

图像、音频、视频处理、转换与优化工具

数学、日期与金融647

计算器、数值计算、日期逻辑、统计与金融工具

设计与颜色298

颜色、版式、图形、视觉样式与设计辅助工具

文本与写作209

写作、清洗、格式化、提取与文本分析工具

开发与 Web194

开发者工具、网络处理、Web 调试与自动化辅助工具

转换与编码175

格式、文件、压缩包、单位与编码转换工具

潜客测验发布与联系人交接
讣告、悼词与纪念卡的家属复核及交付
本地习惯记录的备份、恢复演练与分享前检查
长文改编 X 串文与 LinkedIn 帖子的事实复核与交付
Markdown 互动课件审校、离线演示与交付

© 2026 Elysia Tools. 保留所有权利。

文档价格关于我们服务条款隐私政策Cookie 政策联系我们

Development

JSON 转 Java 类

将 JSON 数据转换为 Java class,含 Jackson @JsonProperty 注解、私有字段及 getter/setter

Design

Figma iOS Android pt dp sp 响应式设计规范卡

把 Figma 像素值换算成平台正确单位并生成开发者规范卡:iOS pt(@1x/@2x/@3x 及各倍率渲染 px)、Android dp/sp(mdpi…xxxhdpi 全密度桶渲染表、字体缩放)、Web rem/vw 与 clamp() 流式排版片段;按 iOS 44pt / Android 48dp / WCAG 44px 与 24px 审计最小触控目标,并标注 Material 窗口类与 Tailwind 断点。

Development

Java 正则测试器

在线测试 java.util.regex 正则:(?<name>…) 命名组、占有优先量词与原子组、Pattern 标志、\d 的 ASCII 与 UNICODE_CHARACTER_CLASS 语义、可直接粘贴的 Java 代码。

Kotlin

Android Kotlin 多线程编程示例

Android Kotlin 多线程编程示例,包括线程创建、锁同步和线程池使用

Swift

macOS Swift 多线程示例

macOS Swift 多线程示例,包括线程创建、使用锁同步和线程池使用

Rust

Web Rust 多线程编程示例

Web Rust 多线程编程示例,包括线程创建、同步和线程池

Java

Android Java 加密安全示例

Android Java 加密安全示例,包括哈希计算、AES加密解密和Base64编码

Java

Android Java 数据库示例

Android Java 数据库示例,包括SQLite连接、SQL查询和事务处理

Java

Android Java 数据结构示例

Android Java 数据结构示例,包括数组、哈希表和链表的常用操作