随笔分类 - java线程
摘要:线程池相关API JDK 5.0起提供了线程池相关API:ExecutorService和 Executors ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor void execute(Runnable command):执行任务/命令,没有返回值,一
阅读全文
摘要:import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; class NumThread implemen
阅读全文
摘要:线程窗口买票例子(存在线程安全问题) 开发中:优先选择Runnable接口的方式 原因:1.实现的方式没有类的单继承性的局限性 2.实现的方式更适合来处理多个线程有共享数据的情况 二者联系:public class Thread implements Runnable 相同点:两种都要重写run()
阅读全文
摘要:import java.util.concurrent.locks.ReentrantLock; // 测试 public class LockTest { public static void main(String[] args) { Windows w = new Windows(); Thr
阅读全文
摘要:改进后线程安全 // 懒汉式 class LazySingleton { // 私有化构造器 private LazySingleton() {} // 类的内部创建实例 private static LazySingleton instance = null; public static Lazy
阅读全文