新建,,就绪,运行,阻塞,死亡
synchronized (Object) { for (;;) { if (ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ": " + ticket); ticket--; } } }
class Bank { private Bank() {} private static Bank instance = null; public static Bank getInstance() { if (instance == null) { synchronized (Bank.class) { if (instance == null) { instance = new Bank(); } } } return instance; } }
// 1.指定线程数量的连接池 ExecutorService service = Executors.newFixedThreadPool(10); ThreadPoolExecutor service1 = (ThreadPoolExecutor) service; // 设置线程池属性 service1.setCorePoolSize(0); // 2.执行线程池操作,需要提供实现Runnable接口或Callable接口实现类的对象 service.execute(new NumberThread()); // 3.关闭连接池 service.shutdown();