import com.israfil.concurrent.ThreadPool; public class ConcurrentTaskExample { public static void main(String[] args) { ThreadPool threadPool = new ThreadPool(5); for (int i = 0; i < 10; i++) { Runnable task = new MyTask(i); threadPool.submit(task); } threadPool.shutdown(); } } class MyTask implements Runnable { private int taskId; public MyTask(int taskId) { this.taskId = taskId; } @Override public void run() { System.out.println("Task ID : " + taskId + " performed by " + Thread.currentThread().getName()); } } import com.israfil.concurrent.Lock; import com.israfil.concurrent.RunnableWithLock; public class ThreadSyncExample { private static int counter = 0; private static Lock lock = new Lock(); public static void main(String[] args) { for (int i = 0; i < 5; i++) { RunnableWithLock task = new IncrementTask(); new Thread(task).start(); } } static class IncrementTask implements RunnableWithLock { @Override public void run() { lock.lock(); try { counter++; System.out.println("Counter: " + counter); } finally { lock.unlock(); } } } }


上一篇:
下一篇:
切换中文