猪八戒网做动漫弹幕网站,兴县做网站的公司,wordpress更改复原,小制作灯笼简单又漂亮开启多线程下变量共享与私有问题
#x1f335;ThreadLocal和Atomic是Java中用于多线程编程的两个重要工具。
ThreadLocal是一个线程局部变量#xff0c;它为每个线程提供了独立的变量副本#xff0c;确保每个线程都可以访问自己的变量副本而不会影响其他线程的变量。在多线…开启多线程下变量共享与私有问题
ThreadLocal和Atomic是Java中用于多线程编程的两个重要工具。
ThreadLocal是一个线程局部变量它为每个线程提供了独立的变量副本确保每个线程都可以访问自己的变量副本而不会影响其他线程的变量。在多线程环境下使用ThreadLocal可以避免线程安全问题。
Atomic是一组原子操作类提供了一些常见的原子操作如原子更新整型、原子更新引用等。在多线程环境下使用Atomic可以保证操作的原子性避免出现数据竞争和线程安全问题。
下面是一个简单的示例ThreadLocal和Atomic在多线程环境下的用法
import java.util.concurrent.atomic.AtomicInteger;public class ThreadLocalAndAtomicExample {private static ThreadLocalInteger threadLocal ThreadLocal.withInitial(() - 0);private static AtomicInteger atomicInteger new AtomicInteger(0);public static void main(String[] args) {for (int i 0; i 5; i) {new Thread(() - {int localValue threadLocal.get();localValue;threadLocal.set(localValue);int atomicValue atomicInteger.incrementAndGet();System.out.println(ThreadLocal value: threadLocal.get() Atomic value: atomicValue);}).start();}}
}在上面的示例中我们创建了一个ThreadLocal变量和一个AtomicInteger变量然后启动了5个线程每个线程对这两个变量进行操作。通过输出结果可以看到ThreadLocal变量在每个线程中独立维护了自己的值而AtomicInteger变量则保证了操作的原子性。
总结来说在多线程编程中ThreadLocal可以用于保证每个线程拥有独立的变量副本避免线程安全问题而Atomic可以用于保证操作的原子性避免数据竞争和线程安全问题。在实际开发中我们可以根据具体需求选择合适的工具来保证多线程程序的正确性和性能。
下面是一个类似的示例,多线程使用EasyExcel处理导出数据使用ExecutorService、CountDownLatch、ThreadLocal和Atomic在多线程环境下的用法 public void exportAllApply(HttpServletResponse response, ApplyScope applyScope){SimpleDateFormat format new SimpleDateFormat(yyyy-MM-dd);String fileName APPLY -Calendar.getInstance().getTimeInMillis().xlsx;ListApplyExportVo allApply new ArrayList();try {ExcelWriter excelWriter EasyExcel.write(fileName, ApplyExportVo.class).build();WriteSheet writeSheet EasyExcel.writerSheet(sheet1).build();int threadCount 5;//默认开启五个线程ExecutorService executorService Executors.newFixedThreadPool(threadCount);CountDownLatch latch new CountDownLatch(threadCount);ThreadLocalApplyScope threadLocalApply ThreadLocal.withInitial(ApplyScope::new);AtomicLong updatePage new AtomicLong(1);for (int i 0; i threadCount; i) {int finalI i1;executorService.execute(() - {ApplyScope localApply threadLocalApply.get();BeanUtils.copyProperties(applyScope,localApply);while (true) {long currentPage updatePage.getAndIncrement();//更新分页信息需同步updatePage(localApply, currentPage);ApplyGeneralVo applyGeneralVo this.queryApplyView(localApply);ListApplyVo applyList applyGeneralVo.getApplyList();log.info(线程threadCountfinalI当前页码currentPage,数量applyList.size());if(CollectionUtils.isEmpty(applyList)){break;}ListApplyExportVo exportVos BeanUtil.toBeanList(applyList, ApplyExportVo.class);allApply.addAll(exportVos);}latch.countDown();});}executorService.shutdown();latch.await();Long startWrite Calendar.getInstance().getTimeInMillis();Optional.ofNullable(allApply).ifPresent(data - {excelWriter.write(data, writeSheet);});excelWriter.finish();log.info(----- writeExcelTime: (Calendar.getInstance().getTimeInMillis() - startWrite));} catch (Exception e) {e.printStackTrace();}
}private synchronized long updatePage(ApplyScope scope, long current){PageScope page scope.getPages();if(page null){page new PageScope();page.setSize(1000L);page.setCurrent(current);scope.setPages(page);} else {page.setCurrent(current);}return page.getCurrent();
}
在上面的示例中我们创建了一个固定大小的线程池ExecutorService并使用CountDownLatch来等待所有线程执行完毕。每个线程在执行时会对ThreadLocal变量和AtomicInteger变量进行操作并在操作完成后调用CountDownLatch的countDown()方法来通知主线程。
通过输出结果可以看到每个线程中的ThreadLocal变量和AtomicInteger变量都能够正确地保持独立和原子性同时CountDownLatch确保了所有线程执行完毕后主线程才会继续执行。