创建java.util.concurrent.atomic.AtomicBoolean类的实例对象,
运用:在接口项目中的dao层查询时
public static AtomicBoolean notify = new AtomicBoolean(false);
线程中
new Thread() {
public void run() {
while (true) {
//先获取状态,如果其他线程在使用则会改为true,此线程就会跳出
while (notify.get()){
continue;
}
notify.set(true);
}
};
}.start();
同时可以配合java.util.concurrent.CountDownLatch这个同步工具类
//threadCount表示查询数据库后需要进行操作的数据的条数
final CountDownLatch latch = new CountDownLatch(threadCount);
/*ExecutorService 是Java提供的线程池它可以有效控制最大并发线程数,提高系统资源的使用率,
同时避免过多资源竞争,避免堵塞,同时提供定时执行、定期执行、单线程、并发数控制等功能,也不用使用TimerTask了*/
ExecutorService pool = Executors.newFixedThreadPool(threadCount);
//如何是list集合则获取每一个执行对象
//push查找出来的对象,liquidations 为list<pust> 对象
for (final Push push : liquidations) {
pool.execute( ... );
}
latch.await();
pool.shutdown();
//最后再设置为false,其他线程调用
notify.set(false);