Tag: multithreading

静态变量与易失性

我只是以线程的角度提问……可能已经回答了很多次,但请帮助我理解这一点。 参考java中的 post Volatile Vs Static 要求一个静态变量值也将是所有线程的一个值,那么我们为什么要选择volatile呢? 我找到了以下示例: public class VolatileExample { public static void main(String args[]) { new ExampleThread(“Thread 1 “).start(); new ExampleThread(“Thread 2 “).start(); } } class ExampleThread extends Thread { private static volatile int testValue = 1; public ExampleThread(String str){ super(str); } public void run() { for (int i = 0; i […]

如何在junit中使用未捕获的exception处理程序进行multithreading测试?

我有以下代码,我希望成功运行完成,但代码在“失败”(“不应该达到”)行失败;“。 有人可以解释为什么不调用默认的未捕获exception处理程序: public class UncaughtExceptionTest extends TestCase implements UncaughtExceptionHandler { private final List uncaughtExceptions = new CopyOnWriteArrayList(); class UncaughtExceptionTestInnerClass implements Runnable { private final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); private final CountDownLatch latch; UncaughtExceptionTestInnerClass(CountDownLatch latch) { this.latch = latch; executor.schedule(this, 50, TimeUnit.MILLISECONDS); } @Override public void run() { System.out.println(“This is printed”); fail(“this should fail”); latch.countDown(); […]

Java中的巧妙异步重绘

我有一个用例来自GUI问题,我想提交给你的睿智。 用例 我有一个GUI,根据用户在GUI中设置的一些参数显示计算结果。 例如,当用户移动滑块时,会触发几个事件,这些事件都会触发新的计算。 当用户将滑块值从A调整为B时,会触发数十个事件。 但是计算可能需要几秒钟,而滑块调整可以每隔几百毫秒触发一次事件。 如何编写一个能够监听这些事件的正确线程,并对它们进行过滤以使结果的重绘更加生动? 理想情况下,你会喜欢这样的东西 收到第一次更改事件后立即开始新的计算; 如果收到新事件则取消第一次计算,并用新参数开始新计算; 但要确保最后一个事件不会丢失,因为最后完成的计算需要是具有最后更新参数的计算。 我试过的 我的一个朋友(A. Cardona)提出了一种更新程序线程的低级方法,可以防止太多事件触发计算。 我在这里复制粘贴(GPL): 他把它放在一个扩展Thread的类中: public void doUpdate() { if (isInterrupted()) return; synchronized (this) { request++; notify(); } } public void quit() { interrupt(); synchronized (this) { notify(); } } public void run() { while (!isInterrupted()) { try { final long r; synchronized (this) […]

在与中断线程的关系之前,是否在线程上调用interrupt()

换句话说,我想知道在中断线程中检测到中断时,在中断之前更改变量是否始终可见。 例如 private int sharedVariable; public static void interruptTest() { Thread someThread = new Thread(() -> { try { Thread.sleep(5000); } catch (InterruptedException e) { // Is it here guaranteed that changes before interrupt are always visible here? System.out.println(sharedVariable); } }); someThread.start(); Thread.sleep(1000); sharedVariable = 10; someThread.interrupt(); } 我试图在Java语言规范和Java教程 中提到的java.util.concurrent包的Summary页面中找到答案,但没有提到interrupt 。 我知道volatile和其他同步原语但我需要它们吗?

multithreadinghttpClient

public class test { public static final int nThreads = 2; public static void main(String[] args) throws ExecutionException, InterruptedException{ // Runnable myrunnable = new myRunnable(); ExecutorService execute = Executors.newFixedThreadPool(nThreads); for (int i = 0; i = maxCalls) { break; } try { Thread.currentThread().sleep(sleepMillis); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } […]

如何配置spring来执行重叠的fixedRate任务?

我正在尝试使用java spring中的@Scheduled注释以固定速率执行任务。 但是,默认情况下,如果任务比速率慢,spring将不会以固定速率执行fixedRate任务。 是否有一些设置我可以添加到我的弹簧配置来改变这种行为? 例子 : @Service public class MyTask{ @Scheduled(fixedRate = 1000) public void doIt(){ // this sometimes takes >1000ms, in which case the next execution is late … } } 我有一个解决方案 ,但似乎不太理想。 基本上,我只是用线程池替换默认的单线程执行器,然后我有一个调度方法调用异步方法,因为@Async注释允许并发执行: @Service public class MyTask{ @Async public void doIt(){ // this sometimes takes >1000ms, but the next execution is on time […]

并发log4j

我有自己的日志引擎,它将日志写入带有阻塞队列的单独线程。 为了使用“标准软件”,我正在考虑切换到log4j。 我不希望我的高度并发软件被日志命令放慢速度,这些日志命令将所有内容写入磁盘,就像调用命令一样。 可以将log4j用作垃圾箱吗?

同步块中的读取障碍和写入障碍是什么

我正在研究同步和volatile变量如何在java中工作,我遇到了一个名为read and write barrier的概念。 任何人都可以帮助我理解这些术语的含义

在Java中,是否需要同步不使用静态或类变量的方法?

仅使用局部变量的方法是否会遇到任何线程问题? 在某处提到过,使用局部变量的方法被复制到每个线程堆栈框架中,除非它使用类级别或静态引用/变量,否则不需要为multithreading实现进行同步?

Java同步HashMap中的size(),put(),remove(),get()是否为primefaces?

我将Java Map声明为 Map map = Collections.synchronizedMap(new HashMap()); 处理并发问题,并在地图上同步其上的所有操作。 但是,我读到当操作是primefaces操作时,synchronizedMap上不需要synchronizedMap 。 我检查了Java API,HashMap的文档似乎没有提到哪些是primefaces的,所以我不确定是哪些。 我正在同步以下对地图的调用: map.size() map.put() map.remove() map.get() 但如果有些是primefaces的,那么似乎并不需要同步。 哪个是primefaces的?